Bastien Sevajol 7 år sedan
förälder
incheckning
bbede6920e
4 ändrade filer med 99 tillägg och 7 borttagningar
  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 Visa fil

@@ -1,5 +1,7 @@
1
-from synergine2.simulation import SubjectBehaviour
1
+from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
2 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 7
 class GrassGrownUp(Event):
@@ -9,6 +11,40 @@ class GrassGrownUp(Event):
9 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 48
 class GrowUp(SubjectBehaviour):
13 49
     frequency = 20
14 50
 
@@ -21,3 +57,45 @@ class GrowUp(SubjectBehaviour):
21 57
             self.subject.id,
22 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 Visa fil

@@ -2,7 +2,7 @@ from random import randint
2 2
 
3 3
 import cocos
4 4
 from cocos.sprite import Sprite
5
-from sandbox.engulf.behaviour import GrassGrownUp
5
+from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn
6 6
 from sandbox.engulf.subject import Cell, Grass
7 7
 from synergine2.terminals import TerminalPackage
8 8
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
@@ -63,6 +63,10 @@ class Game(Gui):
63 63
             GrassGrownUp,
64 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 71
     def get_main_scene(self):
68 72
         return self.main_scene
@@ -79,8 +83,15 @@ class Game(Gui):
79 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 93
     def on_grass_grown_up(self, event: GrassGrownUp):
83 94
         self.main_layer.grasses.set_density(
84 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 Visa fil

@@ -19,7 +19,7 @@ Engulf is simulation containing:
19 19
 
20 20
 """
21 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 24
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
25 25
 from synergine2.core import Core
@@ -31,12 +31,15 @@ from synergine2.xyz_utils import get_around_positions_of, get_distance_between_p
31 31
 
32 32
 
33 33
 class Engulf(Simulation):
34
-    pass
34
+    behaviours_classes = [
35
+        GrassSpawnBehaviour,
36
+    ]
35 37
 
36 38
 
37 39
 class GameTerminal(Terminal):
38 40
     subscribed_events = [
39 41
         GrassGrownUp,
42
+        GrassSpawn,
40 43
     ]
41 44
 
42 45
     def __init__(self):

+ 2 - 2
sandbox/engulf/subject.py Visa fil

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