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-07 12:25:46 -07:00
|
|
|
import logging
|
|
|
|
import random
|
|
|
|
from typing import MutableSet, 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-07 22:34:43 -07:00
|
|
|
from .actions import Action, ActionResult, ExitAction, RegenerateRoomsAction, BumpAction, WaitAction
|
2022-05-03 18:21:24 -07:00
|
|
|
from .geometry import Direction
|
2022-05-07 12:25:46 -07:00
|
|
|
from .object import Entity
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .engine import Engine
|
|
|
|
|
|
|
|
LOG = logging.getLogger('events')
|
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'''
|
|
|
|
|
2022-05-07 12:25:46 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
if not action:
|
|
|
|
return
|
|
|
|
|
|
|
|
result = self.perform_action_until_done(action)
|
|
|
|
|
|
|
|
# Action failed, so do nothing further.
|
|
|
|
if not result.success and result.done:
|
|
|
|
return
|
|
|
|
|
|
|
|
directions = list(Direction.all())
|
|
|
|
|
|
|
|
hero = self.engine.hero
|
|
|
|
moved_entities: MutableSet[Entity] = {self.engine.hero}
|
|
|
|
|
|
|
|
for ent in self.engine.entities:
|
|
|
|
if ent == hero:
|
|
|
|
continue
|
|
|
|
|
|
|
|
while True:
|
|
|
|
new_position = ent.position + random.choice(directions)
|
|
|
|
overlaps_with_previously_moved_entity = any(new_position == moved_ent.position for moved_ent in moved_entities)
|
|
|
|
tile_is_walkable = self.engine.map.tile_is_walkable(new_position)
|
|
|
|
if not overlaps_with_previously_moved_entity and tile_is_walkable:
|
|
|
|
ent.position = new_position
|
|
|
|
moved_entities.add(ent)
|
|
|
|
break
|
|
|
|
|
|
|
|
self.engine.update_field_of_view()
|
|
|
|
|
|
|
|
def perform_action_until_done(self, action: Action) -> ActionResult:
|
|
|
|
'''Perform the given action and any alternate follow-up actions until the action chain is done.'''
|
|
|
|
result = action.perform(self.engine)
|
|
|
|
LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate)
|
|
|
|
|
|
|
|
while not result.done:
|
|
|
|
alternate = result.alternate
|
|
|
|
assert alternate is not None, f'Action {result.action} incomplete but no alternate action given'
|
|
|
|
|
|
|
|
result = alternate.perform(self.engine)
|
|
|
|
LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate)
|
|
|
|
|
|
|
|
if result.success:
|
|
|
|
LOG.info('Action succeded!')
|
|
|
|
break
|
2022-05-07 22:34:43 -07:00
|
|
|
|
|
|
|
if result.done:
|
|
|
|
LOG.info('Action failed!')
|
|
|
|
break
|
2022-05-07 12:25:46 -07:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2022-04-30 21:59:01 -07:00
|
|
|
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
|
|
|
|
return ExitAction()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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 11:57:08 -07:00
|
|
|
case tcod.event.KeySym.SPACE:
|
|
|
|
action = RegenerateRoomsAction()
|
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
|