simulation.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # If we return around_count, when around_count is 0,
  16. # cycle manager will consider as False
  17. return True
  18. def action(self, data):
  19. new_empty = Empty(
  20. simulation=self.simulation,
  21. position=self.subject.position,
  22. )
  23. self.simulation.subjects.remove(self.subject)
  24. self.simulation.subjects.append(new_empty)
  25. class CellBornBehaviour(Behaviour):
  26. use = [CellProximityMechanism]
  27. def run(self, data):
  28. around_count = len(data[CellProximityMechanism])
  29. if around_count == 3:
  30. return 3
  31. return False
  32. def action(self, data):
  33. new_cell = Cell(
  34. simulation=self.simulation,
  35. position=self.subject.position,
  36. )
  37. self.simulation.subjects.remove(self.subject)
  38. self.simulation.subjects.append(new_cell)
  39. class Cell(XYZSubjectMixin, Subject):
  40. collections = Subject.collections[:]
  41. collections.extend([COLLECTION_CELL])
  42. behaviours_classes = [CellDieBehaviour]
  43. class Empty(XYZSubjectMixin, Subject):
  44. """Represent empty position where cell can spawn"""
  45. behaviours_classes = [CellBornBehaviour]