Browse Source

refact event who inspect near objects

Bastien Sevajol 9 years ago
parent
commit
a7c0ec6347

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

1
-from synergine.synergy.event.Event import Event
1
+from intelligine.synergy.event.src.NearEvent import NearEvent
2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
 from intelligine.cst import ATTACKABLE, COLONY, COL_FIGHTER
3
 from intelligine.cst import ATTACKABLE, COLONY, COL_FIGHTER
4
 
4
 
5
 
5
 
6
-class NearAttackableEvent(Event):
6
+class NearAttackableEvent(NearEvent):
7
 
7
 
8
     concern = COL_FIGHTER
8
     concern = COL_FIGHTER
9
+    _near_name = 'objects_ids_attackable'
10
+    _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, ATTACKABLE)
9
 
11
 
10
     def __init__(self, actions):
12
     def __init__(self, actions):
11
         super().__init__(actions)
13
         super().__init__(actions)
12
         self._mechanism = ArroundMechanism
14
         self._mechanism = ArroundMechanism
13
 
15
 
14
     def _object_match(self, object_id, context, parameters={}):
16
     def _object_match(self, object_id, context, parameters={}):
15
-        # TODO: nettoyer
16
         obj_colony_id = context.metas.value.get(COLONY, object_id)
17
         obj_colony_id = context.metas.value.get(COLONY, object_id)
17
-        for obj_near_id in parameters['objects_ids_near']:
18
-            if context.metas.states.have(obj_near_id, ATTACKABLE):
19
-                if obj_colony_id != context.metas.value.get(COLONY, obj_near_id):
20
-                    if 'objects_ids_attackable' not in parameters:
21
-                        parameters['objects_ids_attackable'] = []
22
-                    parameters['objects_ids_attackable'].append(obj_near_id)
23
-        if 'objects_ids_attackable' not in parameters:
18
+        filter = lambda near_object_id, context: obj_colony_id != context.metas.value.get(COLONY, near_object_id)
19
+        self.map(context, parameters, stop_at_first=True, filter=filter)
20
+        if self._near_name not in parameters:
24
             return False
21
             return False
25
         return True
22
         return True

+ 21 - 0
intelligine/synergy/event/src/NearEvent.py View File

1
+from synergine.synergy.event.Event import Event
2
+from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
+
4
+
5
+class NearEvent(Event):
6
+
7
+    _near_name = None
8
+    _near_map = lambda self, near_object_id, context: False
9
+
10
+    def __init__(self, actions):
11
+        super().__init__(actions)
12
+        self._mechanism = ArroundMechanism
13
+
14
+    def map(self, context, parameters, stop_at_first = False, filter = lambda near_object_id, context: True):
15
+        for near_object_id in parameters['objects_ids_near']:
16
+            if self._near_map(near_object_id, context) and filter(near_object_id, context):
17
+                if self._near_name not in parameters:
18
+                    parameters[self._near_name] = []
19
+                parameters[self._near_name].append(near_object_id)
20
+                if stop_at_first:
21
+                    return

+ 6 - 9
intelligine/synergy/event/transport/TakeableEvent.py View File

1
-from synergine.synergy.event.Event import Event
1
+from intelligine.synergy.event.src.NearEvent import NearEvent
2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
 from intelligine.cst import TRANSPORTABLE, CANT_CARRY_STILL, COL_TRANSPORTER_NOT_CARRYING
3
 from intelligine.cst import TRANSPORTABLE, CANT_CARRY_STILL, COL_TRANSPORTER_NOT_CARRYING
4
 
4
 
5
 
5
 
6
-class TakeableEvent(Event):
6
+class TakeableEvent(NearEvent):
7
 
7
 
8
     concern = COL_TRANSPORTER_NOT_CARRYING
8
     concern = COL_TRANSPORTER_NOT_CARRYING
9
+    _near_name = 'objects_ids_transportable'
10
+    _near_map = lambda self, near_object_id, context: context.metas.states.have(near_object_id, TRANSPORTABLE)
9
 
11
 
10
     def __init__(self, actions):
12
     def __init__(self, actions):
11
         super().__init__(actions)
13
         super().__init__(actions)
14
     def _object_match(self, object_id, context, parameters={}):
16
     def _object_match(self, object_id, context, parameters={}):
15
         if context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True):
17
         if context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True):
16
             return False
18
             return False
17
-        # TODO: Nettoyer (refact possible sur ces objets ont tel states, comme dans concern)
18
-        for obj_near_id in parameters['objects_ids_near']:
19
-            if context.metas.states.have(obj_near_id, TRANSPORTABLE):
20
-                if 'objects_ids_transportable' not in parameters:
21
-                    parameters['objects_ids_transportable'] = []
22
-                parameters['objects_ids_transportable'].append(obj_near_id)
23
-        if 'objects_ids_transportable' not in parameters:
19
+        self.map(context, parameters, stop_at_first=True)
20
+        if self._near_name not in parameters:
24
             return False
21
             return False
25
         return True
22
         return True