12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- from synergine2.simulation import Subject
- from synergine2.simulation import Event
- from synergine2.simulation import Behaviour
- from synergine2.xyz import ProximityMechanism
- from synergine2.xyz import XYZSubjectMixin
-
- COLLECTION_CELL = 'COLLECTION_CELL'
-
-
- class CellDieEvent(Event):
- def __init__(self, subject_id, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.subject_id = subject_id
-
-
- class CellBornEvent(Event):
- def __init__(self, subject_id, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.subject_id = subject_id
-
-
- class CellProximityMechanism(ProximityMechanism):
- distance = 1.41
- feel_collections = [COLLECTION_CELL]
-
-
- class CellDieBehaviour(Behaviour):
- use = [CellProximityMechanism]
-
- def run(self, data):
- around_count = len(data[CellProximityMechanism])
- if around_count in [2, 3]:
- return False
-
-
- return True
-
- def action(self, data):
- new_empty = Empty(
- simulation=self.simulation,
- position=self.subject.position,
- )
- self.simulation.subjects.remove(self.subject)
- self.simulation.subjects.append(new_empty)
- return [CellDieEvent(self.subject.id)]
-
-
- class CellBornBehaviour(Behaviour):
- use = [CellProximityMechanism]
-
- def run(self, data):
- around_count = len(data[CellProximityMechanism])
- if around_count == 3:
- return 3
- return False
-
- def action(self, data):
- new_cell = Cell(
- simulation=self.simulation,
- position=self.subject.position,
- )
- self.simulation.subjects.remove(self.subject)
- self.simulation.subjects.append(new_cell)
- return [CellBornEvent(new_cell.id)]
-
-
- class Cell(XYZSubjectMixin, Subject):
- collections = Subject.collections[:]
- collections.extend([COLLECTION_CELL])
- behaviours_classes = [CellDieBehaviour]
-
-
- class Empty(XYZSubjectMixin, Subject):
- """Represent empty position where cell can spawn"""
- behaviours_classes = [CellBornBehaviour]
|