simulation.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from synergine2.simulation import Subject
  2. from synergine2.simulation import Event
  3. from synergine2.simulation import Behaviour
  4. from synergine2.xyz import ProximityMechanism
  5. from synergine2.xyz import XYZSubjectMixin
  6. COLLECTION_CELL = 'COLLECTION_CELL' # Collections of Cell type
  7. class CellDieEvent(Event):
  8. def __init__(self, subject_id, *args, **kwargs):
  9. super().__init__(*args, **kwargs)
  10. self.subject_id = subject_id
  11. class CellBornEvent(Event):
  12. def __init__(self, subject_id, *args, **kwargs):
  13. super().__init__(*args, **kwargs)
  14. self.subject_id = subject_id
  15. class CellProximityMechanism(ProximityMechanism):
  16. distance = 1.41 # distance when on angle
  17. feel_collections = [COLLECTION_CELL]
  18. class CellDieBehaviour(Behaviour):
  19. use = [CellProximityMechanism]
  20. def run(self, data):
  21. around_count = len(data[CellProximityMechanism])
  22. if around_count in [2, 3]:
  23. return False
  24. # If we return around_count, when around_count is 0,
  25. # cycle manager will consider as False
  26. return True
  27. def action(self, data):
  28. new_empty = Empty(
  29. simulation=self.simulation,
  30. position=self.subject.position,
  31. )
  32. self.simulation.subjects.remove(self.subject)
  33. self.simulation.subjects.append(new_empty)
  34. return [CellDieEvent(self.subject.id)]
  35. class CellBornBehaviour(Behaviour):
  36. use = [CellProximityMechanism]
  37. def run(self, data):
  38. around_count = len(data[CellProximityMechanism])
  39. if around_count == 3:
  40. return 3
  41. return False
  42. def action(self, data):
  43. new_cell = Cell(
  44. simulation=self.simulation,
  45. position=self.subject.position,
  46. )
  47. self.simulation.subjects.remove(self.subject)
  48. self.simulation.subjects.append(new_cell)
  49. return [CellBornEvent(new_cell.id)]
  50. class Cell(XYZSubjectMixin, Subject):
  51. collections = Subject.collections[:]
  52. collections.extend([COLLECTION_CELL])
  53. behaviours_classes = [CellDieBehaviour]
  54. class Empty(XYZSubjectMixin, Subject):
  55. """Represent empty position where cell can spawn"""
  56. behaviours_classes = [CellBornBehaviour]