simulation.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. except ValueError:
  19. pass
  20. if isinstance(value, Grass):
  21. del self.grass_xyz[value.position]
  22. def append(self, p_object: XYZSubjectMixin):
  23. super().append(p_object)
  24. if isinstance(p_object, Cell):
  25. self.cell_xyz.setdefault(p_object.position, []).append(p_object)
  26. if isinstance(p_object, Grass):
  27. self.grass_xyz.setdefault(p_object.position, []).append(p_object)
  28. class Engulf(XYZSimulation):
  29. behaviours_classes = [
  30. GrassSpawnBehaviour,
  31. ]
  32. def is_possible_position(self, position: tuple) -> bool:
  33. top_left = (-35, -35, 0)
  34. bottom_right = (35, 35, 0)
  35. pos_x = position[0]
  36. pos_y = position[1]
  37. if pos_x < top_left[0] or pos_x > bottom_right[0]:
  38. return False
  39. if pos_y < top_left[1] or pos_y > bottom_right[1]:
  40. return False
  41. return True