Ant.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from intelligine.core.exceptions import PheromoneException
  2. from intelligine.synergy.object.Bug import Bug
  3. from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
  4. COL_FIGHTER, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, CARRIED, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT
  5. from intelligine.synergy.object.Food import Food
  6. from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
  7. from intelligine.simulation.object.brain.AntBrain import AntBrain
  8. class Ant(Bug):
  9. _body_parts = {
  10. BODY_PART_PHEROMONE_GLAND: MovementPheromoneGland
  11. }
  12. def __init__(self, collection, context):
  13. super().__init__(collection, context)
  14. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  15. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  16. COL_TRANSPORTER_NOT_CARRYING,
  17. COL_FIGHTER])
  18. self._carried = None
  19. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  20. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  21. def die(self):
  22. super().die()
  23. self._remove_state(TRANSPORTER)
  24. self._remove_state(ATTACKER)
  25. self._remove_col(COL_TRANSPORTER)
  26. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  27. self._remove_col(COL_FIGHTER)
  28. def _get_brain_instance(self):
  29. return AntBrain(self._context, self)
  30. def get_movement_pheromone_gland(self):
  31. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  32. def put_carry(self, obj, position=None):
  33. if position is None:
  34. position = self._get_position()
  35. self._carried = None
  36. obj.set_position(position)
  37. self._context.metas.states.remove(self.get_id(), CARRYING)
  38. def get_carried(self):
  39. return self._carried
  40. def carry(self, obj):
  41. self._carried = obj
  42. self._context.metas.states.add(self.get_id(), CARRYING)
  43. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  44. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  45. if isinstance(obj, Food):
  46. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  47. self.get_movement_pheromone_gland().appose()
  48. def is_carrying(self):
  49. if self._carried:
  50. return True
  51. return False
  52. def set_position(self, position):
  53. if self._position is not None and position != self._position:
  54. self._brain.host_moved()
  55. super().set_position(position)
  56. if self.is_carrying():
  57. self._carried.set_position(position)
  58. def initialize(self):
  59. super().initialize()
  60. if self.get_movement_pheromone_gland().is_enabled():
  61. try:
  62. self.get_movement_pheromone_gland().appose()
  63. except PheromoneException:
  64. pass
  65. def get_colony(self):
  66. return self.get_collection()