from typing import Optional import tcod from actions import BaseAction, QuitAction, BumpAction from engine import Engine #event handler is one part of the engine class EventHandler(tcod.event.EventDispatch[BaseAction]): def __init__(self, engine: Engine): super().__init__() self.engine = engine def handle_events(self) -> None: for event in tcod.event.wait(): action = self.dispatch(event) if action is None: continue action.apply() #entity references the engine #callbacks def ev_quit(self, event: tcod.event.Quit) -> Optional[BaseAction]: return QuitAction() def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[BaseAction]: key = event.sym #SDL stuff, neat. player = self.engine.player #parse player input match key: case tcod.event.KeySym.ESCAPE: return QuitAction() case tcod.event.KeySym.UP: return BumpAction(player, xdir = 0, ydir = -1) case tcod.event.KeySym.DOWN: return BumpAction(player, xdir = 0, ydir = 1) case tcod.event.KeySym.LEFT: return BumpAction(player, xdir = -1, ydir = 0) case tcod.event.KeySym.RIGHT: return BumpAction(player, xdir = 1, ydir = 0) case _: return None