from typing import Optional import tcod from actions import Action, QuitAction, MoveAction from engine import Engine #event handler is one part of the engine class EventHandler(tcod.event.EventDispatch[Action]): 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[Action]: return QuitAction() def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: 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 MoveAction(player, xdir = 0, ydir = -1) case tcod.event.KeySym.DOWN: return MoveAction(player, xdir = 0, ydir = 1) case tcod.event.KeySym.LEFT: return MoveAction(player, xdir = -1, ydir = 0) case tcod.event.KeySym.RIGHT: return MoveAction(player, xdir = 1, ydir = 0) case _: return None