Browse Source

rotation on move

Bastien Sevajol 7 years ago
parent
commit
65bc2a0b20

+ 1 - 0
requirements.txt View File

4
 coverage==4.4.1
4
 coverage==4.4.1
5
 Dijkstar==2.2
5
 Dijkstar==2.2
6
 execnet==1.4.1
6
 execnet==1.4.1
7
+numpy==1.13.1
7
 packaging==16.8
8
 packaging==16.8
8
 psutil==5.2.2
9
 psutil==5.2.2
9
 py==1.4.31
10
 py==1.4.31

+ 3 - 0
synergine2_cocos2d/animation.py View File

4
 import pyglet
4
 import pyglet
5
 import cocos
5
 import cocos
6
 
6
 
7
+if False:
8
+    from synergine2_cocos2d.actor import Actor
9
+
7
 ANIMATION_WALK = 'WALK'
10
 ANIMATION_WALK = 'WALK'
8
 ANIMATION_CRAWL = 'CRAWL'
11
 ANIMATION_CRAWL = 'CRAWL'
9
 
12
 

+ 3 - 1
synergine2_cocos2d/gui.py View File

29
 from synergine2_cocos2d.middleware import TMXMiddleware
29
 from synergine2_cocos2d.middleware import TMXMiddleware
30
 from synergine2_cocos2d.user_action import UserAction
30
 from synergine2_cocos2d.user_action import UserAction
31
 from synergine2_xyz.move import MoveEvent
31
 from synergine2_xyz.move import MoveEvent
32
+from synergine2_xyz.utils import get_angle
32
 from synergine2_xyz.xyz import XYZSubjectMixin
33
 from synergine2_xyz.xyz import XYZSubjectMixin
33
 
34
 
34
 
35
 
771
     def move_subject(self, event: MoveEvent):
772
     def move_subject(self, event: MoveEvent):
772
         actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
773
         actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
773
 
774
 
774
-        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.position)
775
+        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
775
         new_window_position = self.layer_manager.scrolling_manager.world_to_screen(*new_world_position)
776
         new_window_position = self.layer_manager.scrolling_manager.world_to_screen(*new_world_position)
776
 
777
 
777
         move_action = MoveTo(new_window_position, 0.5)
778
         move_action = MoveTo(new_window_position, 0.5)
778
         actor.do(move_action)
779
         actor.do(move_action)
779
         # TODO: values
780
         # TODO: values
780
         actor.do(Animate(ANIMATION_WALK, 0.5, 1))
781
         actor.do(Animate(ANIMATION_WALK, 0.5, 1))
782
+        actor.rotation = get_angle(event.from_position, event.to_position)

+ 13 - 4
synergine2_xyz/move.py View File

73
                 found_path = find_path(self.simulation.graph, start, end)
73
                 found_path = find_path(self.simulation.graph, start, end)
74
                 move.path = []
74
                 move.path = []
75
 
75
 
76
-                for position in found_path[0]:
76
+                for position in found_path[0][1:]:
77
                     x, y = map(int, position.split('.'))
77
                     x, y = map(int, position.split('.'))
78
                     move.path.append((x, y))
78
                     move.path.append((x, y))
79
 
79
 
105
 
105
 
106
 
106
 
107
 class MoveEvent(Event):
107
 class MoveEvent(Event):
108
-    def __init__(self, subject_id: int, position: tuple, *args, **kwargs):
108
+    def __init__(
109
+        self,
110
+        subject_id: int,
111
+        from_position: typing.Tuple[int, int],
112
+        to_position: typing.Tuple[int, int],
113
+        *args,
114
+        **kwargs
115
+    ):
109
         super().__init__(*args, **kwargs)
116
         super().__init__(*args, **kwargs)
110
         self.subject_id = subject_id
117
         self.subject_id = subject_id
111
-        self.position = position
118
+        self.from_position = from_position
119
+        self.to_position = to_position
112
 
120
 
113
     def repr_debug(self) -> str:
121
     def repr_debug(self) -> str:
114
         return '{}: subject_id:{}, position:{}'.format(
122
         return '{}: subject_id:{}, position:{}'.format(
147
             # TODO: fin de path
155
             # TODO: fin de path
148
             move.path_progression += 1
156
             move.path_progression += 1
149
             new_position = move.path[move.path_progression]
157
             new_position = move.path[move.path_progression]
158
+            previous_position = self.subject.position
150
             self.subject.position = new_position
159
             self.subject.position = new_position
151
 
160
 
152
-            return [MoveEvent(self.subject.id, new_position)]
161
+            return [MoveEvent(self.subject.id, previous_position, new_position)]
153
 
162
 
154
         except KeyError:
163
         except KeyError:
155
             # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !
164
             # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !

+ 13 - 0
synergine2_xyz/utils.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
 import collections
2
 import collections
3
+import typing
3
 from math import sqrt
4
 from math import sqrt
4
 
5
 
6
+import numpy
7
+
5
 from synergine2_xyz.xyz import DIRECTION_MODIFIERS
8
 from synergine2_xyz.xyz import DIRECTION_MODIFIERS
6
 
9
 
7
 
10
 
215
         from_position[1] + modifier[1],
218
         from_position[1] + modifier[1],
216
         from_position[2] + modifier[2],
219
         from_position[2] + modifier[2],
217
     )
220
     )
221
+
222
+
223
+def get_angle(a: typing.Tuple[int, int], b: typing.Tuple[int, int]) -> int:
224
+    b = (b[0] - a[0], b[1] - a[1])
225
+    a = 0, 1
226
+
227
+    ang1 = numpy.arctan2(*a[::-1])
228
+    ang2 = numpy.arctan2(*b[::-1])
229
+
230
+    return numpy.rad2deg((ang1 - ang2) % (2 * numpy.pi))