simulation.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # coding: utf-8
  2. from sandbox.engulf.behaviour import GrassSpawnBehaviour
  3. from sandbox.engulf.subject import Cell, Grass
  4. from synergine2.xyz import XYZSubjects, XYZSubjectMixin, XYZSimulation
  5. __author__ = 'bux'
  6. class EngulfSubjects(XYZSubjects):
  7. def __init__(self, *args, **kwargs):
  8. super().__init__(*args, **kwargs)
  9. # TODO: accept multiple subjects as same position
  10. # TODO: init xyz with given list
  11. self.cell_xyz = {}
  12. self.grass_xyz = {}
  13. def remove(self, value: XYZSubjectMixin):
  14. super().remove(value)
  15. if isinstance(value, Cell):
  16. try:
  17. self.cell_xyz.get(value.position, []).remove(value)
  18. if not self.cell_xyz[value.position]:
  19. del self.cell_xyz[value.position]
  20. except ValueError:
  21. pass
  22. if isinstance(value, Grass):
  23. del self.grass_xyz[value.position]
  24. def append(self, p_object: XYZSubjectMixin):
  25. super().append(p_object)
  26. if isinstance(p_object, Cell):
  27. self.cell_xyz.setdefault(p_object.position, []).append(p_object)
  28. if isinstance(p_object, Grass):
  29. self.grass_xyz.setdefault(p_object.position, []).append(p_object)
  30. class Engulf(XYZSimulation):
  31. behaviours_classes = [
  32. GrassSpawnBehaviour,
  33. ]
  34. def is_possible_position(self, position: tuple) -> bool:
  35. top_left = (-35, -35, 0)
  36. bottom_right = (35, 35, 0)
  37. pos_x = position[0]
  38. pos_y = position[1]
  39. if pos_x < top_left[0] or pos_x > bottom_right[0]:
  40. return False
  41. if pos_y < top_left[1] or pos_y > bottom_right[1]:
  42. return False
  43. return True