-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLevel.py
283 lines (219 loc) · 8.5 KB
/
Level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
Level
"""
from datetime import datetime
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Point
from Configurations import TEXTURES
from MapCanvas import MapCanvas
import TouchUtils
class Level(FloatLayout):
"""
Touch methods and map utilisation with tiles implementation.
"""
TRACE_TEXTURE = TEXTURES['trace']
TOUCH_SCALING_FACTOR = 6.5
def __init__(self, level_event_dispatcher, set_id, level_id_in_set, **kwargs):
"""
Load map in a layout then load level and touch properties.
:param level_event_dispatcher: Level Dispatcher.
:param set_id: Current set's ID.
:param level_id_in_set: Current level's ID.
:param kwargs: layout's args.
"""
super(Level, self).__init__(**kwargs)
self.level_event_dispatcher = level_event_dispatcher
# Load map in a canvas.
self.set_id = set_id
self.level_id_in_set = level_id_in_set
map_file_path = "./resources/maps/set{0}/level{0}_{1}.cfg".format(self.set_id, self.level_id_in_set)
self.map_canvas = MapCanvas(map_file_path)
self.add_widget(self.map_canvas)
self.start_time = datetime.now()
self.touch_width = int
self.level_size = list()
self.tile_size = list()
self.player_path = list()
self.win_path = list()
self.x_max = int
self.y_max = int
self.touch_matrix = None
self.old_point = list()
self.tile_identifier = list()
self.old_tile_identifier = list()
self.failed_attempts = 0
# Define level settings.
self.define_level_properties()
self.define_win_conditions()
####
# Touch methods.
####
def on_touch_down(self, touch):
"""
:param touch:
:rtype: void
"""
# Reset current path.
self.player_path = []
# get if player can draw here
self.tile_identifier = TouchUtils.get_tile_identifier(self, touch.x, touch.y)
can_draw = TouchUtils.can_start_stop(self.tile_identifier, self.map_canvas.points)
if not can_draw:
self.failed_attempts += 1
self.canvas.after.clear()
return
self.canvas.after.add(
Point(points=(touch.x, touch.y), texture=self.TRACE_TEXTURE, pointsize=self.touch_width)
)
# Save tile.
self.old_tile_identifier = self.tile_identifier[:]
self.player_path.append((self.tile_identifier[0], self.tile_identifier[1]))
self.old_point = (touch.x, touch.y)
touch.grab(self)
def on_touch_move(self, touch):
"""
:param touch:
:rtype: void
"""
if touch.grab_current is not self:
return
# get if player can draw (test if player is in a valid tile then test if tile change).
self.tile_identifier = TouchUtils.get_tile_identifier(self, touch.x, touch.y)
if self.tile_identifier is None:
can_draw = False
# if player change tile
elif self.tile_identifier != self.old_tile_identifier:
old_tile_properties = TouchUtils.get_tile_properties(self.map_canvas.map_matrix, self.old_tile_identifier)
tile_properties = TouchUtils.get_tile_properties(self.map_canvas.map_matrix, self.tile_identifier)
direction = TouchUtils.get_touch_direction(self.tile_identifier, self.old_tile_identifier)
can_draw = TouchUtils.is_authorised(self.tile_identifier, self.player_path,
tile_properties, old_tile_properties, direction)
if can_draw:
self.player_path.append((self.tile_identifier[0], self.tile_identifier[1]))
else:
can_draw = True
if not can_draw:
self.failed_attempts += 1
self.canvas.after.clear()
touch.ungrab(self)
return
points_list = self.get_smooth_points(self.old_point[0], self.old_point[1], touch.x, touch.y)
if not points_list:
points_list = [(touch.x, touch.y)]
for index in range(len(points_list)):
x_coord = points_list[index][0]
y_coord = points_list[index][1]
self.canvas.after.add(
Point(points=(x_coord, y_coord), texture=self.TRACE_TEXTURE, pointsize=self.touch_width)
)
# Save tile.
self.old_tile_identifier = self.tile_identifier
self.old_point = (touch.x, touch.y)
def on_touch_up(self, touch):
"""
:param touch:
:rtype: void
"""
if touch.grab_current is not self:
return
# get if player can draw here
self.tile_identifier = TouchUtils.get_tile_identifier(self, touch.x, touch.y)
if self.tile_identifier is None:
can_draw = False
else:
can_draw = TouchUtils.can_start_stop(self.tile_identifier, self.map_canvas.points)
if can_draw:
if self.is_path_correct():
return self.propagate_level_up()
self.failed_attempts += 1
# Delete touch if player loose.
self.canvas.after.clear()
touch.ungrab(self)
return
def get_smooth_points(self, x1, y1, x2, y2):
"""
When player touch fast, get all the points between old and new point to smooth the trace.
:param x1: new touch
:param y1: new touch
:param x2: old touch
:param y2: old touch
:return: List of tupples (coordinates for smooth points).
"""
distance_x = x2 - x1
distance_y = y2 - y1
distance = (distance_x * distance_x + distance_y * distance_y) ** 0.5
gap = self.touch_width / 4
if distance < gap:
return False
points_list = list()
quantity = distance / gap
for index in range(1, int(quantity)):
factor = index / quantity
x = x1 + distance_x * factor
y = y1 + distance_y * factor
points_list.append((x, y))
return points_list
####
# Initialisation and level up.
####
def define_level_properties(self):
"""
Define properties for the current level.
:rtype: void
"""
self.touch_width = int(self.map_canvas.tile_size / self.TOUCH_SCALING_FACTOR)
# Define level size.
self.level_size = [self.map_canvas.window.size[0] - self.map_canvas.vertical_padding * 2 -
self.map_canvas.ACTION_BAR_SIZE,
self.map_canvas.window.size[1] - self.map_canvas.horizontal_padding * 2]
# Initialise then fill matrix.
self.x_max = self.map_canvas.map_size[0]
self.y_max = self.map_canvas.map_size[1]
self.touch_matrix = []
start_x = self.map_canvas.vertical_padding
start_y = self.map_canvas.window.size[1] - self.map_canvas.horizontal_padding - self.map_canvas.ACTION_BAR_SIZE
for index_y in range(self.y_max):
self.touch_matrix.append([])
for _ in range(self.x_max):
self.touch_matrix[index_y].append(
(start_x,
start_y,
start_x + self.map_canvas.tile_size,
start_y - self.map_canvas.tile_size)
)
start_x += self.map_canvas.tile_size
start_y -= self.map_canvas.tile_size
start_x = self.map_canvas.vertical_padding
def propagate_level_up(self):
"""
Propagate level_completed event.
:rtype: void
"""
self.level_event_dispatcher.dispatch('on_level_completed', {
'set_id': self.set_id,
'level_id_in_set': self.level_id_in_set,
'resolution_time': datetime.now() - self.start_time,
'failed_attempts': self.failed_attempts
})
####
# win methods
####
def define_win_conditions(self):
"""
Get win path.
:rtype: void
"""
for index_y in range(self.y_max):
for index_x in range(self.x_max):
if self.map_canvas.map_matrix[index_y][index_x]['type'] != 'W':
self.win_path.append((index_y, index_x))
def is_path_correct(self):
"""
Test if player win.
:rtype: boolean
"""
for entry in self.win_path:
if entry not in self.player_path:
self.failed_attempts += 1
return False
return True