Pārlūkot izejas kodu

engulf: grass grow up

Bastien Sevajol 8 gadus atpakaļ
vecāks
revīzija
bd85451f5c

+ 23 - 0
sandbox/engulf/behaviour.py Parādīt failu

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 Parādīt failu

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.subject import Cell, Grass
6
 from sandbox.engulf.subject import Cell, Grass
6
 from synergine2.terminals import TerminalPackage
7
 from synergine2.terminals import TerminalPackage
7
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
8
 from synergine2_cocos2d.gui import Gui, GridLayerMixin
25
 class GrassLayer(GridLayerMixin, BaseMainLayer):
26
 class GrassLayer(GridLayerMixin, BaseMainLayer):
26
     def __init__(self, *args, **kwargs):
27
     def __init__(self, *args, **kwargs):
27
         super().__init__(*args, **kwargs)
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
         grass = Sprite('resources/grass.png')
32
         grass = Sprite('resources/grass.png')
32
         grass.rotation = randint(0, 360)
33
         grass.rotation = randint(0, 360)
33
         grass.opacity = opacity
34
         grass.opacity = opacity
34
         self.grid_manager.scale_sprite(grass)
35
         self.grid_manager.scale_sprite(grass)
35
         self.grid_manager.position_sprite(grass, grid_position)
36
         self.grid_manager.position_sprite(grass, grid_position)
36
-        self.cells[grid_position] = grass
37
+        self.grasses[subject_id] = grass
37
         self.add(grass)
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
 class MainLayer(GridLayerMixin, BaseMainLayer):
44
 class MainLayer(GridLayerMixin, BaseMainLayer):
41
     def __init__(self, terminal, *args, **kwargs):
45
     def __init__(self, terminal, *args, **kwargs):
54
 
58
 
55
         self.main_layer = MainLayer(terminal=self.terminal)
59
         self.main_layer = MainLayer(terminal=self.terminal)
56
         self.main_scene = cocos.scene.Scene(self.main_layer)
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
     def get_main_scene(self):
67
     def get_main_scene(self):
60
         return self.main_scene
68
         return self.main_scene
63
         if package.subjects:  # It's thirst package
71
         if package.subjects:  # It's thirst package
64
             for subject in package.subjects:
72
             for subject in package.subjects:
65
                 if isinstance(subject, Cell):
73
                 if isinstance(subject, Cell):
66
-                    self.positions[subject.id] = subject.position
67
                     self.main_layer.cells.born(subject.position)
74
                     self.main_layer.cells.born(subject.position)
68
                 if isinstance(subject, Grass):
75
                 if isinstance(subject, Grass):
69
-                    self.positions[subject.id] = subject.position
70
                     self.main_layer.grasses.born(
76
                     self.main_layer.grasses.born(
77
+                        subject.id,
71
                         subject.position,
78
                         subject.position,
72
                         subject.density,
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 Parādīt failu

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

+ 4 - 0
sandbox/engulf/subject.py Parādīt failu

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

+ 11 - 3
synergine2/cycle.py Parādīt failu

23
 
23
 
24
         self.simulation = simulation
24
         self.simulation = simulation
25
         self.process_manager = process_manager
25
         self.process_manager = process_manager
26
-        self.current_cycle = 0
26
+        self.current_cycle = -1
27
         self.first_cycle = True
27
         self.first_cycle = True
28
 
28
 
29
     def next(self) -> [Event]:
29
     def next(self) -> [Event]:
31
             # To dispatch subjects add/removes, enable track on them
31
             # To dispatch subjects add/removes, enable track on them
32
             self.simulation.subjects.track_changes = True
32
             self.simulation.subjects.track_changes = True
33
             self.first_cycle = False
33
             self.first_cycle = False
34
+        self.current_cycle += 1
34
 
35
 
35
         events = []
36
         events = []
36
         # TODO: gestion des behaviours non parallelisables
37
         # TODO: gestion des behaviours non parallelisables
71
         for process_results in results_by_processes:
72
         for process_results in results_by_processes:
72
             results.update(process_results)
73
             results.update(process_results)
73
         for subject in self.simulation.subjects[:]:  # Duplicate list to prevent conflicts with behaviours subjects manipulations
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
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
76
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
76
                 # (genre se cacher et fuir son pas compatibles)
77
                 # (genre se cacher et fuir son pas compatibles)
77
                 actions_events = subject.behaviours[behaviour_class].action(behaviour_data)
78
                 actions_events = subject.behaviours[behaviour_class].action(behaviour_data)
133
 
134
 
134
     def get_behaviours_to_compute(self, mechanisable) -> [SubjectBehaviour, SimulationBehaviour]:
135
     def get_behaviours_to_compute(self, mechanisable) -> [SubjectBehaviour, SimulationBehaviour]:
135
         # TODO: Implementer un systeme qui inhibe des behaviours (ex. someil inhibe avoir faim)
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
     def apply_actions(
146
     def apply_actions(
139
             self,
147
             self,

+ 6 - 0
synergine2/simulation.py Parādīt failu

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