BaseBug.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from intelligine.core.exceptions import BodyPartAlreadyExist
  2. from intelligine.synergy.object.Transportable import Transportable
  3. from intelligine.cst import ALIVE, ATTACKABLE, COL_ALIVE, COLONY
  4. from intelligine.simulation.object.brain.Brain import Brain
  5. class BaseBug(Transportable):
  6. _body_parts = {}
  7. def __init__(self, collection, context):
  8. super().__init__(collection, context)
  9. context.metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
  10. context.metas.collections.add(self.get_id(), COL_ALIVE)
  11. context.metas.value.set(COLONY, self.get_id(), collection.get_id())
  12. self._life_points = 10
  13. self._movements_count = -1
  14. self._brain = self._get_brain_instance()
  15. self._parts = {}
  16. self._init_parts()
  17. def die(self):
  18. self._remove_state(ALIVE)
  19. self._remove_state(ATTACKABLE)
  20. self._remove_col(COL_ALIVE)
  21. def _init_parts(self):
  22. for body_part_name in self._body_parts:
  23. self._set_body_part(body_part_name, self._body_parts[body_part_name](self, self._context))
  24. def _set_body_part(self, name, body_part, replace=False):
  25. if name in self._parts and not replace:
  26. raise BodyPartAlreadyExist()
  27. self._parts[name] = body_part
  28. def get_body_part(self, name):
  29. return self._parts[name]
  30. def hurted(self, points):
  31. self._life_points -= points
  32. def get_life_points(self):
  33. return self._life_points
  34. def set_position(self, point):
  35. super().set_position(point)
  36. self._movements_count += 1
  37. def get_movements_count(self):
  38. return self._movements_count
  39. def _get_brain_instance(self):
  40. return Brain(self._context, self)
  41. def get_brain(self):
  42. return self._brain