TakeableEvent.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from intelligine.core.exceptions import NearNothingFound
  2. from intelligine.shorcut.brain import get_brain_part
  3. from synergine.core.exceptions import NotConcernedEvent
  4. from intelligine.synergy.event.src.NearEvent import NearEvent
  5. from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
  6. from intelligine.cst import TRANSPORTABLE, CANT_CARRY_STILL, COL_TRANSPORTER_NOT_CARRYING, BRAIN_SCHEMA, BRAIN_PART_TAKE
  7. class TakeableEvent(NearEvent):
  8. _mechanism = AroundMechanism
  9. PARAM_TAKE = 'take'
  10. _concern = COL_TRANSPORTER_NOT_CARRYING
  11. _near_name = 'objects_ids_transportable'
  12. _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
  13. def _prepare(self, object_id, context, parameters={}):
  14. if not self._can_carry(object_id, context):
  15. raise NotConcernedEvent()
  16. try:
  17. self.map(context, parameters, stop_at_first=True)
  18. except NearNothingFound:
  19. raise NotConcernedEvent()
  20. if not self._object_can_take(object_id, context, parameters[self._near_name][0]):
  21. raise NotConcernedEvent()
  22. parameters[self.PARAM_TAKE] = parameters[self._near_name][0]
  23. return parameters
  24. @staticmethod
  25. def _can_carry(object_id, context):
  26. return not context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True)
  27. @classmethod
  28. def _object_can_take(cls, object_id, context, object_to_take_id):
  29. object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_TAKE)
  30. return object_take_brain_part.can_take(context, object_id, object_to_take_id)