Browse Source

rotation on move

Bastien Sevajol 6 years ago
parent
commit
65bc2a0b20
5 changed files with 33 additions and 5 deletions
  1. 1 0
      requirements.txt
  2. 3 0
      synergine2_cocos2d/animation.py
  3. 3 1
      synergine2_cocos2d/gui.py
  4. 13 4
      synergine2_xyz/move.py
  5. 13 0
      synergine2_xyz/utils.py

+ 1 - 0
requirements.txt View File

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

+ 3 - 0
synergine2_cocos2d/animation.py View File

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

+ 3 - 1
synergine2_cocos2d/gui.py View File

@@ -29,6 +29,7 @@ from synergine2_cocos2d.middleware import MapMiddleware
29 29
 from synergine2_cocos2d.middleware import TMXMiddleware
30 30
 from synergine2_cocos2d.user_action import UserAction
31 31
 from synergine2_xyz.move import MoveEvent
32
+from synergine2_xyz.utils import get_angle
32 33
 from synergine2_xyz.xyz import XYZSubjectMixin
33 34
 
34 35
 
@@ -771,10 +772,11 @@ class TMXGui(Gui):
771 772
     def move_subject(self, event: MoveEvent):
772 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 776
         new_window_position = self.layer_manager.scrolling_manager.world_to_screen(*new_world_position)
776 777
 
777 778
         move_action = MoveTo(new_window_position, 0.5)
778 779
         actor.do(move_action)
779 780
         # TODO: values
780 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,7 +73,7 @@ class MoveToMechanism(SubjectMechanism):
73 73
                 found_path = find_path(self.simulation.graph, start, end)
74 74
                 move.path = []
75 75
 
76
-                for position in found_path[0]:
76
+                for position in found_path[0][1:]:
77 77
                     x, y = map(int, position.split('.'))
78 78
                     move.path.append((x, y))
79 79
 
@@ -105,10 +105,18 @@ class MoveToMechanism(SubjectMechanism):
105 105
 
106 106
 
107 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 116
         super().__init__(*args, **kwargs)
110 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 121
     def repr_debug(self) -> str:
114 122
         return '{}: subject_id:{}, position:{}'.format(
@@ -147,9 +155,10 @@ class MoveToBehaviour(SubjectBehaviour):
147 155
             # TODO: fin de path
148 156
             move.path_progression += 1
149 157
             new_position = move.path[move.path_progression]
158
+            previous_position = self.subject.position
150 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 163
         except KeyError:
155 164
             # TODO: log ? Il devrait y avoir un move puisque data du run/mechanism !

+ 13 - 0
synergine2_xyz/utils.py View File

@@ -1,7 +1,10 @@
1 1
 # coding: utf-8
2 2
 import collections
3
+import typing
3 4
 from math import sqrt
4 5
 
6
+import numpy
7
+
5 8
 from synergine2_xyz.xyz import DIRECTION_MODIFIERS
6 9
 
7 10
 
@@ -215,3 +218,13 @@ def get_position_for_direction(from_position: tuple, direction: int) -> tuple:
215 218
         from_position[1] + modifier[1],
216 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))