Bastien Sevajol 6 jaren geleden
bovenliggende
commit
e290e2374c

+ 17 - 16
sandbox/tile/gui/base.py Bestand weergeven

@@ -9,20 +9,21 @@ from synergine2_cocos2d.gui import TMXGui
9 9
 
10 10
 class Game(TMXGui):
11 11
     def before_run(self) -> None:
12
+        pass
12 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 Bestand weergeven

@@ -5,10 +5,13 @@ import sys
5 5
 import logging
6 6
 from random import seed
7 7
 
8
+from sandbox.tile.simulation.subject import Man
9
+
8 10
 synergine2_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
9 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 15
 from synergine2.log import get_default_logger
13 16
 from synergine2.config import Config
14 17
 from sandbox.tile.terminal.base import CocosTerminal
@@ -24,9 +27,17 @@ def main(map_dir_path: str, seed_value: int=42):
24 27
     config.load_files(['sandbox/engulf/config.yaml'])
25 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 41
     simulation.subjects = subjects
31 42
 
32 43
     core = Core(
@@ -53,7 +64,7 @@ def main(map_dir_path: str, seed_value: int=42):
53 64
     core.run()
54 65
 
55 66
 if __name__ == '__main__':
56
-    parser = argparse.ArgumentParser(description='Run TiledStrategy')
67
+    parser = argparse.ArgumentParser(description='Run TileStrategy')
57 68
     parser.add_argument('map_dir_path', help='map directory path')
58 69
     parser.add_argument('--seed', dest='seed', default=42)
59 70
 

+ 8 - 2
sandbox/tile/simulation/base.py Bestand weergeven

@@ -1,13 +1,19 @@
1 1
 # coding: utf-8
2
+from synergine2.simulation import Subject
2 3
 from synergine2.xyz import XYZSimulation
4
+from synergine2.xyz import XYZSubjectMixin
3 5
 from synergine2.xyz import XYZSubjects
4 6
 
5 7
 
6
-class TiledStrategySimulation(XYZSimulation):
8
+class TileStrategySimulation(XYZSimulation):
7 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 19
     pass

+ 6 - 0
sandbox/tile/simulation/subject.py Bestand weergeven

@@ -0,0 +1,6 @@
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 Bestand weergeven

@@ -1,9 +1,10 @@
1 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 8
     subscribed_events = [
8 9
 
9 10
     ]
@@ -11,15 +12,23 @@ class CocosTerminal(Terminal):
11 12
     def __init__(self, *args, asynchronous: bool, map_dir_path: str, **kwargs):
12 13
         super().__init__(*args, **kwargs)
13 14
         self.asynchronous = asynchronous
14
-        self.gui = None
15 15
         self.map_dir_path = map_dir_path
16 16
 
17 17
     def run(self):
18 18
         from sandbox.tile.gui.base import Game
19
+        from synergine2_cocos2d.gui import SubjectMapper
20
+
19 21
         self.gui = Game(
20 22
             self.config,
21 23
             self.logger,
22 24
             self,
23 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 34
         self.gui.run()

+ 56 - 1
synergine2_cocos2d/gui.py Bestand weergeven

@@ -15,8 +15,10 @@ from cocos.layer import ScrollableLayer
15 15
 from cocos.sprite import Sprite
16 16
 from synergine2.config import Config
17 17
 from synergine2.log import SynergineLogger
18
+from synergine2.simulation import Subject
18 19
 from synergine2.terminals import Terminal
19 20
 from synergine2.terminals import TerminalPackage
21
+from synergine2.xyz import XYZSubjectMixin
20 22
 from synergine2_cocos2d.actor import Actor
21 23
 from synergine2_cocos2d.exception import OuterWorldPosition
22 24
 from synergine2_cocos2d.layer import LayerManager
@@ -601,6 +603,47 @@ class MainLayer(ScrollableLayer):
601 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 647
 class Gui(object):
605 648
     def __init__(
606 649
             self,
@@ -630,6 +673,8 @@ class Gui(object):
630 673
         pyglet.gl.glEnable(pyglet.gl.GL_ALPHA_TEST)
631 674
         pyglet.gl.glAlphaFunc(pyglet.gl.GL_GREATER, .1)
632 675
 
676
+        self.subject_mapper_factory = SubjectMapperFactory()
677
+
633 678
     def run(self):
634 679
         self.before_run()
635 680
         pyglet.clock.schedule_interval(
@@ -681,4 +726,14 @@ class TMXGui(Gui):
681 726
         self.layer_manager.center()
682 727
 
683 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 Bestand weergeven

@@ -0,0 +1,16 @@
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()