BaseBug.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, BRAIN
  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. _brain_class = Brain
  10. def __init__(self, collection, context):
  11. super().__init__(collection, context)
  12. context.metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
  13. context.metas.collections.add(self.get_id(), COL_ALIVE)
  14. context.metas.value.set(COLONY, self.get_id(), collection.get_id())
  15. self._life_points = 10
  16. self._alive = True
  17. self._movements_count = -1
  18. self._brain = self._brain_class(self._context, self)
  19. self._context.metas.value.set(BRAIN, self.__class__, self._brain_class)
  20. self._parts = {}
  21. self._init_parts()
  22. def die(self):
  23. self._set_alive(False)
  24. self._remove_state(ALIVE)
  25. self._remove_state(ATTACKABLE)
  26. self._remove_col(COL_ALIVE)
  27. def _init_parts(self):
  28. for body_part_name in self._body_parts:
  29. self._set_body_part(body_part_name, self._body_parts[body_part_name](self, self._context))
  30. def _set_body_part(self, name, body_part, replace=False):
  31. if name in self._parts and not replace:
  32. raise BodyPartAlreadyExist()
  33. self._parts[name] = body_part
  34. def get_body_part(self, name):
  35. return self._parts[name]
  36. def hurted(self, points):
  37. self._life_points -= points
  38. if self.get_life_points() <= 0 and self.is_alive():
  39. self.die()
  40. Signals.signal(ACTION_DIE).send(obj=self, context=self._context)
  41. def is_alive(self):
  42. return self._alive
  43. def _set_alive(self, alive):
  44. self._alive = bool(alive)
  45. def get_life_points(self):
  46. return self._life_points
  47. def set_position(self, point):
  48. super().set_position(point)
  49. self._movements_count += 1
  50. def get_movements_count(self):
  51. return self._movements_count
  52. def get_brain(self):
  53. return self._brain