Ant.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
  5. COL_TRANSPORTER_CARRYING, MOVE_MODE_NURSE, MOVE_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. def __init__(self, collection, context):
  14. super().__init__(collection, context)
  15. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  16. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  17. COL_TRANSPORTER_NOT_CARRYING,
  18. COL_FIGHTER])
  19. self._carried = None
  20. self._brain.switch_to_mode(MOVE_MODE_HOME)
  21. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  22. def die(self):
  23. super().die()
  24. self._remove_state(TRANSPORTER)
  25. self._remove_state(ATTACKER)
  26. self._remove_col(COL_TRANSPORTER)
  27. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  28. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  29. self._remove_col(COL_FIGHTER)
  30. def _get_brain_instance(self):
  31. return AntBrain(self._context, self)
  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(MOVE_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()