Browse Source

body parts

Bastien Sevajol 9 years ago
parent
commit
7d04ad816a

+ 8 - 0
intelligine/core/exceptions.py View File

@@ -54,6 +54,14 @@ class BrainPartAlreadyExist(BrainException):
54 54
     pass
55 55
 
56 56
 
57
+class BodyException(Exception):
58
+    pass
59
+
60
+
61
+class BodyPartAlreadyExist(BodyException):
62
+    pass
63
+
64
+
57 65
 class DirectionException(Exception):
58 66
     pass
59 67
 

+ 4 - 1
intelligine/cst.py View File

@@ -43,4 +43,7 @@ BRAIN_SCHEMA = IncrementedNamedInt.get('intelligine.brain_schema')
43 43
 BRAIN_PART_MOVE = IncrementedNamedInt.get('intelligine.brain.part.move')
44 44
 BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')
45 45
 BRAIN_PART_PUT = IncrementedNamedInt.get('intelligine.brain.part.put')
46
-BRAIN_PART_ATTACK = IncrementedNamedInt.get('intelligine.brain.part.attack')
46
+BRAIN_PART_ATTACK = IncrementedNamedInt.get('intelligine.brain.part.attack')
47
+
48
+BODY_SCHEMA = IncrementedNamedInt.get('intelligine.body_schema')
49
+BODY_PART_PHEROMONE_GLAND = IncrementedNamedInt.get('intelligine.body.part.pheromone_gland')

+ 2 - 0
intelligine/simulation/object/brain/Brain.py View File

@@ -30,4 +30,6 @@ class Brain():
30 30
         self._schema = {}
31 31
         for part_name in self._parts:
32 32
             self._schema[part_name] = self._parts[part_name].__class__
33
+        # TODO: N'est-ce pas un schema appartenant a la classe ? Ne suffirai t-il pas de stocker ce schema par classe
34
+        # plutôt que par objet ?
33 35
         self._context.metas.value.set(BRAIN_SCHEMA, self._host.get_id(), self._schema)

+ 17 - 0
intelligine/synergy/object/BaseBug.py View File

@@ -1,3 +1,4 @@
1
+from intelligine.core.exceptions import BodyPartAlreadyExist
1 2
 from intelligine.synergy.object.Transportable import Transportable
2 3
 from intelligine.cst import ALIVE, ATTACKABLE, COL_ALIVE
3 4
 from intelligine.simulation.object.brain.Brain import Brain
@@ -5,6 +6,8 @@ from intelligine.simulation.object.brain.Brain import Brain
5 6
 
6 7
 class BaseBug(Transportable):
7 8
 
9
+    _body_parts = {}
10
+
8 11
     def __init__(self, collection, context):
9 12
         super().__init__(collection, context)
10 13
         context.metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
@@ -12,6 +15,20 @@ class BaseBug(Transportable):
12 15
         self._life_points = 10
13 16
         self._movements_count = -1
14 17
         self._brain = self._get_brain_instance()
18
+        self._parts = {}
19
+        self._init_parts()
20
+
21
+    def _init_parts(self):
22
+        for body_part_name in self._body_parts:
23
+            self._set_body_part(body_part_name, self._body_parts[body_part_name](self, self._context))
24
+
25
+    def _set_body_part(self, name, body_part, replace=False):
26
+        if name in self._parts and not replace:
27
+            raise BodyPartAlreadyExist()
28
+        self._parts[name] = body_part
29
+
30
+    def get_body_part(self, name):
31
+        return self._parts[name]
15 32
 
16 33
     def hurted(self, points):
17 34
         self._life_points -= points

+ 7 - 7
intelligine/synergy/object/ant/Ant.py View File

@@ -1,9 +1,7 @@
1 1
 from intelligine.core.exceptions import PheromoneException
2 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
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
7 5
 from intelligine.synergy.object.Food import Food
8 6
 from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
9 7
 from intelligine.simulation.object.brain.AntBrain import AntBrain
@@ -11,6 +9,10 @@ from intelligine.simulation.object.brain.AntBrain import AntBrain
11 9
 
12 10
 class Ant(Bug):
13 11
 
12
+    _body_parts = {
13
+        BODY_PART_PHEROMONE_GLAND: MovementPheromoneGland
14
+    }
15
+
14 16
     def __init__(self, collection, context):
15 17
         super().__init__(collection, context)
16 18
         context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
@@ -18,15 +20,13 @@ class Ant(Bug):
18 20
                                                            COL_TRANSPORTER_NOT_CARRYING,
19 21
                                                            COL_FIGHTER])
20 22
         self._carried = None
21
-        # TODO: Faire un body_part schema pour ces trucs la
22
-        self._movement_pheromone_gland = MovementPheromoneGland(self, self._context)
23 23
         self._brain.switch_to_mode(MOVE_MODE_EXPLO)
24 24
 
25 25
     def _get_brain_instance(self):
26 26
         return AntBrain(self._context, self)
27 27
 
28 28
     def get_movement_pheromone_gland(self):
29
-        return self._movement_pheromone_gland
29
+        return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
30 30
 
31 31
     def put_carry(self, obj, position=None):
32 32
         if position is None: