Clean up Action processing a little bit

- Sort entities by their Euclidean distance to the hero so actions from entities
  near the hero are processed first
- Fewer local variables for cleaner reading
- Pass hero into the RegerateRoomsAction, which was causing a pylint error
This commit is contained in:
Eryn Wells 2022-05-12 08:53:26 -07:00
parent 70c17b6235
commit 4084d98efd

View file

@ -32,18 +32,25 @@ class EventHandler(tcod.event.EventDispatch[Action]):
'''Handle the given event. Transform that event into an Action via an EventHandler and perform it.''' '''Handle the given event. Transform that event into an Action via an EventHandler and perform it.'''
action = self.dispatch(event) action = self.dispatch(event)
# Unhandled event. Ignore it.
if not action: if not action:
LOG.debug('Unhandled event: %s', event)
return return
result = self.perform_action_until_done(action) result = self.perform_action_until_done(action)
# Action failed, so do nothing further. # Player's action failed, don't proceed with turn.
if not result.success and result.done: if not result.success and result.done:
return return
# Copy the list so we only act on the entities that exist at the start of this turn # Copy the list so we only act on the entities that exist at the start of this turn. Sort it by Euclidean
entities = list(self.engine.entities) # distance to the Hero, so entities closer to the hero act first.
for ent in entities: hero_position = self.engine.hero.position
entities = sorted(
self.engine.entities,
key=lambda e: e.position.euclidean_distance_to(hero_position))
for i, ent in enumerate(entities):
if not isinstance(ent, Actor): if not isinstance(ent, Actor):
continue continue
@ -62,18 +69,15 @@ class EventHandler(tcod.event.EventDispatch[Action]):
LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate) LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate)
while not result.done: while not result.done:
alternate = result.alternate action = result.alternate
assert alternate is not None, f'Action {result.action} incomplete but no alternate action given' assert action is not None, f'Action {result.action} incomplete but no alternate action given'
result = alternate.perform(self.engine) result = action.perform(self.engine)
LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate) LOG.debug('Performed action success=%s done=%s alternate=%s', result.success, result.done, result.alternate)
if result.success: if result.success:
break break
if result.done:
break
return result return result
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]: def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
@ -103,7 +107,7 @@ class EventHandler(tcod.event.EventDispatch[Action]):
case tcod.event.KeySym.y: case tcod.event.KeySym.y:
action = BumpAction(hero, Direction.NorthWest) action = BumpAction(hero, Direction.NorthWest)
case tcod.event.KeySym.SPACE: case tcod.event.KeySym.SPACE:
action = RegenerateRoomsAction() action = RegenerateRoomsAction(hero)
case tcod.event.KeySym.PERIOD: case tcod.event.KeySym.PERIOD:
action = WaitAction(hero) action = WaitAction(hero)