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-05-07 11:22:54 -07:00
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
import tcod
|
2023-03-07 21:29:05 -08:00
|
|
|
import tcod.event as tev
|
2022-05-07 11:22:54 -07:00
|
|
|
|
2022-05-28 08:52:54 -07:00
|
|
|
from .actions.action import Action
|
2023-03-11 01:09:53 -08:00
|
|
|
from .actions.game import BumpAction, WaitAction
|
2023-03-07 21:29:05 -08:00
|
|
|
from .geometry import Direction
|
2022-05-07 12:25:46 -07:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .engine import Engine
|
|
|
|
|
2023-02-10 21:25:00 -08:00
|
|
|
|
2023-03-07 21:29:05 -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
|
|
|
|
2022-05-07 12:25:46 -07:00
|
|
|
def __init__(self, engine: 'Engine'):
|
|
|
|
super().__init__()
|
|
|
|
self.engine = engine
|
|
|
|
|
2023-03-07 21:29:05 -08:00
|
|
|
def ev_keydown(self, event: tev.KeyDown) -> Optional[Action]:
|
2022-04-30 21:59:01 -07:00
|
|
|
action: Optional[Action] = None
|
|
|
|
|
2022-05-07 12:25:46 -07:00
|
|
|
hero = self.engine.hero
|
|
|
|
|
2023-02-12 14:29:45 -08:00
|
|
|
is_shift_pressed = bool(event.mod & tcod.event.Modifier.SHIFT)
|
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
sym = event.sym
|
2022-05-07 11:57:08 -07:00
|
|
|
match sym:
|
|
|
|
case tcod.event.KeySym.b:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.SouthWest)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.h:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.West)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.j:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.South)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.k:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.North)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.l:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.East)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.n:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.SouthEast)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.u:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.NorthEast)
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.y:
|
2022-05-07 12:25:46 -07:00
|
|
|
action = BumpAction(hero, Direction.NorthWest)
|
2022-05-07 22:34:43 -07:00
|
|
|
case tcod.event.KeySym.PERIOD:
|
2023-02-12 14:29:45 -08:00
|
|
|
if not is_shift_pressed:
|
|
|
|
action = WaitAction(hero)
|
2022-04-30 21:59:01 -07:00
|
|
|
|
|
|
|
return action
|
2022-05-14 23:43:04 -07:00
|
|
|
|
2023-02-10 21:25:00 -08:00
|
|
|
|
2023-03-07 21:29:05 -08:00
|
|
|
class GameOverEventHandler(tev.EventDispatch[Action]):
|
2022-05-14 23:43:04 -07:00
|
|
|
'''When the game is over (the hero dies, the player quits, etc), this event handler takes over.'''
|
|
|
|
|
2023-03-07 21:29:05 -08:00
|
|
|
def __init__(self, engine: 'Engine'):
|
|
|
|
super().__init__()
|
|
|
|
self.engine = engine
|