simulation.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. return new_empty
  26. class CellBornBehaviour(Behaviour):
  27. use = [CellProximityMechanism]
  28. def run(self, data):
  29. around_count = len(data[CellProximityMechanism])
  30. if around_count == 3:
  31. return 3
  32. return False
  33. def action(self, data):
  34. new_cell = Cell(
  35. simulation=self.simulation,
  36. position=self.subject.position,
  37. )
  38. self.simulation.subjects.remove(self.subject)
  39. self.simulation.subjects.append(new_cell)
  40. return new_cell
  41. class Cell(XYZSubjectMixin, Subject):
  42. collections = Subject.collections[:]
  43. collections.extend([COLLECTION_CELL])
  44. behaviours_classes = [CellDieBehaviour]
  45. class Empty(XYZSubjectMixin, Subject):
  46. """Represent empty position where cell can spawn"""
  47. behaviours_classes = [CellBornBehaviour]