MoveAction.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from synergine.synergy.event.Action import Action
  2. from intelligine.synergy.event.move.MoveEvent import MoveEvent
  3. from synergine.synergy.event.exception.ActionAborted import ActionAborted
  4. from xyzworld.cst import POSITION
  5. from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE, BRAIN_PART_MOVE, BRAIN_SCHEMA
  6. from intelligine.synergy.event.move.direction import get_position_with_direction_decal
  7. class MoveAction(Action):
  8. _listen = MoveEvent
  9. def __init__(self, object_id, parameters):
  10. super().__init__(object_id, parameters)
  11. self._move_to_point = None
  12. self._move_to_direction = None
  13. def prepare(self, context):
  14. object_point = context.metas.value.get(POSITION, self._object_id)
  15. direction = self._get_prepared_direction(context)
  16. self._set_prepared_direction(context, object_point, direction)
  17. def _get_prepared_direction(self, context):
  18. object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, self._object_id)
  19. object_move_brain_part = object_brain_schema[BRAIN_PART_MOVE]
  20. return object_move_brain_part.get_direction(context, self._object_id)
  21. def _set_prepared_direction(self, context, object_point, direction):
  22. move_to_point = get_position_with_direction_decal(direction, object_point)
  23. if self._direction_point_is_possible(context, move_to_point):
  24. self._move_to_point = move_to_point
  25. self._move_to_direction = direction
  26. else:
  27. # TODO: mettre self._dont_move = True ?
  28. pass
  29. @staticmethod
  30. def _direction_point_is_possible(context, direction_point):
  31. return context.position_is_penetrable(direction_point)
  32. def run(self, obj, context, synergy_manager):
  33. try:
  34. self._apply_move(obj, context)
  35. except ActionAborted:
  36. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
  37. context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
  38. def _apply_move(self, obj, context):
  39. # TODO: il ne faut pas choisir une direction 14.
  40. if self._move_to_point is None or self._move_to_direction == 14:
  41. raise ActionAborted()
  42. obj.set_position(self._move_to_point)
  43. obj.get_brain().get_part(BRAIN_PART_MOVE).done(obj, context)
  44. # TODO: Ces metas update dans ant ?
  45. context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
  46. context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)