simulation.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from synergine2.simulation import Subject
  2. from synergine2.simulation import Behaviour
  3. from synergine2.xyz import ProximityMechanism
  4. from synergine2.xyz import XYZSubjectMixin
  5. COLLECTION_CELL = 'COLLECTION_CELL' # Collections of Cell type
  6. class CellProximityMechanism(ProximityMechanism):
  7. distance = 1.41 # distance when on angle
  8. feel_collections = [COLLECTION_CELL]
  9. class CellDieBehaviour(Behaviour):
  10. use = [CellProximityMechanism]
  11. def run(self, data):
  12. around_count = len(data[CellProximityMechanism])
  13. if around_count in [2, 3]:
  14. return False
  15. return True # If we return around_count, when around_count is 0
  16. def action(self, data):
  17. new_empty = Empty(
  18. simulation=self.simulation,
  19. position=self.subject.position,
  20. )
  21. self.simulation.subjects.remove(self.subject)
  22. self.simulation.subjects.append(new_empty)
  23. class CellBornBehaviour(Behaviour):
  24. use = [CellProximityMechanism]
  25. def run(self, data):
  26. around_count = len(data[CellProximityMechanism])
  27. if around_count == 3:
  28. return 3
  29. return False
  30. def action(self, data):
  31. new_cell = Cell(
  32. simulation=self.simulation,
  33. position=self.subject.position,
  34. )
  35. self.simulation.subjects.remove(self.subject)
  36. self.simulation.subjects.append(new_cell)
  37. class Cell(XYZSubjectMixin, Subject):
  38. collections = Subject.collections[:]
  39. collections.extend([COLLECTION_CELL])
  40. behaviours_classes = [CellDieBehaviour]
  41. class Empty(XYZSubjectMixin, Subject):
  42. """Represent empty position where cell can spawn"""
  43. behaviours_classes = [CellBornBehaviour]