160 lines
5.5 KiB
GDScript
160 lines
5.5 KiB
GDScript
extends Control
|
|
|
|
## Menu provisional sin seleccion de personaje, progreso ni lista manual de niveles.
|
|
## Solo descubre escenas directamente dentro de levels, nunca sus subcarpetas.
|
|
const LEVEL_DIRECTORY: String = "res://levels"
|
|
|
|
@onready var title_label: Label = $Margin/Layout/Title
|
|
@onready var home_panel: VBoxContainer = $Margin/Layout/Home
|
|
@onready var levels_panel: VBoxContainer = $Margin/Layout/Levels
|
|
@onready var level_list: VBoxContainer = $Margin/Layout/Levels/Scroll/LevelList
|
|
@onready var empty_label: Label = $Margin/Layout/Levels/Empty
|
|
@onready var play_button: Button = $Margin/Layout/Home/Play
|
|
@onready var quit_button: Button = $Margin/Layout/Home/Quit
|
|
@onready var back_button: Button = $Margin/Layout/Levels/Back
|
|
@onready var error_label: Label = $Margin/Layout/Error
|
|
@onready var loading_panel: ColorRect = $Loading
|
|
@onready var loading_label: Label = $Loading/Content/Status
|
|
@onready var loading_progress: ProgressBar = $Loading/Content/Progress
|
|
|
|
var _loading_path: String = ""
|
|
|
|
|
|
func _ready() -> void:
|
|
set_process(false)
|
|
play_button.pressed.connect(_show_levels)
|
|
back_button.pressed.connect(_show_home)
|
|
quit_button.pressed.connect(_quit_game)
|
|
# En web no se puede cerrar la pestana desde el juego.
|
|
quit_button.visible = not OS.has_feature("web")
|
|
_show_home()
|
|
|
|
|
|
## ResourceLoader conserva los nombres originales tambien dentro del paquete exportado.
|
|
## El filtro no es recursivo y ordenar evita diferencias entre plataformas.
|
|
static func discover_level_paths() -> PackedStringArray:
|
|
var paths: Array[String] = []
|
|
for entry: String in ResourceLoader.list_directory(LEVEL_DIRECTORY):
|
|
if entry.ends_with("/") or entry.get_extension().to_lower() not in ["tscn", "scn"]:
|
|
continue
|
|
paths.append(LEVEL_DIRECTORY.path_join(entry))
|
|
paths.sort_custom(func(a: String, b: String) -> bool: return a.naturalnocasecmp_to(b) < 0)
|
|
return PackedStringArray(paths)
|
|
|
|
|
|
static func level_label(path: String) -> String:
|
|
var filename: String = path.get_file().get_basename()
|
|
if filename == "level_00":
|
|
return "NIVEL 00 - BETA / PRUEBAS"
|
|
if filename == "level_01":
|
|
return "NIVEL 01 - PASAJES DEL FEEDBACK"
|
|
return filename.replace("level_", "nivel_").replace("_", " ").to_upper()
|
|
|
|
|
|
func _show_home() -> void:
|
|
if not _loading_path.is_empty():
|
|
return
|
|
home_panel.show()
|
|
levels_panel.hide()
|
|
loading_panel.hide()
|
|
error_label.hide()
|
|
title_label.text = "INICIO"
|
|
play_button.grab_focus()
|
|
|
|
|
|
## Reconstruye al abrir el submenu; no carga ni instancia los niveles para enumerarlos.
|
|
func _show_levels() -> void:
|
|
if not _loading_path.is_empty():
|
|
return
|
|
for child: Node in level_list.get_children():
|
|
level_list.remove_child(child)
|
|
child.queue_free()
|
|
var paths: PackedStringArray = discover_level_paths()
|
|
for path: String in paths:
|
|
var button := Button.new()
|
|
button.text = level_label(path)
|
|
button.tooltip_text = path.get_file()
|
|
button.custom_minimum_size.y = 64.0
|
|
button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
# El texto no fuerza el ancho: sigue cabiendo en pantallas estrechas.
|
|
button.clip_text = true
|
|
button.pressed.connect(_load_level.bind(path))
|
|
level_list.add_child(button)
|
|
empty_label.visible = paths.is_empty()
|
|
home_panel.hide()
|
|
levels_panel.show()
|
|
error_label.hide()
|
|
title_label.text = "ELEGIR NIVEL"
|
|
if level_list.get_child_count() > 0:
|
|
(level_list.get_child(0) as Button).grab_focus()
|
|
else:
|
|
back_button.grab_focus()
|
|
|
|
|
|
## La pantalla de carga real no introduce otros diez segundos artificiales.
|
|
func _load_level(path: String) -> void:
|
|
if not _loading_path.is_empty():
|
|
return
|
|
if not ResourceLoader.exists(path, "PackedScene"):
|
|
_show_load_error("No se encuentra el nivel: " + path.get_file())
|
|
return
|
|
var error: Error = ResourceLoader.load_threaded_request(path, "PackedScene")
|
|
if error != OK:
|
|
_show_load_error("No se pudo iniciar la carga: " + error_string(error))
|
|
return
|
|
_loading_path = path
|
|
# Ocultar el panel tambien evita activaciones por teclado mientras se carga.
|
|
levels_panel.hide()
|
|
loading_panel.show()
|
|
loading_label.text = "Cargando " + level_label(path).to_lower() + "..."
|
|
loading_progress.value = 0.0
|
|
set_process(true)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if _loading_path.is_empty():
|
|
return
|
|
var progress: Array = []
|
|
var status: ResourceLoader.ThreadLoadStatus = ResourceLoader.load_threaded_get_status(_loading_path, progress)
|
|
if not progress.is_empty():
|
|
loading_progress.value = float(progress[0]) * 100.0
|
|
match status:
|
|
ResourceLoader.THREAD_LOAD_LOADED:
|
|
var level: PackedScene = ResourceLoader.load_threaded_get(_loading_path) as PackedScene
|
|
if level == null:
|
|
_show_load_error("El recurso no es una escena de nivel.")
|
|
return
|
|
set_process(false)
|
|
var error: Error = get_tree().change_scene_to_packed(level)
|
|
if error != OK:
|
|
_show_load_error("No se pudo abrir el nivel: " + error_string(error))
|
|
ResourceLoader.THREAD_LOAD_FAILED, ResourceLoader.THREAD_LOAD_INVALID_RESOURCE:
|
|
_show_load_error("Fallo al cargar " + _loading_path.get_file())
|
|
|
|
|
|
func _show_load_error(message: String) -> void:
|
|
_loading_path = ""
|
|
set_process(false)
|
|
loading_panel.hide()
|
|
_show_levels()
|
|
error_label.text = message
|
|
error_label.show()
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("ui_cancel") and _loading_path.is_empty() and levels_panel.visible:
|
|
_show_home()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_WM_GO_BACK_REQUEST and is_node_ready() and _loading_path.is_empty():
|
|
if levels_panel.visible:
|
|
_show_home()
|
|
else:
|
|
_quit_game()
|
|
|
|
|
|
func _quit_game() -> void:
|
|
get_tree().quit()
|