Browse Source

implement run

Bastien Sevajol 7 years ago
parent
commit
65a00d9858

+ 1 - 1
sandbox/tile/config.yaml View File

@@ -6,4 +6,4 @@ terminals:
6 6
 game:
7 7
     move:
8 8
         walk_ref_time: 3
9
-        run_ref_time: 1.5
9
+        run_ref_time: 1

+ 2 - 0
sandbox/tile/gui/base.py View File

@@ -1,11 +1,13 @@
1 1
 # coding: utf-8
2 2
 from synergine2_cocos2d.gui import TMXGui
3 3
 from synergine2_cocos2d.interaction import MoveActorInteraction
4
+from synergine2_cocos2d.interaction import MoveFastActorInteraction
4 5
 
5 6
 
6 7
 class Game(TMXGui):
7 8
     def before_run(self) -> None:
8 9
         self.layer_manager.interaction_manager.register(MoveActorInteraction, self.layer_manager)
10
+        self.layer_manager.interaction_manager.register(MoveFastActorInteraction, self.layer_manager)
9 11
 
10 12
         # Test
11 13
         # from sandbox.tile.gui.actor import Man

+ 5 - 0
synergine2_cocos2d/actor.py View File

@@ -43,6 +43,11 @@ class Actor(AnimatedInterface, cocos.sprite.Sprite):
43 43
         self.current_image = image
44 44
         self.need_update_cshape = False
45 45
 
46
+    def stop_actions(self, action_types: typing.Tuple[typing.Type[cocos.actions.Action], ...]) -> None:
47
+        for action in self.actions:
48
+            if isinstance(action, action_types):
49
+                self.remove_action(action)
50
+
46 51
     def update_cshape(self) -> None:
47 52
         self.cshape = collision_model.AARectShape(
48 53
             euclid.Vector2(self.position[0], self.position[1]),

+ 18 - 2
synergine2_cocos2d/gui.py View File

@@ -11,6 +11,7 @@ import cocos
11 11
 from cocos import collision_model
12 12
 from cocos import euclid
13 13
 from cocos.layer import ScrollableLayer
14
+from cocos.actions import MoveTo as BaseMoveTo
14 15
 from synergine2.config import Config
15 16
 from synergine2.log import SynergineLogger
16 17
 from synergine2.terminals import Terminal
@@ -397,6 +398,8 @@ class EditLayer(cocos.layer.Layer):
397 398
         if self.selection:
398 399
             if k == key.M:
399 400
                 self.user_action_pending = UserAction.ORDER_MOVE
401
+            if k == key.R:
402
+                self.user_action_pending = UserAction.ORDER_MOVE_FAST
400 403
 
401 404
         if k in binds:
402 405
             self.buttons[binds[k]] = 1
@@ -757,6 +760,7 @@ class TMXGui(Gui):
757 760
 
758 761
         # configs
759 762
         self.move_duration_ref = float(self.config.resolve('game.move.walk_ref_time'))
763
+        self.move_fast_duration_ref = float(self.config.resolve('game.move.run_ref_time'))
760 764
 
761 765
     def get_layer_middleware(self) -> MapMiddleware:
762 766
         return TMXMiddleware(
@@ -782,13 +786,25 @@ class TMXGui(Gui):
782 786
         actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
783 787
         new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
784 788
 
789
+        actor.stop_actions((BaseMoveTo,))
785 790
         actor.set_position(*new_world_position)
786 791
 
787 792
     def start_move_subject(self, event: StartMoveEvent):
788 793
         actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
789 794
         new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
790 795
 
791
-        move_action = MoveTo(new_world_position, self.move_duration_ref)
796
+        if event.gui_action == UserAction.ORDER_MOVE:
797
+            animation = ANIMATION_WALK
798
+            cycle_duration = 2
799
+            move_duration = self.move_duration_ref
800
+        elif event.gui_action == UserAction.ORDER_MOVE_FAST:
801
+            animation = ANIMATION_WALK
802
+            cycle_duration = 0.5
803
+            move_duration = self.move_fast_duration_ref
804
+        else:
805
+            raise NotImplementedError()
806
+
807
+        move_action = MoveTo(new_world_position, move_duration)
792 808
         actor.do(move_action)
793
-        actor.do(Animate(ANIMATION_WALK, duration=self.move_duration_ref, cycle_duration=2))
809
+        actor.do(Animate(animation, duration=move_duration, cycle_duration=cycle_duration))
794 810
         actor.rotation = get_angle(event.from_position, event.to_position)

+ 15 - 3
synergine2_cocos2d/interaction.py View File

@@ -70,8 +70,9 @@ class Interaction(object):
70 70
         raise NotImplementedError()
71 71
 
72 72
 
73
-class MoveActorInteraction(Interaction):
74
-    gui_action = UserAction.ORDER_MOVE
73
+class BaseMoveActorInteraction(Interaction):
74
+    gui_action = None
75
+    color = None
75 76
     request_move_behaviour_class = RequestMoveBehaviour
76 77
 
77 78
     def draw_pending(self) -> None:
@@ -82,7 +83,7 @@ class MoveActorInteraction(Interaction):
82 83
                 draw_line(
83 84
                     self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
84 85
                     self.layer_manager.edit_layer.screen_mouse,
85
-                    (0, 0, 255),
86
+                    self.color,
86 87
                 )
87 88
 
88 89
     def get_package_for_terminal(self) -> TerminalPackage:
@@ -99,9 +100,20 @@ class MoveActorInteraction(Interaction):
99 100
                 self.request_move_behaviour_class, {
100 101
                     'subject_id': actor.subject.id,
101 102
                     'move_to': mouse_grid_position,
103
+                    'gui_action': self.gui_action,
102 104
                 }
103 105
             ))
104 106
 
105 107
         return TerminalPackage(
106 108
             simulation_actions=actions,
107 109
         )
110
+
111
+
112
+class MoveActorInteraction(BaseMoveActorInteraction):
113
+    gui_action = UserAction.ORDER_MOVE
114
+    color = (0, 0, 255)
115
+
116
+
117
+class MoveFastActorInteraction(BaseMoveActorInteraction):
118
+    gui_action = UserAction.ORDER_MOVE_FAST
119
+    color = (72, 244, 66)

+ 1 - 0
synergine2_cocos2d/user_action.py View File

@@ -4,3 +4,4 @@ from enum import Enum
4 4
 
5 5
 class UserAction(Enum):
6 6
     ORDER_MOVE = 'ORDER_MOVE'
7
+    ORDER_MOVE_FAST = 'ORDER_MOVE_FAST'

+ 32 - 5
synergine2_xyz/move.py View File

@@ -11,6 +11,7 @@ from synergine2.simulation import SubjectMechanism
11 11
 from synergine2.simulation import Intention
12 12
 from synergine2.simulation import Simulation
13 13
 from synergine2.simulation import Event
14
+from synergine2_cocos2d.user_action import UserAction
14 15
 from synergine2_xyz.simulation import XYZSimulation
15 16
 
16 17
 
@@ -19,6 +20,7 @@ class MoveToIntention(Intention):
19 20
         self,
20 21
         move_to: typing.Tuple[int, int],
21 22
         start_time: float,
23
+        gui_action: typing.Any,
22 24
     ) -> None:
23 25
         self.move_to = move_to
24 26
         self.path = []  # type: typing.List[typing.Tuple[int, int]]
@@ -26,6 +28,7 @@ class MoveToIntention(Intention):
26 28
         self.last_intention_time = start_time
27 29
         self.just_reach = False
28 30
         self.initial = True
31
+        self.gui_action = gui_action
29 32
 
30 33
 
31 34
 class RequestMoveBehaviour(SimulationBehaviour):
@@ -54,7 +57,11 @@ class RequestMoveBehaviour(SimulationBehaviour):
54 57
 
55 58
         try:
56 59
             subject = self.simulation.subjects.index[subject_id]
57
-            subject.intentions.set(self.move_intention_class(move_to, start_time=time.time()))
60
+            subject.intentions.set(self.move_intention_class(
61
+                move_to,
62
+                start_time=time.time(),
63
+                gui_action=data['gui_action'],
64
+            ))
58 65
         except KeyError:
59 66
             # TODO: log error here
60 67
             pass
@@ -102,6 +109,7 @@ class MoveToMechanism(SubjectMechanism):
102 109
                 'last_intention_time': move.last_intention_time,
103 110
                 'just_reach': move.just_reach,
104 111
                 'initial': move.initial,
112
+                'gui_action': move.gui_action,
105 113
             }
106 114
 
107 115
         except IndexError:  # TODO: Specialize ? No movement left
@@ -116,6 +124,7 @@ class FinishMoveEvent(Event):
116 124
         subject_id: int,
117 125
         from_position: typing.Tuple[int, int],
118 126
         to_position: typing.Tuple[int, int],
127
+        gui_action: typing.Any,
119 128
         *args,
120 129
         **kwargs
121 130
     ):
@@ -123,6 +132,7 @@ class FinishMoveEvent(Event):
123 132
         self.subject_id = subject_id
124 133
         self.from_position = from_position
125 134
         self.to_position = to_position
135
+        self.gui_action = gui_action
126 136
 
127 137
     def repr_debug(self) -> str:
128 138
         return '{}: subject_id:{}, from_position:{} to_position: {}'.format(
@@ -139,6 +149,7 @@ class StartMoveEvent(Event):
139 149
         subject_id: int,
140 150
         from_position: typing.Tuple[int, int],
141 151
         to_position: typing.Tuple[int, int],
152
+        gui_action: typing.Any,
142 153
         *args,
143 154
         **kwargs
144 155
     ):
@@ -146,6 +157,7 @@ class StartMoveEvent(Event):
146 157
         self.subject_id = subject_id
147 158
         self.from_position = from_position
148 159
         self.to_position = to_position
160
+        self.gui_action = gui_action
149 161
 
150 162
     def repr_debug(self) -> str:
151 163
         return '{}: subject_id:{}, from_position:{} to_position: {}'.format(
@@ -167,7 +179,8 @@ class MoveToBehaviour(SubjectBehaviour):
167 179
         subject: Subject,
168 180
     ) -> None:
169 181
         super().__init__(config, simulation, subject)
170
-        self._duration = float(self.config.resolve('game.move.walk_ref_time'))
182
+        self._walk_duration = float(self.config.resolve('game.move.walk_ref_time'))
183
+        self._run_duration = float(self.config.resolve('game.move.run_ref_time'))
171 184
 
172 185
     def run(self, data):
173 186
         move_to_data = data[self.move_to_mechanism]
@@ -184,7 +197,11 @@ class MoveToBehaviour(SubjectBehaviour):
184 197
         return False
185 198
 
186 199
     def _can_move_to_next_step(self, move_to_data: dict) -> bool:
187
-        return time.time() - move_to_data['last_intention_time'] >= self._duration
200
+        # TODO: Relation vers cocos ! Deplacer UserAction ?
201
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE:
202
+            return time.time() - move_to_data['last_intention_time'] >= self._walk_duration
203
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE_FAST:
204
+            return time.time() - move_to_data['last_intention_time'] >= self._run_duration
188 205
 
189 206
     def _is_fresh_new_step(self, move_to_data: dict) -> bool:
190 207
         return move_to_data['just_reach'] or move_to_data['initial']
@@ -208,10 +225,20 @@ class MoveToBehaviour(SubjectBehaviour):
208 225
             self.subject.position = new_position
209 226
             move.last_intention_time = time.time()
210 227
             move.just_reach = True
211
-            event = FinishMoveEvent(self.subject.id, previous_position, new_position)
228
+            event = FinishMoveEvent(
229
+                self.subject.id,
230
+                previous_position,
231
+                new_position,
232
+                gui_action=move.gui_action,
233
+            )
212 234
         else:
213 235
             move.just_reach = False
214
-            event = StartMoveEvent(self.subject.id, previous_position, new_position)
236
+            event = StartMoveEvent(
237
+                self.subject.id,
238
+                previous_position,
239
+                new_position,
240
+                gui_action=move.gui_action,
241
+            )
215 242
 
216 243
         move.initial = False
217 244
         # Note: Need to explicitly set to update shared data