going-rogue/erynrl/events.py

80 lines
2.6 KiB
Python
Raw Normal View History

2022-04-30 21:59:01 -07:00
# Eryn Wells <eryn@erynwells.me>
2022-05-07 11:56:55 -07:00
'''Defines event handling mechanisms.'''
2022-05-08 10:03:28 -07:00
from typing import Optional, TYPE_CHECKING
2022-04-30 21:59:01 -07:00
import tcod
from . import log
from .actions import Action, ExitAction, RegenerateRoomsAction, BumpAction, WaitAction
from .geometry import Direction
if TYPE_CHECKING:
from .engine import Engine
2022-04-30 21:59:01 -07:00
class EventHandler(tcod.event.EventDispatch[Action]):
2022-05-07 11:56:55 -07:00
'''Handler of `tcod` events'''
def __init__(self, engine: 'Engine'):
super().__init__()
self.engine = engine
def wait_for_events(self):
'''Wait for events and handle them.'''
for event in tcod.event.wait():
self.handle_event(event)
def handle_event(self, event: tcod.event.Event) -> None:
'''Handle the given event. Transform that event into an Action via an EventHandler and perform it.'''
action = self.dispatch(event)
# Unhandled event. Ignore it.
if not action:
log.EVENTS.debug('Unhandled event: %s', event)
return
result = self.engine.process_hero_action(action)
# Player's action failed, don't proceed with turn.
if not result.success and result.done:
return
self.engine.process_entity_actions()
self.engine.update_field_of_view()
2022-04-30 21:59:01 -07:00
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
return ExitAction(self.engine.hero)
2022-04-30 21:59:01 -07:00
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None
hero = self.engine.hero
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-12 20:40:46 -07:00
case tcod.event.KeySym.ESCAPE:
action = ExitAction(hero)
case tcod.event.KeySym.SPACE:
action = RegenerateRoomsAction(hero)
2022-05-07 22:34:43 -07:00
case tcod.event.KeySym.PERIOD:
action = WaitAction(hero)
2022-04-30 21:59:01 -07:00
return action