Browse Source

reorganize move code

Bastien Sevajol 6 years ago
parent
commit
94cb546615

+ 99 - 20
sandbox/tile/gui/base.py View File

@@ -1,30 +1,109 @@
1 1
 # coding: utf-8
2
+from pyglet.window import key
3
+from cocos.actions import MoveTo as BaseMoveTo
4
+from sandbox.tile.gui.move import MoveActorInteraction
5
+from sandbox.tile.gui.move import MoveFastActorInteraction
6
+from sandbox.tile.gui.move import MoveCrawlActorInteraction
7
+from synergine2_cocos2d.layer import LayerManager
8
+from synergine2_xyz.move.simulation import FinishMoveEvent
9
+from synergine2_xyz.move.simulation import StartMoveEvent
10
+
11
+from synergine2_xyz.utils import get_angle
12
+from synergine2.config import Config
13
+from synergine2.log import SynergineLogger
14
+from synergine2.terminals import Terminal
2 15
 from synergine2_cocos2d.gui import TMXGui
3
-from synergine2_cocos2d.interaction import MoveActorInteraction
4
-from synergine2_cocos2d.interaction import MoveFastActorInteraction
5
-from synergine2_cocos2d.interaction import MoveCrawlActorInteraction
16
+from synergine2_cocos2d.gui import EditLayer as BaseEditLayer
17
+from synergine2_cocos2d.actions import MoveTo
18
+from synergine2_cocos2d.animation import Animate
19
+# TODO NOW: MOVE
20
+from synergine2_cocos2d.animation import ANIMATION_CRAWL
21
+from synergine2_cocos2d.animation import ANIMATION_WALK
22
+
23
+from sandbox.tile.user_action import UserAction
24
+
25
+
26
+class EditLayer(BaseEditLayer):
27
+    def _on_key_press(self, k, m):
28
+        if self.selection:
29
+            if k == key.M:
30
+                self.user_action_pending = UserAction.ORDER_MOVE
31
+            if k == key.R:
32
+                self.user_action_pending = UserAction.ORDER_MOVE_FAST
33
+            if k == key.C:
34
+                self.user_action_pending = UserAction.ORDER_MOVE_CRAWL
35
+
36
+
37
+class TileLayerManager(LayerManager):
38
+    edit_layer_class = EditLayer
6 39
 
7 40
 
8 41
 class Game(TMXGui):
42
+    layer_manager_class = TileLayerManager
43
+
44
+    def __init__(
45
+        self,
46
+        config: Config,
47
+        logger: SynergineLogger,
48
+        terminal: Terminal,
49
+        read_queue_interval: float = 1 / 60.0,
50
+        map_dir_path: str=None,
51
+    ):
52
+        super().__init__(
53
+            config,
54
+            logger,
55
+            terminal,
56
+            read_queue_interval,
57
+            map_dir_path,
58
+        )
59
+
60
+        self.terminal.register_event_handler(
61
+            FinishMoveEvent,
62
+            self.set_subject_position,
63
+        )
64
+
65
+        self.terminal.register_event_handler(
66
+            StartMoveEvent,
67
+            self.start_move_subject,
68
+        )
69
+
70
+        # configs
71
+        self.move_duration_ref = float(self.config.resolve('game.move.walk_ref_time'))
72
+        self.move_fast_duration_ref = float(self.config.resolve('game.move.run_ref_time'))
73
+        self.move_crawl_duration_ref = float(self.config.resolve('game.move.crawl_ref_time'))
74
+
9 75
     def before_run(self) -> None:
10 76
         self.layer_manager.interaction_manager.register(MoveActorInteraction, self.layer_manager)
11 77
         self.layer_manager.interaction_manager.register(MoveFastActorInteraction, self.layer_manager)
12 78
         self.layer_manager.interaction_manager.register(MoveCrawlActorInteraction, self.layer_manager)
13 79
 
14
-        # Test
15
-        # from sandbox.tile.gui.actor import Man
16
-        # from cocos import euclid
17
-        #
18
-        # for i in range(10):
19
-        #     x = random.randint(0, 600)
20
-        #     y = random.randint(0, 300)
21
-        #     man = Man()
22
-        #     man.update_position(euclid.Vector2(x, y))
23
-        #     self.layer_manager.add_subject(man)
24
-        #     self.layer_manager.set_selectable(man)
25
-        #     man.scale = 1
26
-        #
27
-        #     if x % 2:
28
-        #         man.do(Animate(ANIMATION_WALK, 10, 4))
29
-        #     else:
30
-        #         man.do(Animate(ANIMATION_CRAWL, 20, 4))
80
+    def set_subject_position(self, event: FinishMoveEvent):
81
+        actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
82
+        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
83
+
84
+        actor.stop_actions((BaseMoveTo,))
85
+        actor.set_position(*new_world_position)
86
+
87
+    def start_move_subject(self, event: StartMoveEvent):
88
+        actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
89
+        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
90
+
91
+        if event.gui_action == UserAction.ORDER_MOVE:
92
+            animation = ANIMATION_WALK
93
+            cycle_duration = 2
94
+            move_duration = self.move_duration_ref
95
+        elif event.gui_action == UserAction.ORDER_MOVE_FAST:
96
+            animation = ANIMATION_WALK
97
+            cycle_duration = 0.5
98
+            move_duration = self.move_fast_duration_ref
99
+        elif event.gui_action == UserAction.ORDER_MOVE_CRAWL:
100
+            animation = ANIMATION_CRAWL
101
+            cycle_duration = 2
102
+            move_duration = self.move_crawl_duration_ref
103
+        else:
104
+            raise NotImplementedError()
105
+
106
+        move_action = MoveTo(new_world_position, move_duration)
107
+        actor.do(move_action)
108
+        actor.do(Animate(animation, duration=move_duration, cycle_duration=cycle_duration))
109
+        actor.rotation = get_angle(event.from_position, event.to_position)

+ 60 - 0
sandbox/tile/gui/move.py View File

@@ -0,0 +1,60 @@
1
+# coding: utf-8
2
+from sandbox.tile.user_action import UserAction
3
+from synergine2.terminals import TerminalPackage
4
+from synergine2_cocos2d.gl import draw_line
5
+from synergine2_cocos2d.interaction import Interaction
6
+from synergine2_xyz.move.simulation import RequestMoveBehaviour
7
+
8
+
9
+class BaseMoveActorInteraction(Interaction):
10
+    gui_action = None
11
+    color = None
12
+    request_move_behaviour_class = RequestMoveBehaviour
13
+
14
+    def draw_pending(self) -> None:
15
+        for actor in self.layer_manager.edit_layer.selection:
16
+            grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
17
+            pixel_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(grid_position)
18
+
19
+            draw_line(
20
+                self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
21
+                self.layer_manager.edit_layer.screen_mouse,
22
+                self.color,
23
+            )
24
+
25
+    def get_package_for_terminal(self) -> TerminalPackage:
26
+        # TODO: FinishMoveEvent ?
27
+        actions = []
28
+        mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
29
+            self.layer_manager.scrolling_manager.screen_to_world(
30
+                *self.layer_manager.edit_layer.screen_mouse,
31
+            )
32
+        )
33
+
34
+        for actor in self.layer_manager.edit_layer.selection:
35
+            actions.append((
36
+                self.request_move_behaviour_class, {
37
+                    'subject_id': actor.subject.id,
38
+                    'move_to': mouse_grid_position,
39
+                    'gui_action': self.gui_action,
40
+                }
41
+            ))
42
+
43
+        return TerminalPackage(
44
+            simulation_actions=actions,
45
+        )
46
+
47
+
48
+class MoveActorInteraction(BaseMoveActorInteraction):
49
+    gui_action = UserAction.ORDER_MOVE
50
+    color = (0, 0, 255)
51
+
52
+
53
+class MoveFastActorInteraction(BaseMoveActorInteraction):
54
+    gui_action = UserAction.ORDER_MOVE_FAST
55
+    color = (72, 244, 66)
56
+
57
+
58
+class MoveCrawlActorInteraction(BaseMoveActorInteraction):
59
+    gui_action = UserAction.ORDER_MOVE_CRAWL
60
+    color = (235, 244, 66)

+ 0 - 1
sandbox/tile/run.py View File

@@ -8,7 +8,6 @@ from random import seed
8 8
 synergine2_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
9 9
 sys.path.append(synergine2_path)
10 10
 
11
-from synergine2_xyz.move import MoveToMechanism, MoveToBehaviour
12 11
 from sandbox.tile.simulation.subject import Man
13 12
 from sandbox.tile.simulation.base import TileStrategySimulation
14 13
 from sandbox.tile.simulation.base import TileStrategySubjects

+ 29 - 0
sandbox/tile/simulation/behaviour.py View File

@@ -0,0 +1,29 @@
1
+# coding: utf-8
2
+import time
3
+
4
+from sandbox.tile.user_action import UserAction
5
+from synergine2.config import Config
6
+from synergine2.simulation import Simulation
7
+from synergine2.simulation import Subject
8
+from synergine2_xyz.move.simulation import MoveToBehaviour as BaseMoveToBehaviour
9
+
10
+
11
+class MoveToBehaviour(BaseMoveToBehaviour):
12
+    def __init__(
13
+        self,
14
+        config: Config,
15
+        simulation: Simulation,
16
+        subject: Subject,
17
+    ) -> None:
18
+        super().__init__(config, simulation, subject)
19
+        self._walk_duration = float(self.config.resolve('game.move.walk_ref_time'))
20
+        self._run_duration = float(self.config.resolve('game.move.run_ref_time'))
21
+        self._crawl_duration = float(self.config.resolve('game.move.crawl_ref_time'))
22
+
23
+    def _can_move_to_next_step(self, move_to_data: dict) -> bool:
24
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE:
25
+            return time.time() - move_to_data['last_intention_time'] >= self._walk_duration
26
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE_FAST:
27
+            return time.time() - move_to_data['last_intention_time'] >= self._run_duration
28
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE_CRAWL:
29
+            return time.time() - move_to_data['last_intention_time'] >= self._crawl_duration

+ 1 - 1
sandbox/tile/simulation/subject.py View File

@@ -1,7 +1,7 @@
1 1
 # coding: utf-8
2 2
 from sandbox.tile.const import COLLECTION_ALIVE
3 3
 from sandbox.tile.simulation.base import BaseSubject
4
-from synergine2_xyz.move import MoveToBehaviour
4
+from sandbox.tile.simulation.behaviour import MoveToBehaviour
5 5
 
6 6
 
7 7
 class Man(BaseSubject):

+ 2 - 2
sandbox/tile/terminal/base.py View File

@@ -2,8 +2,8 @@
2 2
 from sandbox.tile.simulation.subject import Man as ManSubject
3 3
 from sandbox.tile.gui.actor import Man as ManActor
4 4
 from synergine2_cocos2d.terminal import GameTerminal
5
-from synergine2_xyz.move import FinishMoveEvent
6
-from synergine2_xyz.move import StartMoveEvent
5
+from synergine2_xyz.move.simulation import FinishMoveEvent
6
+from synergine2_xyz.move.simulation import StartMoveEvent
7 7
 
8 8
 
9 9
 class CocosTerminal(GameTerminal):

+ 8 - 0
sandbox/tile/user_action.py View File

@@ -0,0 +1,8 @@
1
+# coding: utf-8
2
+from synergine2_cocos2d.user_action import UserAction as BaseUserAction
3
+
4
+
5
+class UserAction(BaseUserAction):
6
+    ORDER_MOVE = 'ORDER_MOVE'
7
+    ORDER_MOVE_FAST = 'ORDER_MOVE_FAST'
8
+    ORDER_MOVE_CRAWL = 'ORDER_MOVE_CRAWL'

+ 10 - 68
synergine2_cocos2d/gui.py View File

@@ -4,22 +4,17 @@ import weakref
4 4
 from math import floor
5 5
 
6 6
 import pyglet
7
-from pyglet.window import key
8 7
 from pyglet.window import mouse
9 8
 
10 9
 import cocos
11 10
 from cocos import collision_model
12 11
 from cocos import euclid
13 12
 from cocos.layer import ScrollableLayer
14
-from cocos.actions import MoveTo as BaseMoveTo
15 13
 from synergine2.config import Config
16 14
 from synergine2.log import SynergineLogger
17 15
 from synergine2.terminals import Terminal
18 16
 from synergine2.terminals import TerminalPackage
19
-from synergine2_cocos2d.actions import MoveTo
20 17
 from synergine2_cocos2d.actor import Actor
21
-from synergine2_cocos2d.animation import Animate, ANIMATION_CRAWL
22
-from synergine2_cocos2d.animation import ANIMATION_WALK
23 18
 from synergine2_cocos2d.exception import InteractionNotFound
24 19
 from synergine2_cocos2d.exception import OuterWorldPosition
25 20
 from synergine2_cocos2d.gl import draw_rectangle
@@ -29,9 +24,6 @@ from synergine2_cocos2d.layer import LayerManager
29 24
 from synergine2_cocos2d.middleware import MapMiddleware
30 25
 from synergine2_cocos2d.middleware import TMXMiddleware
31 26
 from synergine2_cocos2d.user_action import UserAction
32
-from synergine2_xyz.move import FinishMoveEvent
33
-from synergine2_xyz.move import StartMoveEvent
34
-from synergine2_xyz.utils import get_angle
35 27
 from synergine2_xyz.xyz import XYZSubjectMixin
36 28
 
37 29
 
@@ -139,7 +131,7 @@ class EditLayer(cocos.layer.Layer):
139 131
         mod_restricted_mov=None,
140 132
     ):
141 133
         # TODO: Clean init params
142
-        super(EditLayer, self).__init__()
134
+        super().__init__()
143 135
 
144 136
         self.config = config
145 137
         self.logger = logger
@@ -188,7 +180,7 @@ class EditLayer(cocos.layer.Layer):
188 180
         self.sright = None
189 181
         self.sbottom = None
190 182
         self.s_top = None
191
-        self.user_action_pending = None  # UserAction
183
+        self.user_action_pending = None  # type: UserAction
192 184
 
193 185
         # opers that change cshape must ensure it goes to False,
194 186
         # selection opers must ensure it goes to True
@@ -248,7 +240,7 @@ class EditLayer(cocos.layer.Layer):
248 240
                 pass
249 241
 
250 242
     def on_enter(self):
251
-        super(EditLayer, self).on_enter()
243
+        super().on_enter()
252 244
         scene = self.get_ancestor(cocos.scene.Scene)
253 245
         if self.elastic_box is None:
254 246
             self.elastic_box = MinMaxRect(self.layer_manager)
@@ -392,16 +384,7 @@ class EditLayer(cocos.layer.Layer):
392 384
 
393 385
     def on_key_press(self, k, m):
394 386
         binds = self.bindings
395
-
396
-        # TODO: Clarify code
397
-        # Actions available if actor selected
398
-        if self.selection:
399
-            if k == key.M:
400
-                self.user_action_pending = UserAction.ORDER_MOVE
401
-            if k == key.R:
402
-                self.user_action_pending = UserAction.ORDER_MOVE_FAST
403
-            if k == key.C:
404
-                self.user_action_pending = UserAction.ORDER_MOVE_CRAWL
387
+        self._on_key_press(k, m)
405 388
 
406 389
         if k in binds:
407 390
             self.buttons[binds[k]] = 1
@@ -409,6 +392,9 @@ class EditLayer(cocos.layer.Layer):
409 392
             return True
410 393
         return False
411 394
 
395
+    def _on_key_press(self, k, m):
396
+        pass
397
+
412 398
     def on_key_release(self, k, m):
413 399
         binds = self.bindings
414 400
         if k in binds:
@@ -664,6 +650,8 @@ class SubjectMapperFactory(object):
664 650
 
665 651
 
666 652
 class Gui(object):
653
+    layer_manager_class = LayerManager
654
+
667 655
     def __init__(
668 656
             self,
669 657
             config: Config,
@@ -689,7 +677,7 @@ class Gui(object):
689 677
             logger=self.logger,
690 678
             terminal=self.terminal,
691 679
         )
692
-        self.layer_manager = LayerManager(
680
+        self.layer_manager = self.layer_manager_class(
693 681
             self.config,
694 682
             self.logger,
695 683
             middleware=self.get_layer_middleware(),
@@ -750,21 +738,6 @@ class TMXGui(Gui):
750 738
             read_queue_interval,
751 739
         )
752 740
 
753
-        self.terminal.register_event_handler(
754
-            FinishMoveEvent,
755
-            self.set_subject_position,
756
-        )
757
-
758
-        self.terminal.register_event_handler(
759
-            StartMoveEvent,
760
-            self.start_move_subject,
761
-        )
762
-
763
-        # configs
764
-        self.move_duration_ref = float(self.config.resolve('game.move.walk_ref_time'))
765
-        self.move_fast_duration_ref = float(self.config.resolve('game.move.run_ref_time'))
766
-        self.move_crawl_duration_ref = float(self.config.resolve('game.move.crawl_ref_time'))
767
-
768 741
     def get_layer_middleware(self) -> MapMiddleware:
769 742
         return TMXMiddleware(
770 743
             self.config,
@@ -784,34 +757,3 @@ class TMXGui(Gui):
784 757
     def append_subject(self, subject: XYZSubjectMixin) -> None:
785 758
         subject_mapper = self.subject_mapper_factory.get_subject_mapper(subject)
786 759
         subject_mapper.append(subject, self.layer_manager)
787
-
788
-    def set_subject_position(self, event: FinishMoveEvent):
789
-        actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
790
-        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
791
-
792
-        actor.stop_actions((BaseMoveTo,))
793
-        actor.set_position(*new_world_position)
794
-
795
-    def start_move_subject(self, event: StartMoveEvent):
796
-        actor = self.layer_manager.subject_layer.subjects_index[event.subject_id]
797
-        new_world_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(event.to_position)
798
-
799
-        if event.gui_action == UserAction.ORDER_MOVE:
800
-            animation = ANIMATION_WALK
801
-            cycle_duration = 2
802
-            move_duration = self.move_duration_ref
803
-        elif event.gui_action == UserAction.ORDER_MOVE_FAST:
804
-            animation = ANIMATION_WALK
805
-            cycle_duration = 0.5
806
-            move_duration = self.move_fast_duration_ref
807
-        elif event.gui_action == UserAction.ORDER_MOVE_CRAWL:
808
-            animation = ANIMATION_CRAWL
809
-            cycle_duration = 2
810
-            move_duration = self.move_crawl_duration_ref
811
-        else:
812
-            raise NotImplementedError()
813
-
814
-        move_action = MoveTo(new_world_position, move_duration)
815
-        actor.do(move_action)
816
-        actor.do(Animate(animation, duration=move_duration, cycle_duration=cycle_duration))
817
-        actor.rotation = get_angle(event.from_position, event.to_position)

+ 0 - 57
synergine2_cocos2d/interaction.py View File

@@ -1,14 +1,11 @@
1 1
 # coding: utf-8
2 2
 import typing
3 3
 
4
-from synergine2_xyz.move import RequestMoveBehaviour
5
-
6 4
 from synergine2.config import Config
7 5
 from synergine2.log import SynergineLogger
8 6
 from synergine2.terminals import Terminal
9 7
 from synergine2.terminals import TerminalPackage
10 8
 from synergine2_cocos2d.exception import InteractionNotFound
11
-from synergine2_cocos2d.gl import draw_line
12 9
 from synergine2_cocos2d.layer import LayerManager
13 10
 from synergine2_cocos2d.user_action import UserAction
14 11
 
@@ -68,57 +65,3 @@ class Interaction(object):
68 65
 
69 66
     def get_package_for_terminal(self) -> TerminalPackage:
70 67
         raise NotImplementedError()
71
-
72
-
73
-class BaseMoveActorInteraction(Interaction):
74
-    gui_action = None
75
-    color = None
76
-    request_move_behaviour_class = RequestMoveBehaviour
77
-
78
-    def draw_pending(self) -> None:
79
-            for actor in self.layer_manager.edit_layer.selection:
80
-                grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
81
-                pixel_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(grid_position)
82
-
83
-                draw_line(
84
-                    self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
85
-                    self.layer_manager.edit_layer.screen_mouse,
86
-                    self.color,
87
-                )
88
-
89
-    def get_package_for_terminal(self) -> TerminalPackage:
90
-        # TODO: FinishMoveEvent ?
91
-        actions = []
92
-        mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
93
-            self.layer_manager.scrolling_manager.screen_to_world(
94
-                *self.layer_manager.edit_layer.screen_mouse,
95
-            )
96
-        )
97
-
98
-        for actor in self.layer_manager.edit_layer.selection:
99
-            actions.append((
100
-                self.request_move_behaviour_class, {
101
-                    'subject_id': actor.subject.id,
102
-                    'move_to': mouse_grid_position,
103
-                    'gui_action': self.gui_action,
104
-                }
105
-            ))
106
-
107
-        return TerminalPackage(
108
-            simulation_actions=actions,
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)
120
-
121
-
122
-class MoveCrawlActorInteraction(BaseMoveActorInteraction):
123
-    gui_action = UserAction.ORDER_MOVE_CRAWL
124
-    color = (235, 244, 66)

+ 6 - 2
synergine2_cocos2d/layer.py View File

@@ -58,6 +58,8 @@ class SubjectLayer(cocos.layer.ScrollableLayer):
58 58
 
59 59
 
60 60
 class LayerManager(object):
61
+    edit_layer_class = None  # type: typing.Type['EditLayer']
62
+
61 63
     def __init__(
62 64
         self,
63 65
         config: Config,
@@ -81,10 +83,12 @@ class LayerManager(object):
81 83
         self.subject_layer = None  # type: SubjectLayer
82 84
         self.top_layer = None  # type: cocos.tiles.RectMapLayer
83 85
 
86
+        from synergine2_cocos2d.gui import EditLayer
87
+        self.edit_layer_class = self.edit_layer_class or EditLayer
88
+
84 89
     def init(self) -> None:
85 90
         # TODO: cyclic import
86 91
         from synergine2_cocos2d.gui import MainLayer
87
-        from synergine2_cocos2d.gui import EditLayer
88 92
         from synergine2_cocos2d.gui import GridManager
89 93
 
90 94
         self.middleware.init()
@@ -107,7 +111,7 @@ class LayerManager(object):
107 111
                 'height': 1000,  # Note: world size
108 112
             }
109 113
         )
110
-        self.edit_layer = EditLayer(
114
+        self.edit_layer = self.edit_layer_class(
111 115
             self.config,
112 116
             self.logger,
113 117
             self,

+ 1 - 3
synergine2_cocos2d/user_action.py View File

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

+ 0 - 0
synergine2_xyz/move/__init__.py View File


+ 20 - 0
synergine2_xyz/move/intention.py View File

@@ -0,0 +1,20 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+from synergine2.simulation import Intention
5
+
6
+
7
+class MoveToIntention(Intention):
8
+    def __init__(
9
+        self,
10
+        move_to: typing.Tuple[int, int],
11
+        start_time: float,
12
+        gui_action: typing.Any,
13
+    ) -> None:
14
+        self.move_to = move_to
15
+        self.path = []  # type: typing.List[typing.Tuple[int, int]]
16
+        self.path_progression = -1  # type: int
17
+        self.last_intention_time = start_time
18
+        self.just_reach = False
19
+        self.initial = True
20
+        self.gui_action = gui_action

synergine2_xyz/move.py → synergine2_xyz/move/simulation.py View File

@@ -1,36 +1,17 @@
1 1
 # coding: utf-8
2
-import typing
3
-from random import randint
4
-
5 2
 import time
3
+import typing
6 4
 
7 5
 from synergine2.config import Config
8
-from synergine2.simulation import SimulationBehaviour, Subject
9
-from synergine2.simulation import SubjectBehaviour
10
-from synergine2.simulation import SubjectMechanism
11
-from synergine2.simulation import Intention
6
+from synergine2.simulation import SimulationBehaviour
12 7
 from synergine2.simulation import Simulation
13 8
 from synergine2.simulation import Event
14
-from synergine2_cocos2d.user_action import UserAction
9
+from synergine2.simulation import SubjectMechanism
10
+from synergine2.simulation import SubjectBehaviour
11
+from synergine2_xyz.move.intention import MoveToIntention
15 12
 from synergine2_xyz.simulation import XYZSimulation
16 13
 
17 14
 
18
-class MoveToIntention(Intention):
19
-    def __init__(
20
-        self,
21
-        move_to: typing.Tuple[int, int],
22
-        start_time: float,
23
-        gui_action: typing.Any,
24
-    ) -> None:
25
-        self.move_to = move_to
26
-        self.path = []  # type: typing.List[typing.Tuple[int, int]]
27
-        self.path_progression = -1  # type: int
28
-        self.last_intention_time = start_time
29
-        self.just_reach = False
30
-        self.initial = True
31
-        self.gui_action = gui_action
32
-
33
-
34 15
 class RequestMoveBehaviour(SimulationBehaviour):
35 16
     move_intention_class = MoveToIntention
36 17
 
@@ -172,17 +153,6 @@ class MoveToBehaviour(SubjectBehaviour):
172 153
     use = [MoveToMechanism]
173 154
     move_to_mechanism = MoveToMechanism
174 155
 
175
-    def __init__(
176
-        self,
177
-        config: Config,
178
-        simulation: Simulation,
179
-        subject: Subject,
180
-    ) -> None:
181
-        super().__init__(config, simulation, subject)
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'))
184
-        self._crawl_duration = float(self.config.resolve('game.move.crawl_ref_time'))
185
-
186 156
     def run(self, data):
187 157
         move_to_data = data[self.move_to_mechanism]
188 158
         if move_to_data:
@@ -198,13 +168,7 @@ class MoveToBehaviour(SubjectBehaviour):
198 168
         return False
199 169
 
200 170
     def _can_move_to_next_step(self, move_to_data: dict) -> bool:
201
-        # TODO: Relation vers cocos ! Deplacer UserAction ?
202
-        if move_to_data['gui_action'] == UserAction.ORDER_MOVE:
203
-            return time.time() - move_to_data['last_intention_time'] >= self._walk_duration
204
-        if move_to_data['gui_action'] == UserAction.ORDER_MOVE_FAST:
205
-            return time.time() - move_to_data['last_intention_time'] >= self._run_duration
206
-        if move_to_data['gui_action'] == UserAction.ORDER_MOVE_CRAWL:
207
-            return time.time() - move_to_data['last_intention_time'] >= self._crawl_duration
171
+        raise NotImplementedError()
208 172
 
209 173
     def _is_fresh_new_step(self, move_to_data: dict) -> bool:
210 174
         return move_to_data['just_reach'] or move_to_data['initial']

+ 27 - 5
tests/test_move.py View File

@@ -4,11 +4,35 @@ import time
4 4
 from freezegun import freeze_time
5 5
 
6 6
 from synergine2.config import Config
7
-from synergine2_cocos2d.user_action import UserAction
8
-from synergine2_xyz.move import MoveToBehaviour, MoveToMechanism, MoveToIntention, StartMoveEvent, FinishMoveEvent
7
+from synergine2.simulation import Simulation, Subject
8
+from synergine2_cocos2d.user_action import UserAction as BaseUserAction
9
+from synergine2_xyz.move.intention import MoveToIntention
10
+from synergine2_xyz.move.simulation import MoveToMechanism
11
+from synergine2_xyz.move.simulation import StartMoveEvent
12
+from synergine2_xyz.move.simulation import FinishMoveEvent
9 13
 from synergine2_xyz.simulation import XYZSimulation
10 14
 from synergine2_xyz.subjects import XYZSubject
11 15
 from tests import BaseTest
16
+from synergine2_xyz.move.simulation import MoveToBehaviour as BaseMoveToBehaviour
17
+
18
+
19
+class MyMoveToBehaviour(BaseMoveToBehaviour):
20
+    def __init__(
21
+        self,
22
+        config: Config,
23
+        simulation: Simulation,
24
+        subject: Subject,
25
+    ) -> None:
26
+        super().__init__(config, simulation, subject)
27
+        self._walk_duration = float(self.config.resolve('game.move.walk_ref_time'))
28
+
29
+    def _can_move_to_next_step(self, move_to_data: dict) -> bool:
30
+        if move_to_data['gui_action'] == UserAction.ORDER_MOVE:
31
+            return time.time() - move_to_data['last_intention_time'] >= self._walk_duration
32
+
33
+
34
+class UserAction(BaseUserAction):
35
+    ORDER_MOVE = 'ORDER_MOVE'
12 36
 
13 37
 
14 38
 class MySubject(XYZSubject):
@@ -25,14 +49,12 @@ class TestMove(BaseTest):
25 49
             'game': {
26 50
                 'move': {
27 51
                     'walk_ref_time': 2,
28
-                    'run_ref_time': 2,
29
-                    'crawl_ref_time': 2,
30 52
                 }
31 53
             }
32 54
         })
33 55
         simulation = MySimulation(config)
34 56
         subject = MySubject(config, simulation)
35
-        behaviour = MoveToBehaviour(config, simulation, subject)
57
+        behaviour = MyMoveToBehaviour(config, simulation, subject)
36 58
 
37 59
         with freeze_time("2000-01-01 00:00:00"):
38 60
             move_intention = MoveToIntention((0, 3), time.time(), gui_action=UserAction.ORDER_MOVE)