BaseBug.py 2.2KB

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