Browse Source

move behaviour with gui interract

Bastien Sevajol 6 years ago
parent
commit
5b5318cf52

+ 3 - 0
sandbox/tile/const.py View File

@@ -0,0 +1,3 @@
1
+# coding: utf-8
2
+
3
+COLLECTION_ALIVE = 'ALIVE'

+ 1 - 1
sandbox/tile/simulation/base.py View File

@@ -1,7 +1,7 @@
1 1
 # coding: utf-8
2 2
 from synergine2_xyz.simulation import XYZSimulation
3
-from synergine2_xyz.xyz import XYZSubject
4 3
 from synergine2_xyz.subjects import XYZSubjects
4
+from synergine2_xyz.subjects import XYZSubject
5 5
 
6 6
 
7 7
 class TileStrategySimulation(XYZSimulation):

+ 10 - 1
sandbox/tile/simulation/subject.py View File

@@ -1,6 +1,15 @@
1 1
 # coding: utf-8
2
+from sandbox.tile.const import COLLECTION_ALIVE
2 3
 from sandbox.tile.simulation.base import BaseSubject
4
+from synergine2_xyz.move import MoveToBehaviour
3 5
 
4 6
 
5 7
 class Man(BaseSubject):
6
-    pass
8
+    collections = [
9
+        COLLECTION_ALIVE,
10
+    ]
11
+    behaviours_classes = [
12
+        MoveToBehaviour,
13
+    ]
14
+    # TODO: implement (copied from engulf)
15
+    # behaviour_selector_class = CellBehaviourSelector

+ 13 - 2
synergine2/simulation.py View File

@@ -11,6 +11,17 @@ class Intention(object):
11 11
     pass
12 12
 
13 13
 
14
+class IntentionManager(object):
15
+    def __init__(self) -> None:
16
+        self.intentions = {}  # type: typing.Dict[typing.Type[Intention], Intention]
17
+
18
+    def set(self, intention: Intention) -> None:
19
+        self.intentions[type(intention)] = intention
20
+
21
+    def get(self, intention_type: typing.Type[Intention]) -> Intention:
22
+        return self.intentions[intention_type]
23
+
24
+
14 25
 class Subject(BaseObject):
15 26
     collections = []
16 27
     behaviours_classes = []
@@ -29,7 +40,7 @@ class Subject(BaseObject):
29 40
         self.simulation = simulation
30 41
         self.behaviours = {}
31 42
         self.mechanisms = {}
32
-        self.intentions = []  # type: typing.List[Intention]
43
+        self.intentions = IntentionManager()
33 44
         self.behaviour_selector = None  # type: SubjectBehaviourSelector
34 45
         if self.behaviour_selector_class:
35 46
             self.behaviour_selector = self.behaviour_selector_class()
@@ -184,7 +195,7 @@ class Event(BaseObject):
184 195
 
185 196
 class SubjectBehaviour(BaseObject):
186 197
     frequency = 1
187
-    use = []
198
+    use = []  # type: typing.List[typing.Type[SubjectMechanism]]
188 199
 
189 200
     def __init__(
190 201
             self,

+ 1 - 0
synergine2_cocos2d/terminal.py View File

@@ -9,6 +9,7 @@ class GameTerminal(Terminal):
9 9
 
10 10
     def receive(self, package: TerminalPackage):
11 11
         self.gui.before_received(package)
12
+        # TODO: pas d'event après le move: il faut subscribe je crois :p
12 13
         super().receive(package)
13 14
         self.gui.after_received(package)
14 15
 

+ 84 - 6
synergine2_xyz/move.py View File

@@ -2,7 +2,9 @@
2 2
 import typing
3 3
 
4 4
 from synergine2.config import Config
5
-from synergine2.simulation import SimulationBehaviour, SubjectBehaviour
5
+from synergine2.simulation import SimulationBehaviour
6
+from synergine2.simulation import SubjectBehaviour
7
+from synergine2.simulation import SubjectMechanism
6 8
 from synergine2.simulation import Intention
7 9
 from synergine2.simulation import Simulation
8 10
 from synergine2.simulation import Event
@@ -13,7 +15,7 @@ class MoveToIntention(Intention):
13 15
     def __init__(self, move_to: typing.Tuple[int, int]) -> None:
14 16
         self.move_to = move_to
15 17
         self.path = []  # type: typing.List[typing.Tuple[int, int]]
16
-        self.path_progression = None  # type: int
18
+        self.path_progression = -1  # type: int
17 19
 
18 20
 
19 21
 class RequestMoveBehaviour(SimulationBehaviour):
@@ -42,7 +44,7 @@ class RequestMoveBehaviour(SimulationBehaviour):
42 44
 
43 45
         try:
44 46
             subject = self.simulation.subjects.index[subject_id]
45
-            subject.intentions.append(self.move_intention_class(move_to))
47
+            subject.intentions.set(self.move_intention_class(move_to))
46 48
         except KeyError:
47 49
             # TODO: log error here
48 50
             pass
@@ -50,11 +52,87 @@ class RequestMoveBehaviour(SimulationBehaviour):
50 52
         return []
51 53
 
52 54
 
55
+class MoveToMechanism(SubjectMechanism):
56
+    def run(self):
57
+        # TODO: Si move to: Si nouveau: a*, si bloque, a*, sinon rien
58
+        # TODO: pourquoi un mechanism plutot que dans run du behaviour ? faire en sorte que lorsque on calcule,
59
+        # si un subject est déjà passé par là et qu'il va au même endroit, ne pas recalculer.
60
+        try:
61
+            # TODO: MoveToIntention doit être configurable
62
+            move = self.subject.intentions.get(MoveToIntention)
63
+            move = typing.cast(MoveToIntention, move)
64
+            new_path = move.path or None
65
+
66
+            if not move.path:
67
+                # TODO: Fake to test
68
+                new_path = []
69
+                for i in range(20):
70
+                    new_path.append((
71
+                        self.subject.position[0],
72
+                        self.subject.position[1] + i,
73
+                    ))
74
+
75
+            next_move = new_path[move.path_progression + 1]
76
+            # TODO: fin de path
77
+            if not self.simulation.is_possible_position(next_move):
78
+                # TODO: refaire le path
79
+                new_path = ['...']
80
+
81
+            return {
82
+                'new_path': new_path,
83
+            }
84
+
85
+        except KeyError:
86
+            return None
87
+
88
+
89
+class MoveEvent(Event):
90
+    def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
91
+        super().__init__(*args, **kwargs)
92
+        self.subject_id = subject_id
93
+        self.position = position
94
+
95
+    def repr_debug(self) -> str:
96
+        return '{}: subject_id:{}, position:{}'.format(
97
+            type(self).__name__,
98
+            self.subject_id,
99
+            self.position,
100
+        )
101
+
102
+
53 103
 class MoveToBehaviour(SubjectBehaviour):
104
+    use = [MoveToMechanism]
105
+    move_to_mechanism = MoveToMechanism
106
+
54 107
     def run(self, data):
55
-        # TODO: progresser dans l'intention (comment implementer ça?)
56
-        raise NotImplementedError()
108
+        # TODO: on fait vraiment rien ici ? Note: meme si il n'y a pas de new_path, l'action doit s'effectuer
109
+        # du moment qu'il y a une intention de move
110
+        move_to_data = data[self.move_to_mechanism]
111
+        if move_to_data:
112
+            return move_to_data
113
+        return False
57 114
 
58 115
     def action(self, data) -> [Event]:
59 116
         # TODO: effectuer un move vers une nouvelle position ou faire progresser "l'entre-deux"
60
-        raise NotImplementedError()
117
+        new_path = data['new_path']
118
+        try:
119
+            # TODO: MoveToIntention doit être configurable
120
+            move = self.subject.intentions.get(MoveToIntention)
121
+            move = typing.cast(MoveToIntention, move)
122
+
123
+            if new_path:
124
+                move.path = new_path
125
+                move.path_progression = -1
126
+
127
+            # TODO: progression et lorsque "vraiment avance d'une case" envoyer le Move
128
+            # pour le moment on move direct
129
+            # TODO: fin de path
130
+            move.path_progression += 1  # BUG: ca progresse pas ?
131
+            new_position = move.path[move.path_progression]
132
+            self.subject.position = new_position
133
+
134
+            return [MoveEvent(self.subject.id, new_position)]
135
+
136
+        except KeyError:
137
+            # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !
138
+            pass

+ 1 - 1
synergine2_xyz/simulation.py View File

@@ -2,8 +2,8 @@
2 2
 import typing
3 3
 
4 4
 from synergine2.simulation import Simulation as BaseSimulation
5
-from synergine2_xyz.xyz import XYZSubject
6 5
 from synergine2_xyz.subjects import XYZSubjects
6
+from synergine2_xyz.subjects import XYZSubject
7 7
 
8 8
 
9 9
 class XYZSimulation(BaseSimulation):

+ 8 - 2
synergine2_xyz/subjects.py View File

@@ -1,6 +1,8 @@
1 1
 # coding: utf-8
2 2
 from synergine2.simulation import Subjects
3
-from synergine2_xyz.xyz import XYZSubjectMixin, PositionNotPossible
3
+from synergine2.simulation import Subject
4
+from synergine2_xyz.xyz import XYZSubjectMixin
5
+from synergine2_xyz.xyz import PositionNotPossible
4 6
 
5 7
 
6 8
 class XYZSubjects(Subjects):
@@ -33,4 +35,8 @@ class XYZSubjects(Subjects):
33 35
                 str(p_object),
34 36
             ))
35 37
 
36
-        self.xyz.setdefault(p_object.position, []).append(p_object)
38
+        self.xyz.setdefault(p_object.position, []).append(p_object)
39
+
40
+
41
+class XYZSubject(XYZSubjectMixin, Subject):
42
+    pass

+ 0 - 4
synergine2_xyz/xyz.py View File

@@ -131,10 +131,6 @@ class XYZSubjectMixin(object, metaclass=XYZSubjectMixinMetaClass):
131 131
         self._position = value
132 132
 
133 133
 
134
-class XYZSubject(XYZSubjectMixin, Subject):
135
-    pass
136
-
137
-
138 134
 class ProximityMixin(object):
139 135
     distance = 1
140 136
     feel_collections = [COLLECTION_XYZ]