Ant.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from intelligine.core.exceptions import PheromoneException
  2. from intelligine.synergy.object.Bug import Bug
  3. from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, \
  4. COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
  5. COL_FIGHTER, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, \
  6. PHEROMON_DIR_EXPLO, CARRIED
  7. from intelligine.synergy.object.Food import Food
  8. from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
  9. from intelligine.simulation.object.brain.AntBrain import AntBrain
  10. class Ant(Bug):
  11. def __init__(self, collection, context):
  12. super().__init__(collection, context)
  13. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  14. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  15. COL_TRANSPORTER_NOT_CARRYING,
  16. COL_FIGHTER])
  17. self._carried = []
  18. # TODO: Faire un body_part schema pour ces trucs la
  19. self._movement_pheromone_gland = MovementPheromoneGland(self, self._context)
  20. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  21. def _get_brain_instance(self):
  22. return AntBrain(self._context, self)
  23. def get_movement_pheromone_gland(self):
  24. return self._movement_pheromone_gland
  25. def put_carry(self, obj, position=None):
  26. if position is None:
  27. position = self._get_position()
  28. self._carried.remove(obj)
  29. obj.set_position(position)
  30. self._context.metas.states.remove(self.get_id(), CARRYING)
  31. def get_carried(self):
  32. # TODO: cas ou plusieurs ?
  33. return self._carried[0]
  34. def carry(self, obj):
  35. self._carried.append(obj)
  36. self._context.metas.states.add(self.get_id(), CARRYING)
  37. # TODO: On gere une liste de carried (mais pas juste la dessous). Ne gerer qu'un objet carried ?
  38. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  39. # TODO: pour le moment hardcode
  40. if isinstance(obj, Food):
  41. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  42. self.get_movement_pheromone_gland().appose()
  43. def is_carrying(self):
  44. if len(self._carried):
  45. return True
  46. return False
  47. # TODO: Est-ce ici que doit etre ce code ?
  48. def set_position(self, position):
  49. if self._position is not None and position != self._position:
  50. self._brain.host_moved()
  51. super().set_position(position)
  52. if self.is_carrying():
  53. for obj_carried in self._carried:
  54. obj_carried.set_position(position)
  55. def initialize(self):
  56. super().initialize()
  57. if self.get_movement_pheromone_gland().is_enabled():
  58. try:
  59. self.get_movement_pheromone_gland().appose()
  60. except PheromoneException:
  61. pass
  62. def get_colony(self):
  63. return self.get_collection()