-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.gd
349 lines (307 loc) · 9.27 KB
/
2048.gd
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
extends Control
export var CELL_PADDING_RATIO = .125
export var RADIUS_RATIO = .05
export var highlight_color = Color("#8cff8c")
export var transition_anim = "Fade"
var cell_size
var cell_padding
var radius
var base_style = StyleBoxFlat.new()
var rects = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
var font1 = DynamicFont.new()
var font2 = DynamicFont.new()
var font3 = DynamicFont.new()
var font4 = DynamicFont.new()
var new_cell = null
var outline_style = StyleBoxFlat.new()
var ending_cell = null
# Called when the node enters the scene tree for the first time.
func _ready():
$TimeDisplay.set("custom_fonts/normal_font", font1)
$EndingCellDisplay.set("custom_fonts/normal_font", font4)
var screen_size = get_viewport_rect().size
cell_size = min(screen_size.x, screen_size.y) / (5 * CELL_PADDING_RATIO + 4)
cell_padding = CELL_PADDING_RATIO * cell_size
radius = RADIUS_RATIO * cell_size
base_style.set_corner_radius_all(radius)
base_style.set_bg_color(Color(0.5, 0.5, 0.5))
outline_style.set_corner_radius_all(radius)
outline_style.set_bg_color(highlight_color)
var shared_font_data = load(globals.font_dir)
font1.font_data = shared_font_data
font1.size = 250
font2.font_data = shared_font_data
font2.size = 200
font3.font_data = shared_font_data
font3.size = 150
font4.font_data = shared_font_data
font4.size = 75
var countdown_time
match globals.difficulty:
"easy":
ending_cell = 64
countdown_time = 45
"medium":
ending_cell = 128
countdown_time = 60
"hard":
ending_cell = 128
countdown_time = 45
$EndingCellDisplay.clear()
$EndingCellDisplay.append_bbcode("[center]" + "Reach cell: " + "[/center]")
randomize()
init_cells()
add_new_cell()
$Countdown.start(countdown_time)
Transition.get_node("AmbientAudio").stream_paused = true
if Transition.get_node("FastAudio").stream_paused:
Transition.get_node("FastAudio").stream_paused = false
else:
Transition.get_node("FastAudio").play()
# Transition.transition_to("res://Score.tscn", transition_anim)
func offset(cell):
match len(str(cell)):
1:
return Vector2(cell_size * 0.25, cell_size * 0.75)
2:
return Vector2(cell_size * 0.12, cell_size * 0.72)
3:
return Vector2(cell_size * 0.06, cell_size * 0.65)
func get_cell_font(cell): # NOTE: get_font is reserved
match len(str(cell)):
1:
return font1
2:
return font2
3:
return font3
func _draw():
var highlight = false
draw_style_box(
get_style_box(ending_cell),
Rect2(
$EndingCellDisplay.rect_position + $EndingCellDisplay.rect_size / 4,
$EndingCellDisplay.rect_size / 2
)
)
draw_string(
get_cell_font(ending_cell),
(
$EndingCellDisplay.rect_position
+ $EndingCellDisplay.rect_size / 4
+ offset(ending_cell)
+ Vector2(12, 0)
),
str(ending_cell)
)
for r in range(4):
for c in range(4):
if rects[r][c] == new_cell:
# # highlight new cell
# var highlighted_style = get_style_box(rects[r][c][1])
# highlighted_style.set_bg_color(highlighted_style.get_bg_color().linear_interpolate(highlight_color, 0.4))
# draw_style_box(highlighted_style, rects[r][c][0])
# new_cell = null
# highlight = true
# outline cell
var outline_rect = rects[r][c][0]
outline_rect.position -= Vector2(cell_padding * 0.25, cell_padding * 0.25)
outline_rect.end += Vector2(cell_padding * 0.5, cell_padding * 0.5)
draw_style_box(outline_style, outline_rect)
draw_style_box(get_style_box(rects[r][c][1]), rects[r][c][0])
else:
draw_style_box(get_style_box(rects[r][c][1]), rects[r][c][0])
if rects[r][c][1] != null:
var value_str = str(rects[r][c][1])
var font
if len(value_str) == 1:
font = font1
elif len(value_str) == 2:
font = font2
elif len(value_str) == 3:
font = font3
if highlight:
draw_string(font, rects[r][c][0].position + offset(rects[r][c][1]), value_str)
highlight = false
else:
draw_string(font, rects[r][c][0].position + offset(rects[r][c][1]), value_str)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
$TimeDisplay.clear()
$TimeDisplay.append_bbcode("[center]" + fmt_time($Countdown.time_left) + "[/center]")
func fmt_time(secs):
secs = int(secs)
return str(secs / 60).pad_zeros(1) + ":" + str(secs % 60).pad_zeros(2)
func _input(event):
$Countdown.paused = true
var rects_copy = rects.duplicate(true)
if event.is_action_pressed("ui_left"):
for r in range(4):
var lst = []
for cell in rects[r]:
if cell[1] != null:
lst.append(cell[1])
for _i in range(4 - len(lst)):
lst.append(null)
for c in range(4):
rects[r][c][1] = lst[c]
for c in range(3):
if rects[r][c][1] == rects[r][c + 1][1] and rects[r][c][1] != null:
rects[r][c][1] *= 2
for i in range(c + 2, 4):
rects[r][i - 1][1] = rects[r][i][1]
rects[r][3][1] = null
elif event.is_action_pressed("ui_right"):
for r in range(4):
var lst = []
for cell in rects[r]:
if cell[1] != null:
lst.append(cell[1])
for _i in range(4 - len(lst)):
lst.insert(0, null)
for c in range(4):
rects[r][c][1] = lst[c]
for c in range(3, 0, -1):
if rects[r][c][1] == rects[r][c - 1][1] and rects[r][c][1] != null:
rects[r][c][1] *= 2
for i in range(c - 2, -1, -1):
rects[r][i + 1][1] = rects[r][i][1]
rects[r][0][1] = null
elif event.is_action_pressed("ui_up"):
for c in range(4):
var lst = []
for r in range(4):
if rects[r][c][1] != null:
lst.append(rects[r][c][1])
for _i in range(4 - len(lst)):
lst.append(null)
for r in range(4):
rects[r][c][1] = lst[r]
for r in range(3):
if rects[r][c][1] == rects[r + 1][c][1] and rects[r][c][1] != null:
rects[r][c][1] *= 2
for i in range(r + 2, 4):
rects[i - 1][c][1] = rects[i][c][1]
rects[3][c][1] = null
elif event.is_action_pressed("ui_down"):
for c in range(4):
var lst = []
for r in range(4):
if rects[r][c][1] != null:
lst.append(rects[r][c][1])
for _i in range(4 - len(lst)):
lst.insert(0, null)
for r in range(4):
rects[r][c][1] = lst[r]
for r in range(3, 0, -1):
if rects[r][c][1] == rects[r - 1][c][1] and rects[r][c][1] != null:
rects[r][c][1] *= 2
for i in range(r - 2, -1, -1):
rects[i + 1][c][1] = rects[i][c][1]
rects[0][c][1] = null
else:
$Countdown.paused = false
return
if rects_copy != rects:
add_new_cell()
update()
for r in range(4):
for c in range(4):
if rects[r][c][1] == ending_cell:
update_score(get_score())
Transition.change_color(globals.minigame_color)
Transition.transition_to("res://Score.tscn", transition_anim)
return
for r in range(4):
for c in range(4):
if rects[r][c][1] == null:
$Countdown.paused = false
return
for vec in neighbor_positions(4, r, c):
if rects[r][c][1] == rects[vec.x][vec.y][1]:
$Countdown.paused = false
return
update_score(get_score())
Transition.change_color(globals.minigame_color)
Transition.transition_to("res://Score.tscn", transition_anim)
func rand_choice(lst):
return lst[randi() % lst.size()]
func neighbor_positions(size, r, c, diagonals = false):
var res = []
for i in range(-1, 2):
for j in range(-1, 2):
if (
(diagonals or not (i and j))
and (r + i in range(size))
and (c + j in range(size))
and (i or j)
):
res.append(Vector2(r + i, c + j))
return res
func blank_cells():
var blank_cells = []
for r in range(4):
for c in range(4):
if rects[r][c][1] == null:
blank_cells.append([r, c])
return blank_cells
func add_new_cell():
var cell_indices = rand_choice(blank_cells())
var cell = rects[cell_indices[0]][cell_indices[1]]
cell[1] = rand_choice([2, 4])
new_cell = cell
update()
func init_cells():
var rect
for r in range(4):
for c in range(4):
rect = Rect2(
Vector2(
c * cell_size + (c + 1) * cell_padding, r * cell_size + (r + 1) * cell_padding
),
Vector2(cell_size, cell_size)
)
rects[r][c] = [rect, null]
func get_style_box(value):
var colored_style = base_style.duplicate()
if value == 2:
colored_style.set_bg_color(Color("#e9ddd1"))
elif value == 4:
colored_style.set_bg_color(Color("#eadabe"))
elif value == 8:
colored_style.set_bg_color(Color("#eea367"))
elif value == 16:
colored_style.set_bg_color(Color("#f28350"))
elif value == 32:
colored_style.set_bg_color(Color("#f2664d"))
elif value == 64:
colored_style.set_bg_color(Color("#f2472e"))
elif value == 128:
colored_style.set_bg_color(Color("#e8c760"))
return colored_style
func get_score():
var max_cell = 0
for r in range(4):
for c in range(4):
if rects[r][c][1] != null:
max_cell = max(rects[r][c][1], max_cell)
return (
stepify(max(50, $Countdown.time_left / $Countdown.wait_time * max_cell / ending_cell * 100), 0.01)
+ globals.math_preparedness
)
func _on_Countdown_timeout():
update_score(get_score())
Transition.change_color(globals.minigame_color)
Transition.transition_to("res://Score.tscn", transition_anim)
func update_score(score):
if globals.math_score[0] == null:
assert(globals.math_score[1] == 0, "Shouldn't happen.")
globals.minigame_score = score
globals.math_score = [score, 1]
return
globals.minigame_score = score
globals.math_score[0] = (
(globals.math_score[0] * globals.math_score[1] + score)
/ (globals.math_score[1] + 1)
)
globals.math_score[1] += 1