MoveEvent.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from intelligine.core.exceptions import UnableToFoundMovement
  2. from intelligine.synergy.event.move.direction import get_position_with_direction_decal
  3. from synergine.core.exceptions import NotConcernedEvent
  4. from intelligine.synergy.event.Event import Event
  5. from synergine.core.simulation.mechanism.Mechanism import Mechanism
  6. from intelligine.cst import COL_WALKER, BRAIN_SCHEMA, BRAIN_PART_MOVE
  7. from xyzworld.cst import POSITION
  8. class MoveEvent(Event):
  9. PARAM_POSITION = 'pos'
  10. PARAM_DIRECTION = 'dir'
  11. concern = COL_WALKER
  12. def __init__(self, actions):
  13. super().__init__(actions)
  14. self._mechanism = Mechanism
  15. def _prepare(self, object_id, context, parameters={}):
  16. try:
  17. direction = self._get_direction(object_id, context)
  18. except UnableToFoundMovement:
  19. raise NotConcernedEvent()
  20. object_point = context.metas.value.get(POSITION, object_id)
  21. move_to_point = get_position_with_direction_decal(direction, object_point)
  22. # TODO: future: c le brain qui calcule ou aller, et donc si c possible
  23. if self._direction_point_is_possible(context, move_to_point):
  24. parameters[self.PARAM_POSITION] = move_to_point
  25. parameters[self.PARAM_DIRECTION] = direction
  26. # TODO: Sinon lever un NotConcernedEvent
  27. return parameters
  28. def _get_direction(self, object_id, context):
  29. object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_id)
  30. object_move_brain_part = object_brain_schema[BRAIN_PART_MOVE]
  31. return object_move_brain_part.get_direction(context, object_id)
  32. @staticmethod
  33. def _direction_point_is_possible(context, direction_point):
  34. return context.position_is_penetrable(direction_point)