going-rogue/erynrl/events.py

61 lines
1.9 KiB
Python
Raw Normal View History

2022-04-30 21:59:01 -07:00
# Eryn Wells <eryn@erynwells.me>
2022-05-08 10:03:28 -07:00
from typing import Optional, TYPE_CHECKING
2022-04-30 21:59:01 -07:00
import tcod
import tcod.event as tev
from .actions.action import Action
from .actions.game import BumpAction, WaitAction
from .geometry import Direction
if TYPE_CHECKING:
from .engine import Engine
2023-02-10 21:25:00 -08:00
class EngineEventHandler(tev.EventDispatch[Action]):
'''Handles event on behalf of the game engine, dispatching Actions back to the engine.'''
2022-05-07 11:56:55 -07:00
def __init__(self, engine: 'Engine'):
super().__init__()
self.engine = engine
def ev_keydown(self, event: tev.KeyDown) -> Optional[Action]:
2022-04-30 21:59:01 -07:00
action: Optional[Action] = None
hero = self.engine.hero
is_shift_pressed = bool(event.mod & tcod.event.Modifier.SHIFT)
2022-04-30 21:59:01 -07:00
sym = event.sym
match sym:
case tcod.event.KeySym.b:
action = BumpAction(hero, Direction.SouthWest)
case tcod.event.KeySym.h:
action = BumpAction(hero, Direction.West)
case tcod.event.KeySym.j:
action = BumpAction(hero, Direction.South)
case tcod.event.KeySym.k:
action = BumpAction(hero, Direction.North)
case tcod.event.KeySym.l:
action = BumpAction(hero, Direction.East)
case tcod.event.KeySym.n:
action = BumpAction(hero, Direction.SouthEast)
case tcod.event.KeySym.u:
action = BumpAction(hero, Direction.NorthEast)
case tcod.event.KeySym.y:
action = BumpAction(hero, Direction.NorthWest)
2022-05-07 22:34:43 -07:00
case tcod.event.KeySym.PERIOD:
if not is_shift_pressed:
action = WaitAction(hero)
2022-04-30 21:59:01 -07:00
return action
2023-02-10 21:25:00 -08:00
class GameOverEventHandler(tev.EventDispatch[Action]):
'''When the game is over (the hero dies, the player quits, etc), this event handler takes over.'''
def __init__(self, engine: 'Engine'):
super().__init__()
self.engine = engine