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
|
extends Node
signal level_map_updated()
signal game_started()
signal game_ended()
signal game_won()
var Levels = [
"Level_0001",
"Level_0002",
"Level_0003",
"Level_0004",
"Level_0005",
"Level_0005",
"Level_0004",
"Level_0002",
"Level_0001",
]
var Level_Map = []
func _ready():
Network.connect("game_started", Callable(self, "_on_Network_game_started"))
Network.connect("game_ended", Callable(self, "_on_Network_game_ended"))
self.init()
func init():
randomize()
self.Levels.shuffle()
for level in self.Levels:
self.Level_Map.push_back({
time = 0,
cleared_by = { # Network.player
idx = -1,
name = ""
},
meta = {
path = "res://Levels/%s.tscn" % level,
name = level,
},
})
func change_scene_to_instance(instance):
call_deferred("_deferred_change_scene_to_instance", instance)
func _deferred_change_scene_to_instance(instance):
get_tree().current_scene.free()
get_tree().get_root().add_child(instance)
get_tree().set_current_scene(instance)
func get_level(idx):
return self.Level_Map[idx]
func get_instance_level(idx):
var global_level = self.get_level(idx)
var Level = load(global_level.meta.path)
var instance_level = Level.instantiate()
instance_level.idx = idx
return instance_level
func start_level(idx):
var instance_level = self.get_instance_level(idx)
self.change_scene_to_instance(instance_level)
func end_level(instance_level):
var global_level = self.get_level(instance_level.idx)
var time = snapped(instance_level.timer, 0.01)
if (global_level.time == 0 or time < global_level.time):
global_level.time = time
global_level.cleared_by = Network.player
for id in Network.players:
rpc_id(id, "_update_level_map", instance_level.idx, global_level)
get_tree().change_scene_to_file("res://UI/LevelSelect.tscn")
self.check_win()
func cancel_level():
get_tree().change_scene_to_file("res://UI/LevelSelect.tscn")
@rpc("any_peer") func _update_level_map(idx, global_level):
self.Level_Map[idx] = global_level
emit_signal("level_map_updated")
func start_game():
emit_signal("game_started")
func _on_Network_game_started():
get_tree().change_scene_to_file("res://UI/CharacterSelect.tscn")
func end_game():
emit_signal("game_ended")
func _on_Network_game_ended():
get_tree().change_scene_to_file("res://UI/GameOver.tscn")
self.Level_Map = []
self.init()
func has_won():
var has_won = false
var cleared_levels_idx = []
for idx in range(self.Level_Map.size()):
if self.Level_Map[idx].cleared_by.idx == Network.player.idx:
cleared_levels_idx.push_back(idx)
var possible_winning_conditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
]
# because there's no intersection method on arrays..
for cond in possible_winning_conditions:
var has_cleared = 0
for digit in cond:
if digit in cleared_levels_idx:
has_cleared += 1
if has_cleared == 3:
has_won = true
break
return has_won
func check_win():
var has_won = self.has_won()
if has_won:
# TODO: start 10s timer with callback check_win_timer()
# SAVE IN REMOTE ARRAY USERS WITH CROWN = rpc_id + running timer?
# Check for each user that already has a crown if the win condition is still true
# if it's not, delete user from array and stop timer
# if it is, do nothing, keep running until game end
self.check_win_timer()
pass
func check_win_timer():
var has_won = self.has_won()
if has_won:
emit_signal("game_won", Network.player)
func open_menu():
var Menu = load("res://UI/Menu/Menu.tscn").instantiate()
get_tree().paused = true
get_tree().current_scene.add_child(Menu)
|