2022-01-07 16:25:07 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Optional, TYPE_CHECKING
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
import tcod.event
|
|
|
|
|
2022-01-10 13:47:10 -05:00
|
|
|
from actions import Action, BumpAction, EscapeAction, WaitAction
|
2022-01-06 11:09:43 -05:00
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
|
|
|
|
2022-01-10 13:47:10 -05:00
|
|
|
MOVE_KEYS = {
|
|
|
|
# Arrow keys
|
|
|
|
tcod.event.K_UP: (0, -1),
|
|
|
|
tcod.event.K_DOWN: (0, 1),
|
|
|
|
tcod.event.K_LEFT: (-1, 0),
|
|
|
|
tcod.event.K_RIGHT: (1, 0),
|
|
|
|
tcod.event.K_HOME: (-1, -1),
|
|
|
|
tcod.event.K_END: (-1, 1),
|
|
|
|
tcod.event.K_PAGEUP: (1, -1),
|
|
|
|
tcod.event.K_PAGEDOWN: (1, 1),
|
|
|
|
|
|
|
|
# Numpad keys
|
|
|
|
tcod.event.K_KP_1: (-1, 1),
|
|
|
|
tcod.event.K_KP_2: (0, 1),
|
|
|
|
tcod.event.K_KP_3: (1, 1),
|
|
|
|
tcod.event.K_KP_4: (-1, 0),
|
|
|
|
tcod.event.K_KP_6: (1, 0),
|
|
|
|
tcod.event.K_KP_7: (-1, -1),
|
|
|
|
tcod.event.K_KP_8: (0, -1),
|
|
|
|
tcod.event.K_KP_9: (1, -1),
|
|
|
|
|
|
|
|
# Vi keys
|
|
|
|
tcod.event.K_h: (-1, 0),
|
|
|
|
tcod.event.K_j: (0, 1),
|
|
|
|
tcod.event.K_k: (0, -1),
|
|
|
|
tcod.event.K_l: (1, 0),
|
|
|
|
tcod.event.K_y: (-1, -1),
|
|
|
|
tcod.event.K_u: (1, -1),
|
|
|
|
tcod.event.K_b: (-1, 1),
|
|
|
|
tcod.event.K_n: (1, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
WAIT_KEYS = {
|
|
|
|
tcod.event.K_PERIOD,
|
|
|
|
tcod.event.K_KP_5,
|
|
|
|
tcod.event.K_CLEAR,
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
class EventHandler(tcod.event.EventDispatch[Action]):
|
2022-01-07 16:25:07 -05:00
|
|
|
def __init__(self, engine: Engine):
|
|
|
|
self.engine = engine
|
|
|
|
|
|
|
|
def handle_events(self) -> None:
|
|
|
|
for event in tcod.event.wait():
|
|
|
|
action = self.dispatch(event)
|
|
|
|
|
|
|
|
if action is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
action.perform()
|
|
|
|
|
|
|
|
self.engine.handle_enemy_turns()
|
|
|
|
self.engine.update_fov()
|
|
|
|
|
2022-01-06 11:09:43 -05:00
|
|
|
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
|
|
|
|
action: Optional[Action] = None
|
|
|
|
|
|
|
|
key = event.sym
|
|
|
|
|
2022-01-07 16:25:07 -05:00
|
|
|
player = self.engine.player
|
|
|
|
|
2022-01-10 13:47:10 -05:00
|
|
|
if key in MOVE_KEYS:
|
|
|
|
dx, dy = MOVE_KEYS[key]
|
|
|
|
action = BumpAction(player, dx, dy)
|
|
|
|
elif key in WAIT_KEYS:
|
|
|
|
action = WaitAction(player)
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
elif key == tcod.event.K_ESCAPE:
|
2022-01-07 16:25:07 -05:00
|
|
|
action = EscapeAction(player)
|
2022-01-06 11:09:43 -05:00
|
|
|
|
|
|
|
# No valid key was pressed
|
|
|
|
return action
|