MoveAction.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from synergine.synergy.event.Action import Action
  2. from socialintengine.synergy.event.move.MoveEvent import MoveEvent
  3. from random import randint
  4. from xyzworld.cst import POSITION, POSITIONS
  5. from socialintengine.cst import IMPENETRABLE
  6. from synergine.synergy.Simulation import Simulation
  7. class MoveAction(Action):
  8. _listen = MoveEvent
  9. def __init__(self, object_id, parameters):
  10. super().__init__(object_id, parameters)
  11. self._move_to = None
  12. def prepare(self, context):
  13. object_point = context.metas.value.get(POSITION, self._object_id)
  14. choosed_direction_point = self._get_random_direction_point(object_point)
  15. if self._direction_point_is_possible(context, choosed_direction_point):
  16. self._move_to = choosed_direction_point
  17. def _get_random_direction_point(self, reference_point):
  18. z, x, y = reference_point
  19. new_z = z
  20. new_x = x + randint(-1, 1)
  21. new_y = y + randint(-1, 1)
  22. return (new_z, new_x, new_y)
  23. def _direction_point_is_possible(self, context, direction_point):
  24. objects_ids_on_this_point = context.metas.list.get(POSITIONS, direction_point, allow_empty=True)
  25. for object_id_on_this_point in objects_ids_on_this_point:
  26. if context.metas.list.have(Simulation.STATE, object_id_on_this_point, IMPENETRABLE):
  27. return False
  28. return True
  29. def run(self, obj, collection, context):
  30. if self._move_to is not None:
  31. obj.add_trace(self._move_to)