Browse Source

engulf: grass grow up

Bastien Sevajol 7 years ago
parent
commit
bd85451f5c

+ 23 - 0
sandbox/engulf/behaviour.py View File

@@ -0,0 +1,23 @@
1
+from synergine2.simulation import SubjectBehaviour
2
+from synergine2.simulation import Event
3
+
4
+
5
+class GrassGrownUp(Event):
6
+    def __init__(self, subject_id, density, *args, **kwargs):
7
+        super().__init__(*args, **kwargs)
8
+        self.subject_id = subject_id
9
+        self.density = density
10
+
11
+
12
+class GrowUp(SubjectBehaviour):
13
+    frequency = 20
14
+
15
+    def run(self, data):
16
+        return True
17
+
18
+    def action(self, data) -> [Event]:
19
+        self.subject.density += 1
20
+        return [GrassGrownUp(
21
+            self.subject.id,
22
+            self.subject.density,
23
+        )]

+ 19 - 6
sandbox/engulf/gui.py View File

@@ -2,6 +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 6
 from sandbox.engulf.subject import Cell, Grass
6 7
 from synergine2.terminals import TerminalPackage
7 8
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
@@ -25,17 +26,20 @@ class CellsLayer(GridLayerMixin, BaseMainLayer):
25 26
 class GrassLayer(GridLayerMixin, BaseMainLayer):
26 27
     def __init__(self, *args, **kwargs):
27 28
         super().__init__(*args, **kwargs)
28
-        self.cells = {}
29
+        self.grasses = {}
29 30
 
30
-    def born(self, grid_position, opacity=100):
31
+    def born(self, subject_id, grid_position, opacity=100):
31 32
         grass = Sprite('resources/grass.png')
32 33
         grass.rotation = randint(0, 360)
33 34
         grass.opacity = opacity
34 35
         self.grid_manager.scale_sprite(grass)
35 36
         self.grid_manager.position_sprite(grass, grid_position)
36
-        self.cells[grid_position] = grass
37
+        self.grasses[subject_id] = grass
37 38
         self.add(grass)
38 39
 
40
+    def set_density(self, subject_id, density):
41
+        self.grasses[subject_id].opacity = density
42
+
39 43
 
40 44
 class MainLayer(GridLayerMixin, BaseMainLayer):
41 45
     def __init__(self, terminal, *args, **kwargs):
@@ -54,7 +58,11 @@ class Game(Gui):
54 58
 
55 59
         self.main_layer = MainLayer(terminal=self.terminal)
56 60
         self.main_scene = cocos.scene.Scene(self.main_layer)
57
-        self.positions = {}
61
+
62
+        self.terminal.register_event_handler(
63
+            GrassGrownUp,
64
+            self.on_grass_grown_up,
65
+        )
58 66
 
59 67
     def get_main_scene(self):
60 68
         return self.main_scene
@@ -63,11 +71,16 @@ class Game(Gui):
63 71
         if package.subjects:  # It's thirst package
64 72
             for subject in package.subjects:
65 73
                 if isinstance(subject, Cell):
66
-                    self.positions[subject.id] = subject.position
67 74
                     self.main_layer.cells.born(subject.position)
68 75
                 if isinstance(subject, Grass):
69
-                    self.positions[subject.id] = subject.position
70 76
                     self.main_layer.grasses.born(
77
+                        subject.id,
71 78
                         subject.position,
72 79
                         subject.density,
73 80
                     )
81
+
82
+    def on_grass_grown_up(self, event: GrassGrownUp):
83
+        self.main_layer.grasses.set_density(
84
+            event.subject_id,
85
+            event.density,
86
+        )

+ 4 - 1
sandbox/engulf/run.py View File

@@ -19,6 +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 23
 
23 24
 from sandbox.engulf.subject import Cell, Grass, COLLECTION_GRASS
24 25
 from synergine2.core import Core
@@ -34,7 +35,9 @@ class Engulf(Simulation):
34 35
 
35 36
 
36 37
 class GameTerminal(Terminal):
37
-    subscribed_events = []
38
+    subscribed_events = [
39
+        GrassGrownUp,
40
+    ]
38 41
 
39 42
     def __init__(self):
40 43
         super().__init__()

+ 4 - 0
sandbox/engulf/subject.py View File

@@ -1,3 +1,4 @@
1
+from sandbox.engulf.behaviour import GrowUp
1 2
 from synergine2.simulation import Subject
2 3
 from synergine2.xyz import XYZSubjectMixin
3 4
 
@@ -20,6 +21,9 @@ class Grass(XYZSubjectMixin, Subject):
20 21
         COLLECTION_EATABLE,
21 22
         COLLECTION_GRASS,
22 23
     ]
24
+    behaviours_classes = [
25
+        GrowUp,
26
+    ]
23 27
 
24 28
     def __init__(self, *args, **kwargs):
25 29
         super().__init__(*args, **kwargs)

+ 11 - 3
synergine2/cycle.py View File

@@ -23,7 +23,7 @@ class CycleManager(object):
23 23
 
24 24
         self.simulation = simulation
25 25
         self.process_manager = process_manager
26
-        self.current_cycle = 0
26
+        self.current_cycle = -1
27 27
         self.first_cycle = True
28 28
 
29 29
     def next(self) -> [Event]:
@@ -31,6 +31,7 @@ class CycleManager(object):
31 31
             # To dispatch subjects add/removes, enable track on them
32 32
             self.simulation.subjects.track_changes = True
33 33
             self.first_cycle = False
34
+        self.current_cycle += 1
34 35
 
35 36
         events = []
36 37
         # TODO: gestion des behaviours non parallelisables
@@ -71,7 +72,7 @@ class CycleManager(object):
71 72
         for process_results in results_by_processes:
72 73
             results.update(process_results)
73 74
         for subject in self.simulation.subjects[:]:  # Duplicate list to prevent conflicts with behaviours subjects manipulations
74
-            for behaviour_class, behaviour_data in results[subject.id].items():
75
+            for behaviour_class, behaviour_data in results.get(subject.id, {}).items():
75 76
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
76 77
                 # (genre se cacher et fuir son pas compatibles)
77 78
                 actions_events = subject.behaviours[behaviour_class].action(behaviour_data)
@@ -133,7 +134,14 @@ class CycleManager(object):
133 134
 
134 135
     def get_behaviours_to_compute(self, mechanisable) -> [SubjectBehaviour, SimulationBehaviour]:
135 136
         # TODO: Implementer un systeme qui inhibe des behaviours (ex. someil inhibe avoir faim)
136
-        return mechanisable.behaviours.values()
137
+        behaviours = list(mechanisable.behaviours.values())
138
+
139
+        for behaviour in behaviours[:]:
140
+            if behaviour.frequency != 1:
141
+                if self.current_cycle % behaviour.frequency:
142
+                    behaviours.remove(behaviour)
143
+
144
+        return behaviours
137 145
 
138 146
     def apply_actions(
139 147
             self,

+ 6 - 0
synergine2/simulation.py View File

@@ -135,6 +135,9 @@ class Event(object):
135 135
 
136 136
 
137 137
 class SubjectBehaviour(object):
138
+    frequency = 1
139
+    use = []
140
+
138 141
     def __init__(
139 142
             self,
140 143
             simulation: Simulation,
@@ -163,6 +166,9 @@ class SubjectBehaviour(object):
163 166
 
164 167
 
165 168
 class SimulationBehaviour(object):
169
+    frequency = 1
170
+    use = []
171
+
166 172
     def __init__(
167 173
             self,
168 174
             simulation: Simulation,