|
|
@@ -2,31 +2,58 @@
|
|
2
|
2
|
from random import randint
|
|
3
|
3
|
|
|
4
|
4
|
import cocos
|
|
|
5
|
+from cocos.actions import MoveTo, Repeat, ScaleBy, Reverse, RotateTo
|
|
5
|
6
|
from cocos.sprite import Sprite
|
|
6
|
|
-from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn
|
|
|
7
|
+from sandbox.engulf.behaviour import GrassGrownUp, GrassSpawn, MoveTo as MoveToEvent
|
|
7
|
8
|
from sandbox.engulf.subject import Cell, Grass
|
|
8
|
9
|
from synergine2.terminals import TerminalPackage
|
|
9
|
10
|
from synergine2_cocos2d.gui import Gui, GridLayerMixin
|
|
10
|
11
|
from synergine2_cocos2d.gui import MainLayer as BaseMainLayer
|
|
11
|
12
|
|
|
|
13
|
+cell_scale = ScaleBy(1.1, duration=0.25)
|
|
|
14
|
+
|
|
12
|
15
|
|
|
13
|
16
|
class CellsLayer(GridLayerMixin, BaseMainLayer):
|
|
14
|
|
- def __init__(self, *args, **kwargs):
|
|
|
17
|
+ def __init__(self, game: 'Game', *args, **kwargs):
|
|
15
|
18
|
super().__init__(*args, **kwargs)
|
|
16
|
|
- self.cells = {}
|
|
|
19
|
+ self.game = game
|
|
|
20
|
+ self.cell_positions = {}
|
|
|
21
|
+ self.cell_ids = {}
|
|
|
22
|
+
|
|
|
23
|
+ @property
|
|
|
24
|
+ def move_duration(self):
|
|
|
25
|
+ return self.game.cycle_duration
|
|
|
26
|
+
|
|
|
27
|
+ @property
|
|
|
28
|
+ def fake_move_rotate_duration(self):
|
|
|
29
|
+ return self.move_duration / 3
|
|
17
|
30
|
|
|
18
|
|
- def born(self, grid_position):
|
|
|
31
|
+ def born(self, subject_id: int, grid_position):
|
|
19
|
32
|
cell = Sprite('resources/cell.png')
|
|
20
|
33
|
cell.rotation = randint(0, 360)
|
|
21
|
34
|
self.grid_manager.scale_sprite(cell)
|
|
22
|
35
|
self.grid_manager.position_sprite(cell, grid_position)
|
|
23
|
|
- self.cells[grid_position] = cell
|
|
|
36
|
+ self.cell_positions[grid_position] = cell
|
|
|
37
|
+ self.cell_ids[subject_id] = cell
|
|
|
38
|
+ cell.do(Repeat(cell_scale + Reverse(cell_scale)))
|
|
24
|
39
|
self.add(cell)
|
|
25
|
40
|
|
|
|
41
|
+ def move(self, subject_id: int, position: tuple):
|
|
|
42
|
+ cell = self.cell_ids[subject_id]
|
|
|
43
|
+
|
|
|
44
|
+ window_position = self.grid_manager.get_window_position(position[0], position[1])
|
|
|
45
|
+ move_action = MoveTo(window_position, self.move_duration)
|
|
|
46
|
+
|
|
|
47
|
+ fake_rotate = RotateTo(randint(0, 360), self.fake_move_rotate_duration)
|
|
|
48
|
+
|
|
|
49
|
+ cell.do(move_action)
|
|
|
50
|
+ cell.do(fake_rotate)
|
|
|
51
|
+
|
|
26
|
52
|
|
|
27
|
53
|
class GrassLayer(GridLayerMixin, BaseMainLayer):
|
|
28
|
|
- def __init__(self, *args, **kwargs):
|
|
|
54
|
+ def __init__(self, game: 'Game', *args, **kwargs):
|
|
29
|
55
|
super().__init__(*args, **kwargs)
|
|
|
56
|
+ self.game = game
|
|
30
|
57
|
self.grasses = {}
|
|
31
|
58
|
|
|
32
|
59
|
def born(self, subject_id, grid_position, opacity=100):
|
|
|
@@ -43,13 +70,14 @@ class GrassLayer(GridLayerMixin, BaseMainLayer):
|
|
43
|
70
|
|
|
44
|
71
|
|
|
45
|
72
|
class MainLayer(GridLayerMixin, BaseMainLayer):
|
|
46
|
|
- def __init__(self, terminal, *args, **kwargs):
|
|
|
73
|
+ def __init__(self, game: 'Game', terminal, *args, **kwargs):
|
|
47
|
74
|
super().__init__(terminal, *args, **kwargs)
|
|
|
75
|
+ self.game = game
|
|
48
|
76
|
|
|
49
|
|
- self.cells = CellsLayer(terminal=terminal)
|
|
|
77
|
+ self.cells = CellsLayer(game=game, terminal=terminal)
|
|
50
|
78
|
self.add(self.cells)
|
|
51
|
79
|
|
|
52
|
|
- self.grasses = GrassLayer(terminal=terminal)
|
|
|
80
|
+ self.grasses = GrassLayer(game=game, terminal=terminal)
|
|
53
|
81
|
self.add(self.grasses)
|
|
54
|
82
|
|
|
55
|
83
|
|
|
|
@@ -57,9 +85,10 @@ class Game(Gui):
|
|
57
|
85
|
def __init__(self, *args, **kwargs):
|
|
58
|
86
|
super().__init__(*args, **kwargs)
|
|
59
|
87
|
|
|
60
|
|
- self.main_layer = MainLayer(terminal=self.terminal)
|
|
|
88
|
+ self.main_layer = MainLayer(game=self, terminal=self.terminal)
|
|
61
|
89
|
self.main_scene = cocos.scene.Scene(self.main_layer)
|
|
62
|
90
|
|
|
|
91
|
+ # Event registering
|
|
63
|
92
|
self.terminal.register_event_handler(
|
|
64
|
93
|
GrassGrownUp,
|
|
65
|
94
|
self.on_grass_grown_up,
|
|
|
@@ -68,6 +97,10 @@ class Game(Gui):
|
|
68
|
97
|
GrassSpawn,
|
|
69
|
98
|
self.on_grass_spawn,
|
|
70
|
99
|
)
|
|
|
100
|
+ self.terminal.register_event_handler(
|
|
|
101
|
+ MoveToEvent,
|
|
|
102
|
+ self.on_move_to,
|
|
|
103
|
+ )
|
|
71
|
104
|
|
|
72
|
105
|
def get_main_scene(self):
|
|
73
|
106
|
return self.main_scene
|
|
|
@@ -76,7 +109,7 @@ class Game(Gui):
|
|
76
|
109
|
if package.subjects: # It's thirst package
|
|
77
|
110
|
for subject in package.subjects:
|
|
78
|
111
|
if isinstance(subject, Cell):
|
|
79
|
|
- self.main_layer.cells.born(subject.position)
|
|
|
112
|
+ self.main_layer.cells.born(subject.id, subject.position)
|
|
80
|
113
|
if isinstance(subject, Grass):
|
|
81
|
114
|
self.main_layer.grasses.born(
|
|
82
|
115
|
subject.id,
|
|
|
@@ -96,3 +129,9 @@ class Game(Gui):
|
|
96
|
129
|
event.subject_id,
|
|
97
|
130
|
event.density, # TODO: Recupe ces données depuis local plutôt que event ?
|
|
98
|
131
|
)
|
|
|
132
|
+
|
|
|
133
|
+ def on_move_to(self, event: MoveToEvent):
|
|
|
134
|
+ self.main_layer.cells.move(
|
|
|
135
|
+ event.subject_id,
|
|
|
136
|
+ event.position,
|
|
|
137
|
+ )
|