subjects.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # coding: utf-8
  2. from synergine2.simulation import Subjects
  3. from synergine2.simulation import Subject
  4. from synergine2_xyz.xyz import XYZSubjectMixin
  5. from synergine2_xyz.xyz import PositionNotPossible
  6. class XYZSubjects(Subjects):
  7. def __init__(self, *args, **kwargs):
  8. super().__init__(*args, **kwargs)
  9. # TODO: accept multiple subjects as same position
  10. # TODO: init xyz with given list
  11. self.xyz = {}
  12. def have_to_check_position_is_possible(self) -> bool:
  13. return True
  14. def remove(self, value: XYZSubjectMixin):
  15. super().remove(value)
  16. try:
  17. self.xyz.get(value.position, []).remove(value)
  18. if not self.xyz[value.position]:
  19. del self.xyz[value.position]
  20. except ValueError:
  21. pass
  22. def append(self, p_object: XYZSubjectMixin):
  23. super().append(p_object)
  24. if self.have_to_check_position_is_possible() \
  25. and not self.simulation.is_possible_subject_position(p_object, p_object.position):
  26. raise PositionNotPossible('Position {} for {} is not possible'.format(
  27. str(p_object.position),
  28. str(p_object),
  29. ))
  30. self.xyz.setdefault(p_object.position, []).append(p_object)
  31. class XYZSubject(XYZSubjectMixin, Subject):
  32. pass