Ant.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. self._brain.switch_to_mode(MODE_EXPLO)
  22. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  23. def die(self):
  24. super().die()
  25. self._remove_state(TRANSPORTER)
  26. self._remove_state(ATTACKER)
  27. self._remove_col(COL_TRANSPORTER)
  28. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  29. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  30. self._remove_col(COL_FIGHTER)
  31. def get_movement_molecule_gland(self):
  32. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  33. def put_carry(self, obj, position=None):
  34. if position is None:
  35. position = self._get_position()
  36. self._carried = None
  37. obj.set_position(position)
  38. obj.set_is_carried(False, self)
  39. self._context.metas.states.remove(self.get_id(), CARRYING)
  40. self._add_col(COL_TRANSPORTER_NOT_CARRYING)
  41. self._remove_col(COL_TRANSPORTER_CARRYING)
  42. def get_carried(self):
  43. return self._carried
  44. def carry(self, obj):
  45. self._carried = obj
  46. self._context.metas.states.add(self.get_id(), CARRYING)
  47. self._add_col(COL_TRANSPORTER_CARRYING)
  48. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  49. obj.set_is_carried(True, self)
  50. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  51. if isinstance(obj, Food):
  52. self.get_brain().switch_to_mode(MODE_GOHOME)
  53. self.get_movement_molecule_gland().appose()
  54. def is_carrying(self):
  55. if self._carried:
  56. return True
  57. return False
  58. def set_position(self, position):
  59. if self._position is not None and position != self._position:
  60. self._brain.host_moved()
  61. super().set_position(position)
  62. if self.is_carrying():
  63. self._carried.set_position(position)
  64. def initialize(self):
  65. super().initialize()
  66. if self.get_movement_molecule_gland().is_enabled():
  67. try:
  68. self.get_movement_molecule_gland().appose()
  69. except MoleculeException:
  70. pass
  71. def get_colony(self):
  72. return self.get_collection()