-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrid.gd
484 lines (440 loc) · 13.7 KB
/
Grid.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
extends Node2D
@export var grid_cells = [] # (Array, PackedScene)
@onready var grid_start_position = $GridStartPosition
@onready var cells = $Cells
var map = [] # tile type map
var b_map = [] # bomb count map
var v_map = [] # visited tiles map
var n_map = [] # nodes map
var c_map = {} # dirty map (to update)
var o_map = [] # old map to keep track of update frame
var bomb_cells: Array = []
var flagged_cells: Array = []
var timer = 0
var revealed_tiles = []
var gridmap_size: Vector2i = Vector2i(20, 20)
var game_restart = false
var bombed = false
var won = false
var skip_animation = false
var hovered_cells = []
var timer_start = false
# Called when the node enters the scene tree for the first time.
func _ready():
if not OS.has_feature("debug"):
randomize()
for i in gridmap_size.x:
map.append([])
for j in gridmap_size.y:
map[i].append("default")
print((gridmap_size.x*gridmap_size.y)/8)
for i in int((gridmap_size.x*gridmap_size.y)/(32/4)):
var pos = Vector2(randi_range(0, gridmap_size.x-1), randi_range(0, gridmap_size.y-1))
while bomb_cells.has(pos):
pos = Vector2(randi_range(0, gridmap_size.x-1), randi_range(0, gridmap_size.y-1))
map[pos.x][pos.y] = "bomb"
bomb_cells.append(pos)
print(len(bomb_cells))
for i in gridmap_size.x:
b_map.append([])
for j in gridmap_size.y:
b_map[i].append(0)
for i in gridmap_size.x:
v_map.append([])
for j in gridmap_size.y:
v_map[i].append(false)
for i in gridmap_size.x:
o_map.append([])
for j in gridmap_size.y:
o_map[i].append(map[i][j])
var start_position = grid_start_position.position
$AnimationPlayer.play_backwards("opening") # closing animation with visibilty changes
await $AnimationPlayer.animation_finished
for i in gridmap_size.x:
n_map.append([])
for j in gridmap_size.y:
start_position += Vector2(32, 0)
var cell = _create_cell(i, j, start_position)
cells.add_child(cell)
n_map[i][j] = cell
await get_tree().process_frame
start_position.x = grid_start_position.position.x
start_position += Vector2(0, 32)
for i in gridmap_size.x:
for j in gridmap_size.y:
var val = map[i][j]
if val in ["default", "bomb"]:
var cell = n_map[i][j]
cell.get_node("AnimationPlayer").play("create")
await get_tree().process_frame
func _input(event):
if event is InputEventKey:
if event.keycode == KEY_R and game_restart:
reload()
# if event is InputEventMouseMotion:
# var pos = (event.position)-Vector2(0, 32)
# var gp = Vector2(int(pos.y / 32) - 1, int(pos.x / 32) - 1)
# hovered_cells.append(gp)
if event is InputEventMouseButton:
var pos = (event.position)-Vector2(0, 32)
var gp = Vector2(int(pos.y / 32) - 1, int(pos.x / 32) - 1)
if OS.has_feature('debug') and Input.is_key_pressed(KEY_U) and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
for bombed_cell in bomb_cells:
o_map[bombed_cell.x][bombed_cell.y] = map[bombed_cell.x][bombed_cell.y]
map[bombed_cell.x][bombed_cell.y] = "flag"
flagged_cells.append(bombed_cell)
c_map[bombed_cell] = true
return
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed and not (game_restart or bombed or won):
if gp in flagged_cells:
o_map[gp.x][gp.y] = map[gp.x][gp.y]
map[gp.x][gp.y] = "default"
flagged_cells.erase(gp)
c_map[gp] = true
else:
o_map[gp.x][gp.y] = map[gp.x][gp.y]
map[gp.x][gp.y] = "flag"
flagged_cells.append(gp)
c_map[gp] = true
print(_check_win())
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed and bombed:
skip_animation = true
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed and not (bombed or won):
if gp in bomb_cells:
if timer == 0:
for bomb_cell in bomb_cells:
map[bomb_cell.x][bomb_cell.y] = "default"
bomb_cells.clear()
for i in int((gridmap_size.x*gridmap_size.y)/(32/4)):
var b_pos = Vector2(randi_range(0, gridmap_size.x-1), randi_range(0, gridmap_size.y-1))
map[b_pos.x][b_pos.y] = "bomb"
bomb_cells.append(b_pos)
print("opening tiles")
_open_tiles(gp)
timer_start = true
return
set_bombed(gp)
elif not (game_restart or bombed or won):
timer_start = true
await _open_tiles_queue(gp)
if _check_win():
$Label.show()
$AnimationPlayer.play("won")
won = true
$stopwatch/AnimatedSprite2D.stop()
game_restart = true
await get_tree().create_timer(2).timeout
$AnimationPlayer.play_backwards("won")
await $AnimationPlayer.animation_finished
$Label.hide()
_setup_game_ened_scene()
if OS.has_feature("debug"):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_MIDDLE and event.pressed:
for bomb_cell in bomb_cells:
map[bomb_cell.x][bomb_cell.y] = "bomb_rendered"
c_map[bomb_cell] = true
elif event.button_index == MOUSE_BUTTON_MIDDLE and !event.pressed:
for bomb_cell in bomb_cells:
map[bomb_cell.x][bomb_cell.y] = "bomb"
if bomb_cell in flagged_cells:
map[bomb_cell.x][bomb_cell.y] = "flag"
c_map[bomb_cell] = true
func set_bombed(gp):
var time = 0.5
o_map[gp.x][gp.y] = map[gp.x][gp.y]
map[gp.x][gp.y] = "bomb_rendered"
c_map[gp] = true
await get_tree().create_timer(time).timeout
bomb_cells.sort_custom(func(a, b):
return gp.direction_to(b) < gp.direction_to(a) and gp.distance_to(a) < gp.distance_to(b)
)
bombed = true
$stopwatch/AnimatedSprite2D.stop()
if not skip_animation:
await get_tree().create_timer(time).timeout
for bomb_cell in bomb_cells:
if bomb_cell == gp:
continue
if bomb_cell in flagged_cells:
continue
o_map[gp.x][gp.y] = map[gp.x][gp.y]
map[bomb_cell.x][bomb_cell.y] = "bomb_rendered"
c_map[bomb_cell] = true
time -= exp(time) * get_process_delta_time()
if not skip_animation:
await get_tree().create_timer(time).timeout
await get_tree().create_timer(0.5).timeout
game_restart = true
# $Label3.show()
$Label4.show()
$AnimationPlayer.play("bombed")
await get_tree().create_timer(2).timeout
$AnimationPlayer.play_backwards("bombed")
await $AnimationPlayer.animation_finished
# $Label3.hide()
_setup_game_ened_scene()
func _check_win():
if OS.has_feature('debug') and Input.is_key_pressed(KEY_Y):
return true
var dict = {}
for revealed_tile in revealed_tiles:
if not ((revealed_tile in bomb_cells) or (revealed_tile in flagged_cells)):
dict[revealed_tile] = null
var revealed_tiles_count = len(dict.keys())
var bomb_count = len(bomb_cells)
var total_cell_count = gridmap_size.x*gridmap_size.y
prints((total_cell_count-bomb_count) <= revealed_tiles_count, revealed_tiles_count, total_cell_count, bomb_count)
return (total_cell_count) <= revealed_tiles_count+bomb_count
var bombs = 0
for bomb_pos in bomb_cells:
if bomb_pos in flagged_cells:
bombs += 1
prints(bomb_cells.size(), bombs)
return bombs == bomb_cells.size()
func _open_tiles_queue(gp):
var poses = [
Vector2(-1, -1), Vector2(-1, 0), Vector2(-1, 1),
Vector2(0, -1), Vector2(0, 1),
Vector2(1, -1), Vector2(1, 0), Vector2(1, 1),
]
var queue = [gp]
var bomb_found = false
if v_map[gp.x][gp.y]:
if b_map[gp.x][gp.y] > 0:
var flags = 0
for pos in poses:
var new_pos = gp+pos
if new_pos in flagged_cells:
flags += 1
print("Flags: ", flags, " Bombs: ", b_map[gp.x][gp.y])
if flags != b_map[gp.x][gp.y]:
return
queue.pop_front()
for pos in poses:
queue.append(gp+pos)
while queue.size() > 0:
var ngp = queue.pop_front()
if ngp == null:
continue
if _not_inside_bounds(ngp):
continue
if ngp in flagged_cells:
continue
if ngp in bomb_cells:
set_bombed(ngp)
queue.clear()
continue
print("ngp", ngp)
# calculate bombs
var bombs = 0
for pos in poses:
var new_pos = ngp+pos
if new_pos in bomb_cells:
bombs += 1
# tile revealed?
if v_map[ngp.x][ngp.y]:
continue
# reveal tile
v_map[ngp.x][ngp.y] = true
b_map[ngp.x][ngp.y] = bombs
c_map[ngp] = true
# bombs are higher than 0
await get_tree().process_frame
if bombs > 0:
continue
# add neighbours
for pos in poses:
queue.append(ngp+pos)
func _open_tiles(gp):
var poses = [
Vector2(-1, -1), Vector2(-1, 0), Vector2(-1, 1),
Vector2(0, -1), Vector2(0, 0), Vector2(0, 1),
Vector2(1, -1), Vector2(1, 0), Vector2(1, 1),
]
if _not_inside_bounds(gp):
return -1
if v_map[gp.x][gp.y]:
if b_map[gp.x][gp.y] < 1:
return
if not b_map[gp.x][gp.y]:
var bombs = 0
for pos in poses:
var new_pos = gp+pos
if new_pos in bomb_cells:
bombs += 1
if not revealed_tiles.has(gp):
revealed_tiles.append(gp)
v_map[gp.x][gp.y] = true
b_map[gp.x][gp.y] = bombs
c_map[gp] = true
if bombs > 0:
return bombs
var dfs_poses = [
Vector2(-1, -1), Vector2(-1, 0), Vector2(-1, 1),
Vector2(0, -1), Vector2(0, 0), Vector2(0, 1),
Vector2(1, -1), Vector2(1, 0), Vector2(1, 1),
]
var flags = 0
for pos in dfs_poses:
var new_pos = gp+pos
if new_pos in flagged_cells:
flags += 1
if flags != b_map[gp.x][gp.y]:
return b_map[gp.x][gp.y]
# Open Area on click
for pos in dfs_poses:
var new_pos = gp+pos
if _not_inside_bounds(new_pos):
continue
if not revealed_tiles.has(new_pos):
revealed_tiles.append(new_pos)
if v_map[new_pos.x][new_pos.y]:
continue
if not new_pos in bomb_cells:
await get_tree().process_frame
# _open_tiles(new_pos)
var bombs_nei = await _open_tiles(new_pos)
if bombs_nei > 0:
continue
return b_map[gp.x][gp.y]
func _setup_game_ened_scene():
var game_ended = preload("res://game_ended.tscn")
var game = game_ended.instantiate()
game.won = true if won else false if bombed else false
add_child(game)
game.score = timer
game.game = self
func _not_inside_bounds(gp):
return gp.x >= gridmap_size.x or gp.y >= gridmap_size.y or gp.x < 0 or gp.y < 0
func _create_cell(i, j, start_pos):
var value = map[i][j]
var b_val = b_map[i][j]
var v_val = v_map[i][j]
var cell_type = _get_cell_type(Vector2(i, j))
var cell = cell_type.instantiate()
cell.position = start_pos
if value == "default":
cell.get_node("Label").text = str(b_val) if b_val > 0 and v_val else ""
n_map[i].append(cell)
return cell
# var i = 0
# for child in cells.get_children():
# cells.remove_child(child)
# if i % 90 == 0:
# await get_tree().process_frame
# i += 1
# var start_position = grid_start_position.position
#
# for x in map.size():
# for y in map[x].size():
# var value = map[x][y]
# var b_val = b_map[x][y]
# var v_val = v_map[x][y]
# start_position += Vector2(32, 0)
# if value == "default":
# var default
# if v_val:
# default = grid_cells[3]
# else:
# default = grid_cells[0]
# var cell = default.instantiate()
# cells.add_child(cell)
# cell.position = start_position
# cell.get_node("Label").text = str(b_val) if b_val > 0 and v_val else ""
# if value == "bomb":
# var default = grid_cells[0]
# var cell = default.instantiate()
# cells.add_child(cell)
# cell.position = start_position
# if value == "bomb_rendered":
# var bomb = grid_cells[1]
# var cell = bomb.instantiate()
# cells.add_child(cell)
# cell.position = start_position
# if value == "flag":
# var flag = grid_cells[2]
# var cell = flag.instantiate()
# cells.add_child(cell)
# cell.position = start_position
# await get_tree().idle_frame
# start_position.x = grid_start_position.position.x
# start_position += Vector2(0, 16)
func _update_render():
for pos in c_map.duplicate():
c_map.erase(pos)
var cell = n_map[pos.x][pos.y]
var new_cell = _create_cell(pos.x, pos.y, cell.position)
var val = map[pos.x][pos.y]
var o_val = o_map[pos.x][pos.y]
var v_val = v_map[pos.x][pos.y]
print(o_val == "flag", val == "default")
if o_val == "flag" and val == "default":
cell.get_node("AnimationPlayer").play("unplace")
await get_tree().create_timer(0.35).timeout
if is_instance_valid(cell):
for child in cell.get_children():
cell.remove_child(child)
child.queue_free()
cell.replace_by(new_cell)
if val == "default" and not v_val:
new_cell.get_node("AnimationPlayer").play("create")
if val == "bomb_rendered":
new_cell.get_node("AnimationPlayer").play("explode")
if val == "default" and v_val:
new_cell.get_node("AnimationPlayer").play("open")
if val == "flag":
new_cell.get_node("AnimationPlayer").play("place")
n_map[pos.x][pos.y] = new_cell
cell.free()
func _process(delta):
_update_render()
# for hovered_cell in hovered_cells:
# if _not_inside_bounds(hovered_cell):
# continue
# print((n_map.has(hovered_cell.x) and n_map[hovered_cell.x].has(hovered_cell.y)))
# if (n_map.size() > hovered_cell.x and n_map[hovered_cell.x].size() > hovered_cell.y):
# var cell = n_map[hovered_cell.x][hovered_cell.y]
# var old_scale = Vector2.ONE * 0.25
# cell.scale = old_scale + Vector2.ONE * 0.1
# await get_tree().process_frame
# cell.scale = old_scale
# hovered_cells.clear()
if (not (bombed or won)) and timer_start:
timer += delta
$Label5.text = "⏱️ %03d" % timer
$Label6.text = "💣 %03d" % (len(bomb_cells)-len(flagged_cells))
func _get_cell_type(gp):
var cell_type
var value = map[gp.x][gp.y]
var v_val = v_map[gp.x][gp.y]
match value:
"default":
if v_val:
cell_type = grid_cells[0]
else:
cell_type = grid_cells[3]
"bomb":
cell_type = grid_cells[3]
"bomb_rendered":
cell_type = grid_cells[1]
"flag":
cell_type = grid_cells[2]
return cell_type
func _clean_board():
for x in map.size():
for y in map[x].size():
var cell = n_map[x][y]
var v_val = v_map[x][y]
var val = map[x][y]
cell.get_node("AnimationPlayer").play("remove")
await get_tree().process_frame
func _exit():
$AnimationPlayer.play("closing")
_clean_board()
func reload():
skip_animation = false
$AnimationPlayer.play("closing")
_clean_board()
await get_tree().create_timer(1.5).timeout
get_tree().reload_current_scene()