BaseBug.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, CARRIED
  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. self._is_carried = False
  18. def die(self):
  19. self._remove_state(ALIVE)
  20. self._remove_state(ATTACKABLE)
  21. self._remove_col(COL_ALIVE)
  22. def _init_parts(self):
  23. for body_part_name in self._body_parts:
  24. self._set_body_part(body_part_name, self._body_parts[body_part_name](self, self._context))
  25. def _set_body_part(self, name, body_part, replace=False):
  26. if name in self._parts and not replace:
  27. raise BodyPartAlreadyExist()
  28. self._parts[name] = body_part
  29. def get_body_part(self, name):
  30. return self._parts[name]
  31. def hurted(self, points):
  32. self._life_points -= points
  33. def get_life_points(self):
  34. return self._life_points
  35. def set_position(self, point):
  36. super().set_position(point)
  37. self._movements_count += 1
  38. def get_movements_count(self):
  39. return self._movements_count
  40. def _get_brain_instance(self):
  41. return Brain(self._context, self)
  42. def get_brain(self):
  43. return self._brain
  44. def set_is_carried(self, is_carried, by_obj):
  45. self._is_carried = bool(is_carried)
  46. if self._is_carried:
  47. self._context.metas.value.set(CARRIED, subject=by_obj.get_id(), value=self.get_id())
  48. else:
  49. self._context.metas.value.unset(CARRIED, subject=by_obj.get_id())