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-05-07 11:22:54 -07:00
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
import tcod
|
2022-05-07 11:22:54 -07:00
|
|
|
|
2022-05-12 20:40:06 -07:00
|
|
|
from . import log
|
2022-05-28 08:52:54 -07:00
|
|
|
from .actions.action import Action
|
|
|
|
from .actions.game import ExitAction, RegenerateRoomsAction, BumpAction, WaitAction
|
2022-05-15 16:50:24 -07:00
|
|
|
from .geometry import Direction, Point
|
2022-05-07 12:25:46 -07:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .engine import Engine
|
|
|
|
|
2023-02-10 21:25:00 -08:00
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
class EventHandler(tcod.event.EventDispatch[Action]):
|
2022-05-14 23:43:04 -07:00
|
|
|
'''Abstract event handler class'''
|
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
|
|
|
|
|
2022-05-15 16:50:24 -07:00
|
|
|
def handle_events(self, context: tcod.context.Context):
|
2022-05-07 12:25:46 -07:00
|
|
|
'''Wait for events and handle them.'''
|
|
|
|
for event in tcod.event.wait():
|
2022-05-15 16:50:24 -07:00
|
|
|
context.convert_event(event)
|
2022-05-07 12:25:46 -07:00
|
|
|
self.handle_event(event)
|
|
|
|
|
2023-02-10 22:35:51 -08:00
|
|
|
def handle_event(self, event: tcod.event.Event):
|
2022-05-14 23:43:04 -07:00
|
|
|
'''
|
|
|
|
Handle an event by transforming it into an Action and processing it until it is completed. If the Action
|
|
|
|
succeeds, also process actions from other Entities.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
event : tcod.event.Event
|
|
|
|
The event to handle
|
|
|
|
'''
|
2022-05-07 12:25:46 -07:00
|
|
|
action = self.dispatch(event)
|
|
|
|
|
2022-05-12 08:53:26 -07:00
|
|
|
# Unhandled event. Ignore it.
|
2022-05-07 12:25:46 -07:00
|
|
|
if not action:
|
2022-05-12 20:40:06 -07:00
|
|
|
log.EVENTS.debug('Unhandled event: %s', event)
|
2022-05-07 12:25:46 -07:00
|
|
|
return
|
|
|
|
|
2022-05-15 13:13:12 -07:00
|
|
|
self.engine.process_input_action(action)
|
2022-05-07 12:25:46 -07:00
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
|
2022-05-28 08:52:54 -07:00
|
|
|
return ExitAction()
|
2022-04-30 21:59:01 -07:00
|
|
|
|
2023-02-10 21:25:00 -08:00
|
|
|
|
2022-05-14 23:43:04 -07:00
|
|
|
class MainGameEventHandler(EventHandler):
|
|
|
|
'''
|
|
|
|
Handler of `tcod` events for the main game.
|
|
|
|
|
|
|
|
Receives input from the player and dispatches actions to the game engine to interat with the hero and other objects
|
|
|
|
in the game.
|
|
|
|
'''
|
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
|
|
|
|
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-12 20:40:46 -07:00
|
|
|
case tcod.event.KeySym.ESCAPE:
|
2022-05-28 08:52:54 -07:00
|
|
|
action = ExitAction()
|
2022-05-07 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.SPACE:
|
2023-02-12 19:47:27 -08:00
|
|
|
action = RegenerateRoomsAction()
|
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
|
|
|
|
2022-05-15 16:50:24 -07:00
|
|
|
def ev_mousemotion(self, event: tcod.event.MouseMotion) -> Optional[Action]:
|
|
|
|
mouse_point = Point(event.tile.x, event.tile.y)
|
|
|
|
if not self.engine.map.tile_is_in_bounds(mouse_point):
|
|
|
|
mouse_point = None
|
2023-02-10 22:35:51 -08:00
|
|
|
self.engine.update_mouse_point(mouse_point)
|
2022-05-15 16:50:24 -07:00
|
|
|
|
2023-02-10 21:25:00 -08:00
|
|
|
|
2022-05-14 23:43:04 -07:00
|
|
|
class GameOverEventHandler(EventHandler):
|
|
|
|
'''When the game is over (the hero dies, the player quits, etc), this event handler takes over.'''
|
|
|
|
|
|
|
|
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
|
|
|
|
action: Optional[Action] = None
|
|
|
|
|
|
|
|
sym = event.sym
|
|
|
|
match sym:
|
|
|
|
case tcod.event.KeySym.ESCAPE:
|
2022-05-28 08:52:54 -07:00
|
|
|
action = ExitAction()
|
2022-05-14 23:43:04 -07:00
|
|
|
|
2022-05-15 16:19:03 -07:00
|
|
|
return action
|