2022-01-02 17:50:33 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional, TYPE_CHECKING
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
import tcod.event
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
from actions import Action, EscapeAction, BumpAction
|
2022-01-02 17:50:33 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from engine import Engine
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EventHandler(tcod.event.EventDispatch[Action]):
|
2022-01-02 17:50:33 +00: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-02 15:23:25 +00:00
|
|
|
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
|
|
|
|
raise SystemExit()
|
|
|
|
|
|
|
|
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
|
|
|
|
action: Optional[Action] = None
|
|
|
|
|
|
|
|
key = event.sym
|
2022-01-02 17:50:33 +00:00
|
|
|
player = self.engine.player
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
if key == tcod.event.K_UP:
|
2022-01-02 17:50:33 +00:00
|
|
|
action = BumpAction(player, dx=0, dy=-1)
|
2022-01-02 15:23:25 +00:00
|
|
|
elif key == tcod.event.K_DOWN:
|
2022-01-02 17:50:33 +00:00
|
|
|
action = BumpAction(player, dx=0, dy=1)
|
2022-01-02 15:23:25 +00:00
|
|
|
elif key == tcod.event.K_LEFT:
|
2022-01-02 17:50:33 +00:00
|
|
|
action = BumpAction(player, dx=-1, dy=0)
|
2022-01-02 15:23:25 +00:00
|
|
|
elif key == tcod.event.K_RIGHT:
|
2022-01-02 17:50:33 +00:00
|
|
|
action = BumpAction(player, dx=1, dy=0)
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
elif key == tcod.event.K_ESCAPE:
|
2022-01-02 17:50:33 +00:00
|
|
|
action = EscapeAction(player)
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
return action
|