simulation.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # coding: utf-8
  2. import typing
  3. from synergine2.config import Config
  4. from synergine2.simulation import SubjectMechanism, Simulation, Subject
  5. from synergine2_xyz.simulation import XYZSimulation
  6. from synergine2_xyz.subjects import XYZSubject
  7. class VisibleMechanism(SubjectMechanism):
  8. from_collection = None
  9. def __init__(
  10. self,
  11. config: Config,
  12. simulation: Simulation,
  13. subject: Subject,
  14. ) -> None:
  15. super().__init__(config, simulation, subject)
  16. self.simulation = typing.cast(XYZSimulation, self.simulation)
  17. self.subject = typing.cast(XYZSubject, self.subject)
  18. def reduce_subjects(self, subjects: typing.List[XYZSubject]) -> typing.Iterator[XYZSubject]:
  19. return subjects
  20. def is_visible(self, observed: XYZSubject) -> bool:
  21. return self.simulation.physics.subject_see_subject(self.subject, observed)
  22. def _get_subject_iterable_from_collection(self, collection_name: str) -> typing.Iterator[XYZSubject]:
  23. for subject_id in self.simulation.collections[collection_name]:
  24. yield self.simulation.subjects.index[subject_id]
  25. def run(self) -> dict:
  26. if self.from_collection is None:
  27. subjects = self.simulation.subjects
  28. else:
  29. subjects = self._get_subject_iterable_from_collection(self.from_collection)
  30. subjects_to_parse = self.reduce_subjects(subjects)
  31. subjects_visible = list(filter(self.is_visible, subjects_to_parse))
  32. return {
  33. 'visible_subjects': subjects_visible,
  34. }