Ant.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from intelligine.core.exceptions import MoleculeException
  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, MODE_EXPLO, MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
  5. COL_TRANSPORTER_CARRYING, MODE_NURSE, MODE_HOME
  6. from intelligine.synergy.object.Food import Food
  7. from intelligine.simulation.object.molecule.MovementMoleculeGland import MovementMoleculeGland
  8. from intelligine.simulation.object.brain.AntBrain import AntBrain
  9. class Ant(Bug):
  10. _body_parts = {
  11. BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
  12. }
  13. _brain_class = AntBrain
  14. def __init__(self, collection, context):
  15. super().__init__(collection, context)
  16. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  17. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  18. COL_TRANSPORTER_NOT_CARRYING,
  19. COL_FIGHTER])
  20. self._carried = None
  21. # TODO: Comme pour lorsque une action put est faite, lancer un algo de choix de la mission a suivre.
  22. self._brain.switch_to_mode(MODE_EXPLO)
  23. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  24. def die(self):
  25. super().die()
  26. self._remove_state(TRANSPORTER)
  27. self._remove_state(ATTACKER)
  28. self._remove_col(COL_TRANSPORTER)
  29. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  30. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  31. self._remove_col(COL_FIGHTER)
  32. def get_movement_molecule_gland(self):
  33. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  34. def put_carry(self, obj, position=None):
  35. if position is None:
  36. position = self._get_position()
  37. self._carried = None
  38. obj.set_position(position)
  39. obj.set_is_carried(False, self)
  40. self._context.metas.states.remove(self.get_id(), CARRYING)
  41. self._add_col(COL_TRANSPORTER_NOT_CARRYING)
  42. self._remove_col(COL_TRANSPORTER_CARRYING)
  43. def get_carried(self):
  44. return self._carried
  45. def carry(self, obj):
  46. self._carried = obj
  47. self._context.metas.states.add(self.get_id(), CARRYING)
  48. self._add_col(COL_TRANSPORTER_CARRYING)
  49. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  50. obj.set_is_carried(True, self)
  51. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  52. if isinstance(obj, Food):
  53. self.get_brain().switch_to_mode(MODE_GOHOME)
  54. self.get_movement_molecule_gland().appose()
  55. def is_carrying(self):
  56. if self._carried:
  57. return True
  58. return False
  59. def set_position(self, position):
  60. if self._position is not None and position != self._position:
  61. self._brain.host_moved()
  62. super().set_position(position)
  63. if self.is_carrying():
  64. self._carried.set_position(position)
  65. def initialize(self):
  66. super().initialize()
  67. if self.get_movement_molecule_gland().is_enabled():
  68. try:
  69. self.get_movement_molecule_gland().appose()
  70. except MoleculeException:
  71. pass
  72. def get_colony(self):
  73. return self.get_collection()