subjects.py 1.2KB

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