simulation.py 1.8KB

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