Repository navigation
Expand file tree
/
Copy pathMainMenu.gd
More file actions
312 lines (263 loc) · 10.8 KB
/
Copy pathMainMenu.gd
File metadata and controls
312 lines (263 loc) · 10.8 KB
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
extends Control
var main_vbox: VBoxContainer
var leaderboard_overlay: VBoxContainer
var hbox_levels: HBoxContainer
var name_edit: LineEdit
func _ready():
# ne asiguram ca tastele de baza exista in InputMap
_asigura_tasta_initiala("jump", KEY_SPACE)
_asigura_tasta_initiala("clone", KEY_C)
_asigura_tasta_initiala("release_clone", KEY_E)
# cream fundalul synthwave pe un canvas layer separat in spate
var fundal_absolut = CanvasLayer.new()
fundal_absolut.layer = -1
var bg = ColorRect.new()
bg.color = ProjectSettings.get_setting("rendering/environment/defaults/default_clear_color")
bg.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
fundal_absolut.add_child(bg)
add_child(fundal_absolut)
var fundal_node = Node2D.new()
fundal_node.set_script(preload("res://backround.gd"))
fundal_absolut.add_child(fundal_node)
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
# container central care tine tot meniul pe mijlocul ecranului
var center = CenterContainer.new()
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(center)
main_vbox = VBoxContainer.new()
main_vbox.alignment = BoxContainer.ALIGNMENT_CENTER
main_vbox.add_theme_constant_override("separation", 25)
center.add_child(main_vbox)
# afisam numele jucatorului curent cu majuscule
var user_display = Label.new()
user_display.text = GameManager.player_name.to_upper()
user_display.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
user_display.add_theme_font_size_override("font_size", 42)
main_vbox.add_child(user_display)
# campul de text pentru schimbarea numelui
name_edit = LineEdit.new()
name_edit.placeholder_text = "SCRIE NUMELE PENTRU SALVARE..."
name_edit.custom_minimum_size = Vector2(350, 45)
name_edit.alignment = HORIZONTAL_ALIGNMENT_CENTER
name_edit.text = GameManager.player_name if GameManager.player_name != "Guest" else ""
# actualizam numele in timp real pe masura ce se scrie
name_edit.text_changed.connect(func(new_name):
GameManager.set_player_name(new_name)
user_display.text = GameManager.player_name.to_upper()
)
# la enter sau la pierderea focusului salvam si recontruim butoanele de nivel
name_edit.text_submitted.connect(func(_new_text):
SaveManager.login_user(GameManager.player_name)
_rebuild_level_buttons()
)
name_edit.focus_exited.connect(func():
SaveManager.login_user(GameManager.player_name)
_rebuild_level_buttons()
)
main_vbox.add_child(name_edit)
# eticheta pentru sectiunea de dificultate
var start_label = Label.new()
start_label.text = "START GAME"
start_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
start_label.add_theme_font_size_override("font_size", 32)
start_label.add_theme_color_override("font_color", Color("ffd700"))
main_vbox.add_child(start_label)
var hbox_diff = HBoxContainer.new()
hbox_diff.alignment = BoxContainer.ALIGNMENT_CENTER
hbox_diff.add_theme_constant_override("separation", 15)
main_vbox.add_child(hbox_diff)
# grup de butoane ca sa se comporte ca radio buttons
var dificultate_group = ButtonGroup.new()
var btn_noob = Button.new()
btn_noob.text = "NOOB MODE"
btn_noob.custom_minimum_size = Vector2(180, 55)
btn_noob.toggle_mode = true
btn_noob.button_group = dificultate_group
# text verde cand e selectat
btn_noob.add_theme_color_override("font_pressed_color", Color("aaffaa"))
btn_noob.pressed.connect(_on_noob_pressed)
hbox_diff.add_child(btn_noob)
var btn_pro = Button.new()
btn_pro.text = "PRO MODE"
btn_pro.custom_minimum_size = Vector2(180, 55)
btn_pro.toggle_mode = true
btn_pro.button_group = dificultate_group
# text rosu cand e selectat
btn_pro.add_theme_color_override("font_pressed_color", Color("ffaaaa"))
btn_pro.pressed.connect(_on_pro_pressed)
hbox_diff.add_child(btn_pro)
# marcam butonul corespunzator dificultatii salvate in GameManager
if GameManager.is_noob_mode:
btn_noob.button_pressed = true
else:
btn_pro.button_pressed = true
# eticheta pentru sectiunea de selectare nivel
var level_label = Label.new()
level_label.text = "SELECT LEVEL"
level_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
level_label.add_theme_font_size_override("font_size", 28)
level_label.add_theme_color_override("font_color", Color("aaaaaa"))
main_vbox.add_child(level_label)
hbox_levels = HBoxContainer.new()
hbox_levels.alignment = BoxContainer.ALIGNMENT_CENTER
hbox_levels.add_theme_constant_override("separation", 15)
main_vbox.add_child(hbox_levels)
# incarcam profilul si generam butoanele de nivel
SaveManager.login_user(GameManager.player_name)
_rebuild_level_buttons()
# butoane pentru customize si leaderboard
var hbox_menu_options = HBoxContainer.new()
hbox_menu_options.alignment = BoxContainer.ALIGNMENT_CENTER
hbox_menu_options.add_theme_constant_override("separation", 15)
main_vbox.add_child(hbox_menu_options)
var btn_customize = Button.new()
btn_customize.text = "CUSTOMISE CHARACTER"
btn_customize.custom_minimum_size = Vector2(220, 55)
btn_customize.pressed.connect(func():
GameManager.set_player_name(name_edit.text)
SaveManager.login_user(GameManager.player_name)
get_tree().change_scene_to_file("res://customise_menu.tscn")
)
hbox_menu_options.add_child(btn_customize)
var btn_leaderboard = Button.new()
btn_leaderboard.text = "LEADERBOARD"
btn_leaderboard.custom_minimum_size = Vector2(180, 55)
btn_leaderboard.pressed.connect(_on_leaderboard_pressed)
hbox_menu_options.add_child(btn_leaderboard)
# butoane pentru settings si quit pe acelasi rand
var hbox_sys_options = HBoxContainer.new()
hbox_sys_options.alignment = BoxContainer.ALIGNMENT_CENTER
hbox_sys_options.add_theme_constant_override("separation", 15)
main_vbox.add_child(hbox_sys_options)
var btn_settings = Button.new()
btn_settings.text = "SETTINGS"
btn_settings.custom_minimum_size = Vector2(192, 45)
btn_settings.pressed.connect(func():
get_tree().change_scene_to_file("res://settings_menu.tscn")
)
hbox_sys_options.add_child(btn_settings)
var btn_quit = Button.new()
btn_quit.text = "QUIT"
btn_quit.custom_minimum_size = Vector2(192, 45)
btn_quit.pressed.connect(func(): get_tree().quit())
hbox_sys_options.add_child(btn_quit)
func _on_leaderboard_pressed():
# ascundem meniul principal si afisam clasamentul
main_vbox.hide()
leaderboard_overlay = VBoxContainer.new()
leaderboard_overlay.alignment = BoxContainer.ALIGNMENT_CENTER
leaderboard_overlay.add_theme_constant_override("separation", 15)
main_vbox.get_parent().add_child(leaderboard_overlay)
var l_title = Label.new()
l_title.text = "TOP SCORURI"
l_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
l_title.add_theme_font_size_override("font_size", 40)
leaderboard_overlay.add_child(l_title)
# citim fisierul de save si extragem lista de jucatori
var path = "user://savegame.json"
var jucatori: Array = []
if FileAccess.file_exists(path):
var file = FileAccess.open(path, FileAccess.READ)
var json_text = file.get_as_text()
file.close()
var parsed_data = JSON.parse_string(json_text)
if parsed_data is Array:
jucatori = parsed_data
# sortam descrescator dupa high score
jucatori.sort_custom(func(a, b):
return float(a.get("high_score", 0)) > float(b.get("high_score", 0))
)
# afisam maxim 5 jucatori
var limita = clamp(jucatori.size(), 0, 5)
for i in range(limita):
var j = jucatori[i]
var lbl = Label.new()
var nume = j.get("player_name", "Anonim")
var scor = int(j.get("high_score", 0))
lbl.text = "%d. %s -- %d pct" % [i + 1, nume, scor]
lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
lbl.add_theme_font_size_override("font_size", 28)
leaderboard_overlay.add_child(lbl)
# mesaj daca nu exista niciun jucator salvat
if limita == 0:
var lbl_gol = Label.new()
lbl_gol.text = "Niciun jucator inregistrat inca."
lbl_gol.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
lbl_gol.add_theme_font_size_override("font_size", 24)
leaderboard_overlay.add_child(lbl_gol)
# spatiu vizual inainte de butonul de inapoi
var spacer = Control.new()
spacer.custom_minimum_size = Vector2(0, 20)
leaderboard_overlay.add_child(spacer)
var btn_back = Button.new()
btn_back.text = "Inapoi"
btn_back.custom_minimum_size = Vector2(180, 50)
btn_back.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
btn_back.add_theme_font_size_override("font_size", 24)
btn_back.pressed.connect(_on_close_leaderboard)
leaderboard_overlay.add_child(btn_back)
func _on_close_leaderboard():
# stergem overlay-ul si redam meniul principal
if is_instance_valid(leaderboard_overlay):
leaderboard_overlay.queue_free()
main_vbox.show()
func _rebuild_level_buttons():
# stergem butoanele vechi inainte sa le recreem
for child in hbox_levels.get_children():
child.queue_free()
var nivel_deblocat = SaveManager.get_nivel_deblocat()
# generam cate un buton pentru fiecare nivel, blocat sau nu
for i in range(1, 4):
var btn_level = Button.new()
btn_level.custom_minimum_size = Vector2(120, 55)
if i <= nivel_deblocat:
btn_level.text = "LEVEL %d" % i
btn_level.pressed.connect(_on_level_pressed.bind(i))
else:
btn_level.text = "LEVEL %d" % i
btn_level.pressed.connect(_on_level_locked.bind(i))
hbox_levels.add_child(btn_level)
# butonul de demo e mereu disponibil
var btn_demo = Button.new()
btn_demo.text = "DEMO"
btn_demo.custom_minimum_size = Vector2(120, 55)
btn_demo.pressed.connect(func():
GameManager.nivel_curent = 0
get_tree().change_scene_to_file("res://Levels.tscn")
)
hbox_levels.add_child(btn_demo)
func _asigura_tasta_initiala(nume_actiune: String, tasta_fizica: Key):
# adaugam actiunea daca lipseste si ii asignam tasta implicita
if not InputMap.has_action(nume_actiune):
InputMap.add_action(nume_actiune)
if InputMap.action_get_events(nume_actiune).size() == 0:
var event = InputEventKey.new()
event.keycode = tasta_fizica
InputMap.action_add_event(nume_actiune, event)
func _on_noob_pressed():
# setam modul noob in GameManager
GameManager.set_difficulty(true)
func _on_pro_pressed():
# setam modul pro in GameManager
GameManager.set_difficulty(false)
func _on_level_pressed(level: int):
# setam nivelul ales si incarcam scena
GameManager.nivel_curent = level
get_tree().change_scene_to_file("res://Levels.tscn")
func _on_level_locked(level: int):
# afisam un mesaj daca jucatorul incearca sa acceseze un nivel blocat
_show_notification("LEVEL %d IS LOCKED!" % level)
func _show_notification(message: String):
# stergem notificarea anterioara daca exista
var old = get_node_or_null("Notification")
if old: old.queue_free()
var notif = Label.new()
notif.name = "Notification"
notif.text = message
notif.add_theme_font_size_override("font_size", 26)
notif.add_theme_color_override("font_color", Color("ff4444"))
notif.set_anchors_and_offsets_preset(Control.PRESET_CENTER_BOTTOM)
notif.position.y -= 80
add_child(notif)
# stergem notificarea dupa 2 secunde
get_tree().create_timer(2.0).timeout.connect(func(): if is_instance_valid(notif): notif.queue_free())