Bastien Sevajol 7 lat temu
rodzic
commit
c5c2a03add

+ 28 - 0
sandbox/tiledstrategy/gui/actor.py Wyświetl plik

1
+# coding: utf-8
2
+import typing
3
+
4
+import pyglet
5
+
6
+from sandbox.tiledstrategy.gui.animation import ANIMATION_WALK
7
+from synergine2_cocos2d.actor import Actor
8
+
9
+
10
+class Man(Actor):
11
+    animation_image_paths = {
12
+        ANIMATION_WALK: [
13
+            'actors/man.png',
14
+            'actors/man_w1.png',
15
+            'actors/man_w2.png',
16
+            'actors/man_w3.png',
17
+            'actors/man_w4.png',
18
+            'actors/man_w5.png',
19
+            'actors/man_w6.png',
20
+            'actors/man_w7.png',
21
+            'actors/man_w8.png',
22
+            'actors/man_w9.png',
23
+            'actors/man_w10.png',
24
+        ]
25
+    }
26
+
27
+    def __init__(self):
28
+        super().__init__(pyglet.resource.image('actors/man.png'))

+ 4 - 0
sandbox/tiledstrategy/gui/animation.py Wyświetl plik

1
+# coding: utf-8
2
+
3
+
4
+ANIMATION_WALK = 'WALK'

+ 14 - 9
sandbox/tiledstrategy/gui/base.py Wyświetl plik

1
 # coding: utf-8
1
 # coding: utf-8
2
+import random
3
+
4
+from sandbox.tiledstrategy.gui.animation import ANIMATION_WALK
5
+from synergine2_cocos2d.animation import Animate
2
 from synergine2_cocos2d.gui import TMXGui
6
 from synergine2_cocos2d.gui import TMXGui
3
 
7
 
4
 
8
 
5
 class Game(TMXGui):
9
 class Game(TMXGui):
6
     def before_run(self) -> None:
10
     def before_run(self) -> None:
7
         # Test
11
         # Test
12
+        from sandbox.tiledstrategy.gui.actor import Man
8
         from cocos import euclid
13
         from cocos import euclid
9
-        from synergine2_cocos2d.gui import Actor
10
 
14
 
11
-        man = Actor('man.png')
12
-        man.update_position(euclid.Vector2(100.0, 100.0))
13
-        self.layer_manager.add_subject(man)
14
-        self.layer_manager.set_selectable(man)
15
+        for i in range(10):
16
+            x = random.randint(0, 600)
17
+            y = random.randint(0, 300)
18
+            man = Man()
19
+            man.update_position(euclid.Vector2(x, y))
20
+            self.layer_manager.add_subject(man)
21
+            self.layer_manager.set_selectable(man)
22
+            man.scale = 1
15
 
23
 
16
-        man2 = Actor('man.png')
17
-        man2.update_position(euclid.Vector2(200.0, 200.0))
18
-        self.layer_manager.add_subject(man2)
19
-        self.layer_manager.set_selectable(man2)
24
+            man.do(Animate(ANIMATION_WALK, 10, 4))

BIN
sandbox/tiledstrategy/maps/003/actors/man.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w1.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w10.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w2.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w3.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w4.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w5.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w6.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w7.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w8.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/actors/man_w9.png Wyświetl plik


BIN
sandbox/tiledstrategy/maps/003/man.png Wyświetl plik


+ 66 - 0
synergine2_cocos2d/actor.py Wyświetl plik

1
+# coding: utf-8
2
+import typing
3
+
4
+import pyglet
5
+
6
+import cocos
7
+from cocos import collision_model
8
+from cocos import euclid
9
+from synergine2_cocos2d.animation import AnimatedInterface
10
+
11
+
12
+class Actor(AnimatedInterface, cocos.sprite.Sprite):
13
+    animation_image_paths = {}  # type: typing.Dict[str, typing.List[str]]
14
+    animation_images = {}  # type: typing.Dict[str, typing.List[pyglet.image.TextureRegion]]
15
+
16
+    def __init__(
17
+        self,
18
+        image: pyglet.image.TextureRegion,
19
+        position=(0, 0),
20
+        rotation=0,
21
+        scale=1,
22
+        opacity=255,
23
+        color=(255, 255, 255),
24
+        anchor=None,
25
+        **kwargs
26
+    ):
27
+        super().__init__(
28
+            image,
29
+            position,
30
+            rotation,
31
+            scale,
32
+            opacity,
33
+            color,
34
+            anchor,
35
+            **kwargs
36
+        )
37
+        self.cshape = collision_model.AARectShape(
38
+            euclid.Vector2(0.0, 0.0),
39
+            self.width,
40
+            self.height,
41
+        )
42
+        self.build_animation_images()
43
+        self.current_image = image
44
+
45
+    def update_position(self, new_position: euclid.Vector2) -> None:
46
+        self.position = new_position
47
+        self.cshape.center = new_position
48
+
49
+    def build_animation_images(self) -> None:
50
+        """
51
+        Fill self.animation_images with self.animation_image_paths
52
+        :return: None
53
+        """
54
+        for animation_name, animation_image_paths in self.animation_image_paths.items():
55
+            self.animation_images[animation_name] = []
56
+            for animation_image_path in animation_image_paths:
57
+                self.animation_images[animation_name].append(pyglet.resource.image(animation_image_path))
58
+
59
+    def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
60
+        return self.animation_images.get(animation_name)
61
+
62
+    def get_inanimate_image(self) -> pyglet.image.TextureRegion:
63
+        return self.current_image
64
+
65
+    def update_image(self, new_image: pyglet.image.TextureRegion):
66
+        self.image = new_image

+ 74 - 0
synergine2_cocos2d/animation.py Wyświetl plik

1
+# coding: utf-8
2
+import typing
3
+
4
+import pyglet
5
+import cocos
6
+
7
+
8
+class AnimatedInterface(object):
9
+    def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
10
+        raise NotImplementedError()
11
+
12
+    def get_inanimate_image(self) -> pyglet.image.TextureRegion:
13
+        """
14
+        Use this function to specify what image have to be used when animation is finished.
15
+        :return: non inanimate pyglet.image.TextureRegion
16
+        """
17
+        raise NotImplementedError()
18
+
19
+    def update_image(self, new_image: pyglet.image.TextureRegion):
20
+        raise NotImplementedError()
21
+
22
+
23
+class Animate(cocos.actions.IntervalAction):
24
+    def __init__(
25
+        self,
26
+        animation_name: str,
27
+        duration: float,
28
+        cycle_duration: float,
29
+        direction: int=1,
30
+    ):
31
+        super().__init__()
32
+        self.animation_name = animation_name
33
+        self.duration = duration
34
+        self.animation_images = []  # type: typing.List[pyglet.image.TextureRegion]
35
+        self.last_step_elapsed = 0.0  # type: float
36
+        self.step_interval = None  # type: float
37
+        self.cycle_duration = cycle_duration
38
+        self.current_step = 0  # typ: int
39
+        self.target = typing.cast(AnimatedInterface, self.target)
40
+        self.direction = direction
41
+
42
+    def __reversed__(self):
43
+        return self.__class__(
44
+            self.animation_name,
45
+            self.duration,
46
+            self.cycle_duration,
47
+            self.direction * -1,
48
+        )
49
+
50
+    def start(self):
51
+        super().start()
52
+        self.animation_images = self.target.get_images_for_animation(self.animation_name)
53
+        self.step_interval = self.cycle_duration / len(self.animation_images)
54
+
55
+    def stop(self):
56
+        self.target.update_image(self.target.get_inanimate_image())
57
+        super().stop()
58
+
59
+    def update(self, t):
60
+        if not self.is_time_for_new_image():
61
+            return
62
+
63
+        self.current_step += self.direction
64
+        try:
65
+            new_image = self.animation_images[self.current_step]
66
+        except IndexError:
67
+            self.current_step = 0
68
+            new_image = self.animation_images[0]
69
+
70
+        self.target.update_image(new_image)
71
+        self.last_step_elapsed = self._elapsed
72
+
73
+    def is_time_for_new_image(self) -> bool:
74
+        return self._elapsed - self.last_step_elapsed >= self.step_interval

+ 10 - 37
synergine2_cocos2d/gui.py Wyświetl plik

1
 # coding: utf-8
1
 # coding: utf-8
2
 import weakref
2
 import weakref
3
 
3
 
4
-import cocos
5
 import pyglet
4
 import pyglet
6
 from pyglet.window import mouse
5
 from pyglet.window import mouse
7
 
6
 
7
+import cocos
8
 from cocos import collision_model
8
 from cocos import collision_model
9
 from cocos import euclid
9
 from cocos import euclid
10
 from cocos.director import director
10
 from cocos.director import director
11
-from cocos.layer import ScrollableLayer
12
 from cocos.layer import Layer
11
 from cocos.layer import Layer
12
+from cocos.layer import ScrollableLayer
13
 from cocos.sprite import Sprite
13
 from cocos.sprite import Sprite
14
-
15
 from synergine2.config import Config
14
 from synergine2.config import Config
16
 from synergine2.log import SynergineLogger
15
 from synergine2.log import SynergineLogger
17
 from synergine2.terminals import Terminal
16
 from synergine2.terminals import Terminal
20
 from synergine2_cocos2d.middleware import TMXMiddleware
19
 from synergine2_cocos2d.middleware import TMXMiddleware
21
 
20
 
22
 
21
 
23
-class Actor(cocos.sprite.Sprite):
24
-    def __init__(
25
-        self,
26
-        image,
27
-        position=(0, 0),
28
-        rotation=0,
29
-        scale=1,
30
-        opacity=255,
31
-        color=(255, 255, 255),
32
-        anchor=None,
33
-        **kwargs
34
-    ):
35
-        super().__init__(
36
-            image,
37
-            position,
38
-            rotation,
39
-            scale,
40
-            opacity,
41
-            color,
42
-            anchor,
43
-            **kwargs
44
-        )
45
-        self.cshape = collision_model.AARectShape(
46
-            euclid.Vector2(0.0, 0.0),
47
-            self.width,
48
-            self.height,
49
-        )
50
-
51
-    def update_position(self, new_position: euclid.Vector2) -> None:
52
-        self.position = new_position
53
-        self.cshape.center = new_position
54
-
55
-
56
 class GridManager(object):
22
 class GridManager(object):
57
     def __init__(
23
     def __init__(
58
         self,
24
         self,
153
 
119
 
154
     def __init__(
120
     def __init__(
155
         self,
121
         self,
122
+        config: Config,
123
+        logger: SynergineLogger,
156
         layer_manager: LayerManager,
124
         layer_manager: LayerManager,
157
         worldview,
125
         worldview,
158
         bindings=None,
126
         bindings=None,
168
     ):
136
     ):
169
         super(EditLayer, self).__init__()
137
         super(EditLayer, self).__init__()
170
 
138
 
139
+        self.config = config
140
+        self.logger = logger
171
         self.layer_manager = layer_manager
141
         self.layer_manager = layer_manager
142
+
172
         self.bindings = bindings
143
         self.bindings = bindings
173
         buttons = {}
144
         buttons = {}
174
         modifiers = {}
145
         modifiers = {}
389
         self.autoscrolling = False
360
         self.autoscrolling = False
390
 
361
 
391
     def on_mouse_press(self, x, y, buttons, modifiers):
362
     def on_mouse_press(self, x, y, buttons, modifiers):
392
-        pass
363
+        if self.logger.is_debug:
364
+            rx, ry = self.layer_manager.scrolling_manager.screen_to_world(x, y)
365
+            self.logger.debug('GUI click: x: {}, y: {}, rx: {}, ry: {}'.format(x, y, rx, ry))
393
 
366
 
394
     def on_mouse_release(self, sx, sy, button, modifiers):
367
     def on_mouse_release(self, sx, sy, button, modifiers):
395
         # should we handle here mod_restricted_mov ?
368
         # should we handle here mod_restricted_mov ?

+ 3 - 1
synergine2_cocos2d/layer.py Wyświetl plik

8
 from synergine2_cocos2d.middleware import MapMiddleware
8
 from synergine2_cocos2d.middleware import MapMiddleware
9
 
9
 
10
 if False:
10
 if False:
11
-    from synergine2_cocos2d.gui import Actor
11
+    from synergine2_cocos2d.actor import Actor
12
 
12
 
13
 
13
 
14
 class LayerManager(object):
14
 class LayerManager(object):
49
             }
49
             }
50
         )
50
         )
51
         self.edit_layer = EditLayer(
51
         self.edit_layer = EditLayer(
52
+            self.config,
53
+            self.logger,
52
             self,
54
             self,
53
             self.main_layer,
55
             self.main_layer,
54
             **{
56
             **{