Browse Source

Transport of eggs

Bastien Sevajol 9 years ago
parent
commit
481f00010b

+ 5 - 1
intelligine/cst.py View File

@@ -1,9 +1,13 @@
1 1
 from synergine.lib.eint import IncrementedNamedInt
2 2
 
3 3
 ALIVE = IncrementedNamedInt.get('intelligine.alive')
4
+WALKER = IncrementedNamedInt.get('intelligine.walker')
4 5
 IMPENETRABLE = IncrementedNamedInt.get('intelligine.impenetrable')
5 6
 PREVIOUS_DIRECTION = IncrementedNamedInt.get('intelligine.previous_direction')
6 7
 BLOCKED_SINCE = IncrementedNamedInt.get('intelligine.blocked_since')
7 8
 ATTACKABLE = IncrementedNamedInt.get('intelligine.attackable')
8 9
 ATTACKER = IncrementedNamedInt.get('intelligine.attacker')
9
-COLONY = IncrementedNamedInt.get('intelligine.colony')
10
+COLONY = IncrementedNamedInt.get('intelligine.colony')
11
+TRANSPORTABLE = IncrementedNamedInt.get('intelligine.transportable')
12
+TRANSPORTER = IncrementedNamedInt.get('intelligine.transporter')
13
+CARRYING = IncrementedNamedInt.get('intelligine.carrying')

BIN
intelligine/display/pygame/image/egg.png View File


+ 5 - 0
intelligine/display/pygame/visualisation.py View File

@@ -6,6 +6,7 @@ from intelligine.sandbox.colored.BlueAnt import BlueAnt
6 6
 from intelligine.sandbox.colored.RedAnt import RedAnt
7 7
 from intelligine.sandbox.colored.GreenAnt import GreenAnt
8 8
 from intelligine.synergy.object.Rock import Rock
9
+from intelligine.synergy.object.ant.Egg import Egg
9 10
 from os import getcwd
10 11
 from synergine.metas import metas
11 12
 from intelligine.cst import PREVIOUS_DIRECTION
@@ -22,6 +23,7 @@ dead_green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame
22 23
 dead_blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_blue_ant.png')
23 24
 bug = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
24 25
 rock = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/rock.png')
26
+egg = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg.png')
25 27
 
26 28
 directions_ant = DirectionnedImage(ant)
27 29
 directions_red_ant = DirectionnedImage(red_ant)
@@ -87,6 +89,9 @@ visualisation = {
87 89
             'default': bug,
88 90
             'callbacks': [bug_direction]
89 91
         },
92
+        Egg: {
93
+            'default': egg
94
+        },
90 95
         Rock: {
91 96
             'default': rock
92 97
         }

+ 19 - 1
intelligine/sandbox/colored/RedColonyConfiguration.py View File

@@ -1,8 +1,26 @@
1 1
 from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
2 2
 from intelligine.sandbox.colored.RedAnt import RedAnt
3
+from intelligine.synergy.object.ant.Egg import Egg
4
+from synergine.metas import metas
5
+from intelligine.cst import COLONY
3 6
 
4 7
 
5 8
 class RedColonyConfiguration(ColonyConfiguration):
6 9
 
7 10
     _start_position = (0, 20, 70)
8
-    _ant_class = RedAnt
11
+    _ant_class = RedAnt
12
+
13
+    def get_start_objects(self, collection):
14
+        objects = super().get_start_objects(collection)
15
+
16
+        for x in range(50):
17
+          for y in range(1, 50):
18
+            if x % 2 == 0 and y % 2 == 0:
19
+              egg = Egg()
20
+              egg.set_position((0, 1+x, 50+y))
21
+              # TODO: Ce COLONY doit devenir un truc automatise au niveau de la collection (qd get_object)
22
+              metas.value.set(COLONY, egg.get_id(), collection.get_id())
23
+              objects.append(egg)
24
+
25
+        return objects
26
+

+ 2 - 1
intelligine/synergy/Colony.py View File

@@ -1,6 +1,7 @@
1 1
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
2 2
 from intelligine.synergy.event.move.MoveAction import MoveAction
3 3
 from intelligine.synergy.event.attack.NearAttackableAction import NearAttackableAction
4
+from intelligine.synergy.event.transport.TakeableAction import TakeableAction
4 5
 
5 6
 
6 7
 class Colony(SynergyCollection):
@@ -8,4 +9,4 @@ class Colony(SynergyCollection):
8 9
 
9 10
     def __init__(self, configuration):
10 11
         super().__init__(configuration)
11
-        self._actions = [MoveAction, NearAttackableAction]
12
+        self._actions = [MoveAction, NearAttackableAction, TakeableAction]

+ 1 - 1
intelligine/synergy/event/attack/NearAttackableEvent.py View File

@@ -15,7 +15,7 @@ class NearAttackableEvent(Event):
15 15
         self._mechanism = ArroundMechanism
16 16
 
17 17
     def _object_match(self, object_id, context, parameters={}):
18
-      # TODO: nettoyer
18
+        # TODO: nettoyer
19 19
         obj_colony_id = context.metas.value.get(COLONY, object_id)
20 20
         for obj_near_id in parameters['objects_ids_near']:
21 21
             if context.metas.list.have(Simulation.STATE, obj_near_id, ATTACKABLE):

+ 3 - 2
intelligine/synergy/event/move/MoveEvent.py View File

@@ -1,13 +1,14 @@
1 1
 from synergine.synergy.event.Event import Event
2 2
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
3 3
 from intelligine.synergy.Simulation import Simulation
4
-from intelligine.cst import ALIVE
4
+from intelligine.cst import ALIVE, WALKER
5 5
 
6 6
 
7 7
 class MoveEvent(Event):
8 8
 
9 9
     def concern(self, object_id, context):
10
-        return context.metas.list.have(Simulation.STATE, object_id, ALIVE)
10
+        return context.metas.list.have(Simulation.STATE, object_id, ALIVE) and \
11
+               context.metas.list.have(Simulation.STATE, object_id, WALKER)
11 12
 
12 13
     def __init__(self, actions):
13 14
         super().__init__(actions)

+ 27 - 0
intelligine/synergy/event/transport/TakeableAction.py View File

@@ -0,0 +1,27 @@
1
+from synergine.synergy.event.Action import Action
2
+from intelligine.synergy.event.transport.TakeableEvent import TakeableEvent
3
+from random import randint
4
+
5
+
6
+class TakeableAction(Action):
7
+
8
+    _listen = TakeableEvent
9
+
10
+    def __init__(self, object_id, parameters):
11
+        super().__init__(object_id, parameters)
12
+
13
+    def prepare(self, context):
14
+        pass
15
+
16
+    def run(self, obj, collection, context, synergy_manager):
17
+        # TODO: TEST
18
+        # TODO: Enlever le state de transportable a ce qui est transporte
19
+        # ?! Comment gerer lorsque deux obj vont vouloir transporter le meme objet ? process !
20
+        obj_id_transportable = self._parameters['objects_ids_transportable'][0]
21
+        obj_transportable = synergy_manager.get_map().get_object(obj_id_transportable)
22
+        try:
23
+            obj_transportable.set_carried_by(obj)
24
+            obj.carry(obj_transportable)
25
+        except ValueError: # Une NotCarriableError serais pus approprie
26
+            # TODO: tmp? Si on as pas pu le porter c'est qu'il vient d'etre porte par une autre.
27
+            pass

+ 27 - 0
intelligine/synergy/event/transport/TakeableEvent.py View File

@@ -0,0 +1,27 @@
1
+from synergine.synergy.event.Event import Event
2
+from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
+from intelligine.synergy.Simulation import Simulation
4
+from intelligine.cst import TRANSPORTABLE, TRANSPORTER, ALIVE, CARRYING
5
+
6
+
7
+class TakeableEvent(Event):
8
+
9
+    def concern(self, object_id, context):
10
+        return context.metas.list.have(Simulation.STATE, object_id, TRANSPORTER) and \
11
+               context.metas.list.have(Simulation.STATE, object_id, ALIVE) and \
12
+               not context.metas.list.have(Simulation.STATE, object_id, CARRYING)
13
+
14
+    def __init__(self, actions):
15
+        super().__init__(actions)
16
+        self._mechanism = ArroundMechanism
17
+
18
+    def _object_match(self, object_id, context, parameters={}):
19
+        # TODO: Nettoyer (refact possible sur ces objets ont tel states, comme dans concern)
20
+        for obj_near_id in parameters['objects_ids_near']:
21
+            if context.metas.list.have(Simulation.STATE, obj_near_id, TRANSPORTABLE):
22
+                if 'objects_ids_transportable' not in parameters:
23
+                    parameters['objects_ids_transportable'] = []
24
+                parameters['objects_ids_transportable'].append(obj_near_id)
25
+        if 'objects_ids_transportable' not in parameters:
26
+            return False
27
+        return True

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

@@ -0,0 +1,29 @@
1
+from xyzworld.SynergyObject import SynergyObject as XyzSynergyObject
2
+from synergine.metas import metas
3
+from intelligine.cst import ALIVE, ATTACKABLE, TRANSPORTABLE
4
+from synergine.synergy.Simulation import Simulation
5
+
6
+
7
+class BaseBug(XyzSynergyObject):
8
+
9
+    def __init__(self):
10
+        super().__init__()
11
+        metas.list.add(Simulation.STATE, self.get_id(), ALIVE)
12
+        metas.list.add(Simulation.STATE, self.get_id(), ATTACKABLE)
13
+        self._life_points = 10
14
+        self._carried_by = []
15
+
16
+    def hurted(self, points):
17
+        self._life_points -= points
18
+
19
+    def get_life_points(self):
20
+        return self._life_points
21
+
22
+    def set_carried_by(self, obj):
23
+        self._carried_by.append(obj)
24
+        metas.list.remove(Simulation.STATE, self.get_id(), TRANSPORTABLE)
25
+
26
+    def is_carried(self):
27
+        if self._carried_by:
28
+            return True
29
+        return False

+ 4 - 13
intelligine/synergy/object/Bug.py View File

@@ -1,20 +1,11 @@
1
-from xyzworld.SynergyObject import SynergyObject as XyzSynergyObject
1
+from intelligine.synergy.object.BaseBug import BaseBug
2 2
 from synergine.metas import metas
3
-from intelligine.cst import ALIVE, ATTACKABLE, ATTACKER
3
+from intelligine.cst import WALKER
4 4
 from synergine.synergy.Simulation import Simulation
5 5
 
6 6
 
7
-class Bug(XyzSynergyObject):
7
+class Bug(BaseBug):
8 8
 
9 9
     def __init__(self):
10 10
         super().__init__()
11
-        metas.list.add(Simulation.STATE, self.get_id(), ALIVE)
12
-        metas.list.add(Simulation.STATE, self.get_id(), ATTACKER)
13
-        metas.list.add(Simulation.STATE, self.get_id(), ATTACKABLE)
14
-        self._life_points = 10
15
-
16
-    def hurted(self, points):
17
-        self._life_points -= points
18
-
19
-    def get_life_points(self):
20
-        return self._life_points
11
+        metas.list.add(Simulation.STATE, self.get_id(), WALKER)

+ 12 - 0
intelligine/synergy/object/Egg.py View File

@@ -0,0 +1,12 @@
1
+from intelligine.synergy.object.BaseBug import BaseBug
2
+from synergine.metas import metas
3
+from intelligine.cst import ALIVE, TRANSPORTABLE
4
+from synergine.synergy.Simulation import Simulation
5
+
6
+
7
+class Egg(BaseBug):
8
+
9
+    def __init__(self):
10
+        super().__init__()
11
+        metas.list.add(Simulation.STATE, self.get_id(), TRANSPORTABLE)
12
+        self._life_points = 1

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

@@ -1,4 +1,29 @@
1 1
 from intelligine.synergy.object.Bug import Bug
2
+from synergine.metas import metas
3
+from intelligine.synergy.Simulation import Simulation
4
+from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER
5
+
2 6
 
3 7
 class Ant(Bug):
4
-  pass
8
+
9
+    def __init__(self):
10
+        super().__init__()
11
+        metas.list.add(Simulation.STATE, self.get_id(), TRANSPORTER)
12
+        metas.list.add(Simulation.STATE, self.get_id(), ATTACKER)
13
+        self._carried = []
14
+
15
+    def carry(self, obj):
16
+        self._carried.append(obj)
17
+        metas.list.add(Simulation.STATE, self.get_id(), CARRYING)
18
+
19
+    def is_carrying(self):
20
+        if len(self._carried):
21
+            return True
22
+        return False
23
+
24
+    # TODO: Est-ce ici que doit etre ce code ?
25
+    def set_position(self, position):
26
+        super().set_position(position)
27
+        if self.is_carrying():
28
+            for obj_carried in self._carried:
29
+                obj_carried.set_position(position)

+ 4 - 0
intelligine/synergy/object/ant/Egg.py View File

@@ -0,0 +1,4 @@
1
+from intelligine.synergy.object.Egg import Egg as BaseEgg
2
+
3
+class Egg(BaseEgg):
4
+    pass