Bastien Sevajol 7 年之前
父節點
當前提交
e290e2374c

+ 17 - 16
sandbox/tile/gui/base.py 查看文件

9
 
9
 
10
 class Game(TMXGui):
10
 class Game(TMXGui):
11
     def before_run(self) -> None:
11
     def before_run(self) -> None:
12
+        pass
12
         # Test
13
         # Test
13
-        from sandbox.tile.gui.actor import Man
14
-        from cocos import euclid
15
-
16
-        for i in range(10):
17
-            x = random.randint(0, 600)
18
-            y = random.randint(0, 300)
19
-            man = Man()
20
-            man.update_position(euclid.Vector2(x, y))
21
-            self.layer_manager.add_subject(man)
22
-            self.layer_manager.set_selectable(man)
23
-            man.scale = 1
24
-
25
-            if x % 2:
26
-                man.do(Animate(ANIMATION_WALK, 10, 4))
27
-            else:
28
-                man.do(Animate(ANIMATION_CRAWL, 20, 4))
14
+        # from sandbox.tile.gui.actor import Man
15
+        # from cocos import euclid
16
+        #
17
+        # for i in range(10):
18
+        #     x = random.randint(0, 600)
19
+        #     y = random.randint(0, 300)
20
+        #     man = Man()
21
+        #     man.update_position(euclid.Vector2(x, y))
22
+        #     self.layer_manager.add_subject(man)
23
+        #     self.layer_manager.set_selectable(man)
24
+        #     man.scale = 1
25
+        #
26
+        #     if x % 2:
27
+        #         man.do(Animate(ANIMATION_WALK, 10, 4))
28
+        #     else:
29
+        #         man.do(Animate(ANIMATION_CRAWL, 20, 4))

+ 16 - 5
sandbox/tile/run.py 查看文件

5
 import logging
5
 import logging
6
 from random import seed
6
 from random import seed
7
 
7
 
8
+from sandbox.tile.simulation.subject import Man
9
+
8
 synergine2_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
10
 synergine2_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
9
 sys.path.append(synergine2_path)
11
 sys.path.append(synergine2_path)
10
 
12
 
11
-from sandbox.tile.simulation.base import TiledStrategySimulation, TiledStrategySubjects
13
+from sandbox.tile.simulation.base import TileStrategySimulation
14
+from sandbox.tile.simulation.base import TileStrategySubjects
12
 from synergine2.log import get_default_logger
15
 from synergine2.log import get_default_logger
13
 from synergine2.config import Config
16
 from synergine2.config import Config
14
 from sandbox.tile.terminal.base import CocosTerminal
17
 from sandbox.tile.terminal.base import CocosTerminal
24
     config.load_files(['sandbox/engulf/config.yaml'])
27
     config.load_files(['sandbox/engulf/config.yaml'])
25
     logger = get_default_logger(level=logging.ERROR)
28
     logger = get_default_logger(level=logging.ERROR)
26
 
29
 
27
-    simulation = TiledStrategySimulation(config)
28
-    subjects = TiledStrategySubjects(simulation=simulation)
29
-    # TODO: Create subjects
30
+    simulation = TileStrategySimulation(config)
31
+    subjects = TileStrategySubjects(simulation=simulation)
32
+
33
+    man = Man(
34
+        config=config,
35
+        simulation=simulation,
36
+        position=(0, 0),
37
+    )
38
+    man.position = 0, 0
39
+    subjects.append(man)
40
+
30
     simulation.subjects = subjects
41
     simulation.subjects = subjects
31
 
42
 
32
     core = Core(
43
     core = Core(
53
     core.run()
64
     core.run()
54
 
65
 
55
 if __name__ == '__main__':
66
 if __name__ == '__main__':
56
-    parser = argparse.ArgumentParser(description='Run TiledStrategy')
67
+    parser = argparse.ArgumentParser(description='Run TileStrategy')
57
     parser.add_argument('map_dir_path', help='map directory path')
68
     parser.add_argument('map_dir_path', help='map directory path')
58
     parser.add_argument('--seed', dest='seed', default=42)
69
     parser.add_argument('--seed', dest='seed', default=42)
59
 
70
 

+ 8 - 2
sandbox/tile/simulation/base.py 查看文件

1
 # coding: utf-8
1
 # coding: utf-8
2
+from synergine2.simulation import Subject
2
 from synergine2.xyz import XYZSimulation
3
 from synergine2.xyz import XYZSimulation
4
+from synergine2.xyz import XYZSubjectMixin
3
 from synergine2.xyz import XYZSubjects
5
 from synergine2.xyz import XYZSubjects
4
 
6
 
5
 
7
 
6
-class TiledStrategySimulation(XYZSimulation):
8
+class TileStrategySimulation(XYZSimulation):
7
     behaviours_classes = [
9
     behaviours_classes = [
8
 
10
 
9
     ]
11
     ]
10
 
12
 
11
 
13
 
12
-class TiledStrategySubjects(XYZSubjects):
14
+class TileStrategySubjects(XYZSubjects):
15
+    pass
16
+
17
+
18
+class BaseSubject(XYZSubjectMixin, Subject):
13
     pass
19
     pass

+ 6 - 0
sandbox/tile/simulation/subject.py 查看文件

1
+# coding: utf-8
2
+from sandbox.tile.simulation.base import BaseSubject
3
+
4
+
5
+class Man(BaseSubject):
6
+    pass

+ 13 - 4
sandbox/tile/terminal/base.py 查看文件

1
 # coding: utf-8
1
 # coding: utf-8
2
+from sandbox.tile.simulation.subject import Man as ManSubject
3
+from sandbox.tile.gui.actor import Man as ManActor
4
+from synergine2_cocos2d.terminal import GameTerminal
2
 
5
 
3
-from synergine2.terminals import Terminal
4
 
6
 
5
-
6
-class CocosTerminal(Terminal):
7
+class CocosTerminal(GameTerminal):
7
     subscribed_events = [
8
     subscribed_events = [
8
 
9
 
9
     ]
10
     ]
11
     def __init__(self, *args, asynchronous: bool, map_dir_path: str, **kwargs):
12
     def __init__(self, *args, asynchronous: bool, map_dir_path: str, **kwargs):
12
         super().__init__(*args, **kwargs)
13
         super().__init__(*args, **kwargs)
13
         self.asynchronous = asynchronous
14
         self.asynchronous = asynchronous
14
-        self.gui = None
15
         self.map_dir_path = map_dir_path
15
         self.map_dir_path = map_dir_path
16
 
16
 
17
     def run(self):
17
     def run(self):
18
         from sandbox.tile.gui.base import Game
18
         from sandbox.tile.gui.base import Game
19
+        from synergine2_cocos2d.gui import SubjectMapper
20
+
19
         self.gui = Game(
21
         self.gui = Game(
20
             self.config,
22
             self.config,
21
             self.logger,
23
             self.logger,
22
             self,
24
             self,
23
             map_dir_path=self.map_dir_path,
25
             map_dir_path=self.map_dir_path,
24
         )
26
         )
27
+
28
+        # TODO: Defind on some other place ?
29
+        self.gui.subject_mapper_factory.register_mapper(
30
+            ManSubject,
31
+            SubjectMapper(ManActor),
32
+        )
33
+
25
         self.gui.run()
34
         self.gui.run()

+ 56 - 1
synergine2_cocos2d/gui.py 查看文件

15
 from cocos.sprite import Sprite
15
 from cocos.sprite import Sprite
16
 from synergine2.config import Config
16
 from synergine2.config import Config
17
 from synergine2.log import SynergineLogger
17
 from synergine2.log import SynergineLogger
18
+from synergine2.simulation import Subject
18
 from synergine2.terminals import Terminal
19
 from synergine2.terminals import Terminal
19
 from synergine2.terminals import TerminalPackage
20
 from synergine2.terminals import TerminalPackage
21
+from synergine2.xyz import XYZSubjectMixin
20
 from synergine2_cocos2d.actor import Actor
22
 from synergine2_cocos2d.actor import Actor
21
 from synergine2_cocos2d.exception import OuterWorldPosition
23
 from synergine2_cocos2d.exception import OuterWorldPosition
22
 from synergine2_cocos2d.layer import LayerManager
24
 from synergine2_cocos2d.layer import LayerManager
601
         self.px_height = height
603
         self.px_height = height
602
 
604
 
603
 
605
 
606
+class SubjectMapper(object):
607
+    def __init__(
608
+        self,
609
+        actor_class: typing.Type[Actor],
610
+    ) -> None:
611
+        self.actor_class = actor_class
612
+
613
+    def append(
614
+        self,
615
+        subject: XYZSubjectMixin,
616
+        layer_manager: LayerManager,
617
+    ) -> None:
618
+        actor = self.actor_class()
619
+        pixel_position = layer_manager.grid_manager.get_pixel_position_of_grid_position((
620
+            subject.position[0],
621
+            subject.position[1],
622
+        ))
623
+        actor.update_position(euclid.Vector2(*pixel_position))
624
+
625
+        # TODO: Selectable nature must be configurable
626
+        layer_manager.add_subject(actor)
627
+        layer_manager.set_selectable(actor)
628
+
629
+
630
+class SubjectMapperFactory(object):
631
+    def __init__(self) -> None:
632
+        self.mapping = {}  # type: typing.Dict[typing.Type[XYZSubjectMixin], SubjectMapper]
633
+
634
+    def register_mapper(self, subject_class: typing.Type[XYZSubjectMixin], mapper: SubjectMapper) -> None:
635
+        if subject_class not in self.mapping:
636
+            self.mapping[subject_class] = mapper
637
+        else:
638
+            raise ValueError('subject_class already register with "{}"'.format(str(self.mapping[subject_class])))
639
+
640
+    def get_subject_mapper(self, subject: XYZSubjectMixin) -> SubjectMapper:
641
+        for subject_class, mapper in self.mapping.items():
642
+            if isinstance(subject, subject_class):
643
+                return mapper
644
+        raise KeyError('No mapper for subject "{}"'.format(str(subject)))
645
+
646
+
604
 class Gui(object):
647
 class Gui(object):
605
     def __init__(
648
     def __init__(
606
             self,
649
             self,
630
         pyglet.gl.glEnable(pyglet.gl.GL_ALPHA_TEST)
673
         pyglet.gl.glEnable(pyglet.gl.GL_ALPHA_TEST)
631
         pyglet.gl.glAlphaFunc(pyglet.gl.GL_GREATER, .1)
674
         pyglet.gl.glAlphaFunc(pyglet.gl.GL_GREATER, .1)
632
 
675
 
676
+        self.subject_mapper_factory = SubjectMapperFactory()
677
+
633
     def run(self):
678
     def run(self):
634
         self.before_run()
679
         self.before_run()
635
         pyglet.clock.schedule_interval(
680
         pyglet.clock.schedule_interval(
681
         self.layer_manager.center()
726
         self.layer_manager.center()
682
 
727
 
683
     def get_main_scene(self) -> cocos.cocosnode.CocosNode:
728
     def get_main_scene(self) -> cocos.cocosnode.CocosNode:
684
-        return self.layer_manager.main_scene
729
+        return self.layer_manager.main_scene
730
+
731
+    def before_received(self, package: TerminalPackage):
732
+        super().before_received(package)
733
+        if package.subjects:  # They are new subjects in the simulation
734
+            for subject in package.subjects:
735
+                self.append_subject(subject)
736
+
737
+    def append_subject(self, subject: XYZSubjectMixin) -> None:
738
+        subject_mapper = self.subject_mapper_factory.get_subject_mapper(subject)
739
+        subject_mapper.append(subject, self.layer_manager)

+ 16 - 0
synergine2_cocos2d/terminal.py 查看文件

1
+# coding: utf-8
2
+from synergine2.terminals import Terminal, TerminalPackage
3
+
4
+
5
+class GameTerminal(Terminal):
6
+    def __init__(self, *args, **kwargs):
7
+        super().__init__(*args, **kwargs)
8
+        self.gui = None
9
+
10
+    def receive(self, package: TerminalPackage):
11
+        self.gui.before_received(package)
12
+        super().receive(package)
13
+        self.gui.after_received(package)
14
+
15
+    def run(self):
16
+        raise NotImplementedError()