NearAttackableEvent.py 1.2KB

1234567891011121314151617181920212223242526272829
  1. from intelligine.core.exceptions import NearNothingFound
  2. from synergine.core.exceptions import NotConcernedEvent
  3. from intelligine.synergy.event.src.NearEvent import NearEvent
  4. from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
  5. from intelligine.cst import ATTACKABLE, COLONY, COL_FIGHTER, BRAIN_PART_ATTACK
  6. class NearAttackableEvent(NearEvent):
  7. _mechanism = AroundMechanism
  8. _concern = COL_FIGHTER
  9. _near_name = 'objects_ids_attackable'
  10. _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, ATTACKABLE)
  11. def _prepare(self, object_id, context, parameters={}):
  12. obj_colony_id = context.metas.value.get(COLONY, object_id)
  13. filter = lambda near_object_id, context: obj_colony_id != context.metas.value.get(COLONY, near_object_id)
  14. try:
  15. self.map(context, parameters, stop_at_first=True, filter=filter)
  16. except NearNothingFound:
  17. raise NotConcernedEvent()
  18. brain_part = self._get_brain_part(context, object_id, BRAIN_PART_ATTACK)
  19. if not brain_part.can_attack(context, object_id, parameters[self._near_name][0]):
  20. raise NotConcernedEvent()
  21. return parameters