瀏覽代碼

grass spawn

Bastien Sevajol 8 年之前
父節點
當前提交
bbede6920e
共有 4 個文件被更改,包括 99 次插入7 次删除
  1. 79 1
      sandbox/engulf/behaviour.py
  2. 13 2
      sandbox/engulf/gui.py
  3. 5 2
      sandbox/engulf/run.py
  4. 2 2
      sandbox/engulf/subject.py

+ 79 - 1
sandbox/engulf/behaviour.py 查看文件

1
-from synergine2.simulation import SubjectBehaviour
1
+from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
2
 from synergine2.simulation import Event
2
 from synergine2.simulation import Event
3
+from synergine2.utils import ChunkManager
4
+from synergine2.xyz_utils import get_around_positions_of_positions
3
 
5
 
4
 
6
 
5
 class GrassGrownUp(Event):
7
 class GrassGrownUp(Event):
9
         self.density = density
11
         self.density = density
10
 
12
 
11
 
13
 
14
+class GrassSpawn(Event):
15
+    def __init__(self, subject_id, position, density, *args, **kwargs):
16
+        super().__init__(*args, **kwargs)
17
+        self.subject_id = subject_id
18
+        self.position = position
19
+        self.density = density
20
+
21
+
22
+class GrassSpotablePositionsMechanism(SimulationMechanism):
23
+    parallelizable = True
24
+
25
+    def run(self, process_number: int=None, process_count: int=None):
26
+        chunk_manager = ChunkManager(process_count)
27
+        positions = list(self.simulation.subjects.grass_xyz.keys())
28
+        positions_chunks = chunk_manager.make_chunks(positions)
29
+        spotables = []
30
+
31
+        for position in positions_chunks[process_number]:
32
+            arounds = get_around_positions_of_positions(position)
33
+            from_subject = self.simulation.subjects.grass_xyz[position]
34
+            around_data = {
35
+                'from_subject': from_subject,
36
+                'around': [],
37
+            }
38
+            for around in arounds:
39
+                if around not in self.simulation.subjects.grass_xyz:
40
+                    around_data['around'].append(around)
41
+
42
+            if around_data['around']:
43
+                spotables.append(around_data)
44
+
45
+        return spotables
46
+
47
+
12
 class GrowUp(SubjectBehaviour):
48
 class GrowUp(SubjectBehaviour):
13
     frequency = 20
49
     frequency = 20
14
 
50
 
21
             self.subject.id,
57
             self.subject.id,
22
             self.subject.density,
58
             self.subject.density,
23
         )]
59
         )]
60
+
61
+
62
+class GrassSpawnBehaviour(SimulationBehaviour):
63
+    frequency = 100
64
+    use = [GrassSpotablePositionsMechanism]
65
+
66
+    def run(self, data):
67
+        spawns = []
68
+
69
+        for around_data in data[GrassSpotablePositionsMechanism]:
70
+            from_subject = around_data['from_subject']
71
+            arounds = around_data['around']
72
+            if from_subject.density >= 40:
73
+                spawns.extend(arounds)
74
+
75
+        return spawns
76
+
77
+    @classmethod
78
+    def merge_data(cls, new_data, start_data=None):
79
+        start_data = start_data or []
80
+        start_data.extend(new_data)
81
+        return start_data
82
+
83
+    def action(self, data) -> [Event]:
84
+        from sandbox.engulf.subject import Grass  # cyclic
85
+        events = []
86
+
87
+        for position in data:
88
+            if position not in list(self.simulation.subjects.grass_xyz.keys()):
89
+                new_grass = Grass(
90
+                    self.simulation,
91
+                    position=position,
92
+                    density=20,
93
+                )
94
+                self.simulation.subjects.append(new_grass)
95
+                events.append(GrassSpawn(
96
+                    new_grass.id,
97
+                    new_grass.position,
98
+                    new_grass.density,
99
+                ))
100
+
101
+        return events

+ 13 - 2
sandbox/engulf/gui.py 查看文件

2
 
2
 
3
 import cocos
3
 import cocos
4
 from cocos.sprite import Sprite
4
 from cocos.sprite import Sprite
5
-from sandbox.engulf.behaviour import GrassGrownUp
5
+from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn
6
 from sandbox.engulf.subject import Cell, Grass
6
 from sandbox.engulf.subject import Cell, Grass
7
 from synergine2.terminals import TerminalPackage
7
 from synergine2.terminals import TerminalPackage
8
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
8
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
63
             GrassGrownUp,
63
             GrassGrownUp,
64
             self.on_grass_grown_up,
64
             self.on_grass_grown_up,
65
         )
65
         )
66
+        self.terminal.register_event_handler(
67
+            GrassSpawn,
68
+            self.on_grass_spawn,
69
+        )
66
 
70
 
67
     def get_main_scene(self):
71
     def get_main_scene(self):
68
         return self.main_scene
72
         return self.main_scene
79
                         subject.density,
83
                         subject.density,
80
                     )
84
                     )
81
 
85
 
86
+    def on_grass_spawn(self, event: GrassSpawn):
87
+        self.main_layer.grasses.born(
88
+            event.subject_id,
89
+            event.position,
90
+            event.density,
91
+        )
92
+
82
     def on_grass_grown_up(self, event: GrassGrownUp):
93
     def on_grass_grown_up(self, event: GrassGrownUp):
83
         self.main_layer.grasses.set_density(
94
         self.main_layer.grasses.set_density(
84
             event.subject_id,
95
             event.subject_id,
85
-            event.density,
96
+            event.density,  # TODO: Recupe ces données depuis local plutôt que event ?
86
         )
97
         )

+ 5 - 2
sandbox/engulf/run.py 查看文件

19
 
19
 
20
 """
20
 """
21
 from random import randint, seed
21
 from random import randint, seed
22
-from sandbox.engulf.behaviour import GrassGrownUp
22
+from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, GrassSpawnBehaviour
23
 
23
 
24
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
24
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
25
 from synergine2.core import Core
25
 from synergine2.core import Core
31
 
31
 
32
 
32
 
33
 class Engulf(Simulation):
33
 class Engulf(Simulation):
34
-    pass
34
+    behaviours_classes = [
35
+        GrassSpawnBehaviour,
36
+    ]
35
 
37
 
36
 
38
 
37
 class GameTerminal(Terminal):
39
 class GameTerminal(Terminal):
38
     subscribed_events = [
40
     subscribed_events = [
39
         GrassGrownUp,
41
         GrassGrownUp,
42
+        GrassSpawn,
40
     ]
43
     ]
41
 
44
 
42
     def __init__(self):
45
     def __init__(self):

+ 2 - 2
sandbox/engulf/subject.py 查看文件

25
         GrowUp,
25
         GrowUp,
26
     ]
26
     ]
27
 
27
 
28
-    def __init__(self, *args, **kwargs):
28
+    def __init__(self, *args, density=100.0, **kwargs):
29
         super().__init__(*args, **kwargs)
29
         super().__init__(*args, **kwargs)
30
-        self._density = 100.0
30
+        self._density = density
31
 
31
 
32
     @property
32
     @property
33
     def density(self) -> float:
33
     def density(self) -> float: