Bastien Sevajol hace 6 años
padre
commit
34efd06395

+ 0 - 1
sandbox/tiledstrategy/gui/base.py Ver fichero

@@ -25,5 +25,4 @@ class Game(TMXGui):
25 25
             if x % 2:
26 26
                 man.do(Animate(ANIMATION_WALK, 10, 4))
27 27
             else:
28
-                # TODO: Problème de zone selectable, elle reste a la taille de image debout.
29 28
                 man.do(Animate(ANIMATION_CRAWL, 20, 4))

+ 11 - 5
synergine2_cocos2d/actor.py Ver fichero

@@ -7,6 +7,7 @@ import cocos
7 7
 from cocos import collision_model
8 8
 from cocos import euclid
9 9
 from synergine2_cocos2d.animation import AnimatedInterface
10
+from synergine2_cocos2d.layer import LayerManager
10 11
 
11 12
 
12 13
 class Actor(AnimatedInterface, cocos.sprite.Sprite):
@@ -34,13 +35,18 @@ class Actor(AnimatedInterface, cocos.sprite.Sprite):
34 35
             anchor,
35 36
             **kwargs
36 37
         )
37
-        self.cshape = collision_model.AARectShape(
38
-            euclid.Vector2(0.0, 0.0),
39
-            self.width,
40
-            self.height,
41
-        )
38
+        self.cshape = None  # type: collision_model.AARectShape
39
+        self.update_cshape()
42 40
         self.build_animation_images()
43 41
         self.current_image = image
42
+        self.need_update_cshape = False
43
+
44
+    def update_cshape(self) -> None:
45
+        self.cshape = collision_model.AARectShape(
46
+            euclid.Vector2(self.position[0], self.position[1]),
47
+            self.width // 2,
48
+            self.height // 2,
49
+        )
44 50
 
45 51
     def update_position(self, new_position: euclid.Vector2) -> None:
46 52
         self.position = new_position

+ 13 - 0
synergine2_cocos2d/animation.py Ver fichero

@@ -39,6 +39,7 @@ class Animate(cocos.actions.IntervalAction):
39 39
         self.current_step = 0  # typ: int
40 40
         self.target = typing.cast(AnimatedInterface, self.target)
41 41
         self.direction = direction
42
+        self.reshape = False
42 43
 
43 44
     def __reversed__(self):
44 45
         return self.__class__(
@@ -48,10 +49,19 @@ class Animate(cocos.actions.IntervalAction):
48 49
             self.direction * -1,
49 50
         )
50 51
 
52
+    def set_need_to_reshape(self) -> None:
53
+        # TODO: Maybe inanimate_image is not the more appropriate image to refer ?
54
+        inanimate_image = self.target.get_inanimate_image()
55
+        for position, animation_image in enumerate(self.animation_images):
56
+            if animation_image.width != inanimate_image.width or animation_image.height != inanimate_image.height:
57
+                self.reshape = True
58
+                return
59
+
51 60
     def start(self):
52 61
         super().start()
53 62
         self.animation_images = self.target.get_images_for_animation(self.animation_name)
54 63
         self.step_interval = self.cycle_duration / len(self.animation_images)
64
+        self.set_need_to_reshape()
55 65
 
56 66
     def stop(self):
57 67
         self.target.update_image(self.target.get_inanimate_image())
@@ -71,5 +81,8 @@ class Animate(cocos.actions.IntervalAction):
71 81
         self.target.update_image(new_image)
72 82
         self.last_step_elapsed = self._elapsed
73 83
 
84
+        if self.reshape:
85
+            self.target.need_update_cshape = True
86
+
74 87
     def is_time_for_new_image(self) -> bool:
75 88
         return self._elapsed - self.last_step_elapsed >= self.step_interval

+ 19 - 0
synergine2_cocos2d/gui.py Ver fichero

@@ -15,6 +15,7 @@ from synergine2.config import Config
15 15
 from synergine2.log import SynergineLogger
16 16
 from synergine2.terminals import Terminal
17 17
 from synergine2.terminals import TerminalPackage
18
+from synergine2_cocos2d.actor import Actor
18 19
 from synergine2_cocos2d.layer import LayerManager
19 20
 from synergine2_cocos2d.middleware import TMXMiddleware
20 21
 
@@ -199,6 +200,24 @@ class EditLayer(cocos.layer.Layer):
199 200
         )
200 201
 
201 202
         self.schedule(self.update)
203
+        self.selectable_actors = []
204
+
205
+    def set_selectable(self, actor: Actor) -> None:
206
+        self.selectable_actors.append(actor)
207
+        self.collision_manager.add(actor)
208
+
209
+    def unset_selectable(self, actor: Actor) -> None:
210
+        self.selectable_actors.remove(actor)
211
+        self.collision_manager.remove_tricky(actor)
212
+
213
+    def draw(self, *args, **kwargs) -> None:
214
+        for actor in self.selectable_actors:
215
+            if actor.need_update_cshape:
216
+                if self.collision_manager.knows(actor):
217
+                    self.collision_manager.remove_tricky(actor)
218
+                    actor.update_cshape()
219
+                    self.collision_manager.add(actor)
220
+                    actor.need_update_cshape = False
202 221
 
203 222
     def on_enter(self):
204 223
         super(EditLayer, self).on_enter()

+ 2 - 2
synergine2_cocos2d/layer.py Ver fichero

@@ -101,7 +101,7 @@ class LayerManager(object):
101 101
         self.subject_layer.remove(subject)
102 102
 
103 103
     def set_selectable(self, subject: 'Actor') -> None:
104
-        self.edit_layer.collision_manager.add(subject)
104
+        self.edit_layer.set_selectable(subject)
105 105
 
106 106
     def unset_selectable(self, subject: 'Actor') -> None:
107
-        self.edit_layer.collision_manager.remove_tricky(subject)
107
+        self.edit_layer.unset_selectable(subject)