Refactor event handling into EventHandler
Move all the event handling code from Engine to EventHandler. EventHandler has a reference to Engine and can deal with entities from its methods. Refactor Action to take an optional Entity in its initializer. Some actions don't require an Entity, but many do/will.
This commit is contained in:
parent
d75c9faea3
commit
8b3c0137a5
4 changed files with 107 additions and 82 deletions
|
@ -2,40 +2,112 @@
|
|||
|
||||
'''Defines event handling mechanisms.'''
|
||||
|
||||
from typing import Optional
|
||||
import logging
|
||||
import random
|
||||
from typing import MutableSet, Optional, TYPE_CHECKING
|
||||
|
||||
import tcod
|
||||
|
||||
from .actions import Action, ExitAction, RegenerateRoomsAction, BumpAction
|
||||
from .actions import Action, ActionResult, ExitAction, RegenerateRoomsAction, BumpAction
|
||||
from .geometry import Direction
|
||||
from .object import Entity
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .engine import Engine
|
||||
|
||||
LOG = logging.getLogger('events')
|
||||
|
||||
class EventHandler(tcod.event.EventDispatch[Action]):
|
||||
'''Handler of `tcod` events'''
|
||||
|
||||
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
|
||||
else:
|
||||
LOG.info('Action failed!')
|
||||
|
||||
return result
|
||||
|
||||
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
|
||||
|
||||
hero = self.engine.hero
|
||||
|
||||
sym = event.sym
|
||||
match sym:
|
||||
case tcod.event.KeySym.b:
|
||||
action = BumpAction(Direction.SouthWest)
|
||||
action = BumpAction(hero, Direction.SouthWest)
|
||||
case tcod.event.KeySym.h:
|
||||
action = BumpAction(Direction.West)
|
||||
action = BumpAction(hero, Direction.West)
|
||||
case tcod.event.KeySym.j:
|
||||
action = BumpAction(Direction.South)
|
||||
action = BumpAction(hero, Direction.South)
|
||||
case tcod.event.KeySym.k:
|
||||
action = BumpAction(Direction.North)
|
||||
action = BumpAction(hero, Direction.North)
|
||||
case tcod.event.KeySym.l:
|
||||
action = BumpAction(Direction.East)
|
||||
action = BumpAction(hero, Direction.East)
|
||||
case tcod.event.KeySym.n:
|
||||
action = BumpAction(Direction.SouthEast)
|
||||
action = BumpAction(hero, Direction.SouthEast)
|
||||
case tcod.event.KeySym.u:
|
||||
action = BumpAction(Direction.NorthEast)
|
||||
action = BumpAction(hero, Direction.NorthEast)
|
||||
case tcod.event.KeySym.y:
|
||||
action = BumpAction(Direction.NorthWest)
|
||||
action = BumpAction(hero, Direction.NorthWest)
|
||||
case tcod.event.KeySym.SPACE:
|
||||
action = RegenerateRoomsAction()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue