|
@@ -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:
|
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
|
+ )
|