|
@@ -5,13 +5,12 @@ from cocos.layer import ScrollableLayer, Layer
|
5
|
5
|
from cocos.sprite import Sprite
|
6
|
6
|
from pyglet.window import key as wkey
|
7
|
7
|
from random import randint
|
8
|
|
-
|
|
8
|
+from sandbox.life_game.simulation import CellBornEvent
|
9
|
9
|
from sandbox.life_game.simulation import CellDieEvent, Cell, InvertCellStateBehaviour, \
|
10
|
10
|
EmptyPositionWithLotOfCellAroundEvent
|
11
|
|
-from sandbox.life_game.simulation import CellBornEvent
|
12
|
|
-from synergine2.gui import Gui
|
13
|
|
-from synergine2.terminals import TerminalPackage
|
14
|
11
|
from synergine2.terminals import Terminal
|
|
12
|
+from synergine2.terminals import TerminalPackage
|
|
13
|
+from synergine2_cocos2d.gui import Gui, GridLayerMixin, MainLayer as BaseMainLayer
|
15
|
14
|
|
16
|
15
|
cell_scale = ScaleBy(1.1, duration=0.25)
|
17
|
16
|
cell_rotate = RotateBy(360, duration=30)
|
|
@@ -19,57 +18,11 @@ flash_flash = ScaleBy(8, duration=0.5)
|
19
|
18
|
flash_rotate = RotateBy(360, duration=6)
|
20
|
19
|
|
21
|
20
|
|
22
|
|
-class GridManager(object):
|
23
|
|
- def __init__(
|
24
|
|
- self,
|
25
|
|
- layer: Layer,
|
26
|
|
- square_width: int,
|
27
|
|
- border: int=0,
|
28
|
|
- ):
|
29
|
|
- self.layer = layer
|
30
|
|
- self.square_width = square_width
|
31
|
|
- self.border = border
|
32
|
|
-
|
33
|
|
- @property
|
34
|
|
- def final_width(self):
|
35
|
|
- return self.square_width + self.border
|
36
|
|
-
|
37
|
|
- def scale_sprite(self, sprite: Sprite):
|
38
|
|
- sprite.scale_x = self.final_width / sprite.image.width
|
39
|
|
- sprite.scale_y = self.final_width / sprite.image.height
|
40
|
|
-
|
41
|
|
- def position_sprite(self, sprite: Sprite, grid_position):
|
42
|
|
- grid_x = grid_position[0]
|
43
|
|
- grid_y = grid_position[1]
|
44
|
|
- sprite.position = grid_x * self.final_width, grid_y * self.final_width
|
45
|
|
-
|
46
|
|
- def get_window_position(self, grid_position_x, grid_position_y):
|
47
|
|
- grid_x = grid_position_x
|
48
|
|
- grid_y = grid_position_y
|
49
|
|
- return grid_x * self.final_width, grid_y * self.final_width
|
50
|
|
-
|
51
|
|
- def get_grid_position(self, window_x, window_y, z=0) -> tuple:
|
52
|
|
- window_size = director.get_window_size()
|
53
|
|
-
|
54
|
|
- window_center_x = window_size[0] // 2
|
55
|
|
- window_center_y = window_size[1] // 2
|
56
|
|
-
|
57
|
|
- window_relative_x = window_x - window_center_x
|
58
|
|
- window_relative_y = window_y - window_center_y
|
59
|
|
-
|
60
|
|
- real_width = self.final_width * self.layer.scale
|
61
|
|
-
|
62
|
|
- return int(window_relative_x // real_width),\
|
63
|
|
- int(window_relative_y // real_width),\
|
64
|
|
- z
|
65
|
|
-
|
66
|
|
-
|
67
|
|
-class Cells(Layer):
|
|
21
|
+class Cells(GridLayerMixin, Layer):
|
68
|
22
|
def __init__(self):
|
69
|
23
|
super().__init__()
|
70
|
24
|
self.cells = {}
|
71
|
25
|
self.flashs = []
|
72
|
|
- self.grid_manager = GridManager(self, 32, border=2)
|
73
|
26
|
|
74
|
27
|
def born(self, grid_position):
|
75
|
28
|
if grid_position in self.cells:
|
|
@@ -103,15 +56,11 @@ class Cells(Layer):
|
103
|
56
|
self.add(flash)
|
104
|
57
|
|
105
|
58
|
|
106
|
|
-class MainLayer(ScrollableLayer):
|
|
59
|
+class MainLayer(GridLayerMixin, BaseMainLayer):
|
107
|
60
|
is_event_handler = True
|
108
|
61
|
|
109
|
|
- def __init__(self, terminal: Terminal):
|
110
|
|
- super().__init__()
|
111
|
|
-
|
112
|
|
- self.terminal = terminal
|
113
|
|
- self.scroll_step = 100
|
114
|
|
- self.grid_manager = GridManager(self, 32, border=2)
|
|
62
|
+ def __init__(self, *args, **kwargs):
|
|
63
|
+ super().__init__(*args, **kwargs)
|
115
|
64
|
|
116
|
65
|
self.background = Sprite('resources/banner-1711735_640.jpg')
|
117
|
66
|
self.background.position = 0, 0
|
|
@@ -131,27 +80,6 @@ class MainLayer(ScrollableLayer):
|
131
|
80
|
window_size = director.get_window_size()
|
132
|
81
|
self.position = window_size[0] // 2, window_size[1] // 2
|
133
|
82
|
|
134
|
|
- def on_key_press(self, key, modifiers):
|
135
|
|
- if key == wkey.LEFT:
|
136
|
|
- self.position = (self.position[0] + self.scroll_step, self.position[1])
|
137
|
|
-
|
138
|
|
- if key == wkey.RIGHT:
|
139
|
|
- self.position = (self.position[0] - self.scroll_step, self.position[1])
|
140
|
|
-
|
141
|
|
- if key == wkey.UP:
|
142
|
|
- self.position = (self.position[0], self.position[1] - self.scroll_step)
|
143
|
|
-
|
144
|
|
- if key == wkey.DOWN:
|
145
|
|
- self.position = (self.position[0], self.position[1] + self.scroll_step)
|
146
|
|
-
|
147
|
|
- if key == wkey.A:
|
148
|
|
- if self.scale >= 0.3:
|
149
|
|
- self.scale -= 0.2
|
150
|
|
-
|
151
|
|
- if key == wkey.Z:
|
152
|
|
- if self.scale <= 4:
|
153
|
|
- self.scale += 0.2
|
154
|
|
-
|
155
|
83
|
def on_mouse_press(self, x, y, buttons, modifiers):
|
156
|
84
|
x, y = director.get_virtual_coordinates(x, y)
|
157
|
85
|
grid_position = self.grid_manager.get_grid_position(x, y)
|