MoveAction.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from synergine.synergy.event.Action import Action
  2. from intelligine.synergy.event.move.MoveEvent import MoveEvent
  3. from random import randint, choice
  4. from xyzworld.cst import POSITION, POSITIONS
  5. from intelligine.cst import IMPENETRABLE, PREVIOUS_DIRECTION, BLOCKED_SINCE
  6. from intelligine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
  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. choosed_direction_name, choosed_direction_point = self._get_random_direction_point(context, object_point)
  16. if self._direction_point_is_possible(context, choosed_direction_point):
  17. self._move_to_point = choosed_direction_point
  18. self._move_to_direction = choosed_direction_name
  19. def _get_random_direction_point(self, context, reference_point):
  20. z, x, y = reference_point
  21. direction_name = self._get_random_direction_name(context)
  22. directions_modifier = directions_modifiers[direction_name]
  23. new_position = (z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2])
  24. return (direction_name, new_position)
  25. def _get_random_direction_name(self, context):
  26. try:
  27. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
  28. except KeyError:
  29. blocked_since = 0
  30. direction_name = None
  31. if blocked_since <= 3: #TODO: config
  32. try:
  33. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, self._object_id)
  34. directions_list = directions_slighty[previous_direction]
  35. # TODO: TMP tant que 1 niveau (z)
  36. directions_list = [direction for direction in directions_list if direction > 9 and direction < 19]
  37. direction_name = choice(directions_list)
  38. except KeyError:
  39. pass
  40. if not direction_name:
  41. direction_name = randint(directions_same_level[0], directions_same_level[1])
  42. return direction_name
  43. def _direction_point_is_possible(self, context, direction_point):
  44. objects_ids_on_this_point = context.metas.list.get(POSITIONS, direction_point, allow_empty=True)
  45. for object_id_on_this_point in objects_ids_on_this_point:
  46. if context.metas.states.have(object_id_on_this_point, IMPENETRABLE):
  47. return False
  48. return True
  49. def run(self, obj, collection, context, synergy_manager):
  50. if self._move_to_point is not None:
  51. obj.set_position(self._move_to_point)
  52. context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
  53. context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
  54. else:
  55. try:
  56. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
  57. except:
  58. blocked_since = 0
  59. context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)