290 lines
9.2 KiB
GDScript
290 lines
9.2 KiB
GDScript
extends CanvasLayer
|
|
|
|
## Pausa y resultados compartidos por todos los LevelRoot. No modifica las reglas de combate.
|
|
## Los hallazgos de raros solo se confirman al superar el nivel.
|
|
const MENU_PATH: String = "res://ui/menu/main_menu.tscn"
|
|
const MENU_SCRIPT: Script = preload("res://ui/menu/main_menu.gd")
|
|
|
|
enum Screen { PLAYING, PAUSED, CONFIRM_EXIT, DEFEAT, VICTORY }
|
|
|
|
var _screen: Screen = Screen.PLAYING
|
|
var _exit_to_levels: bool = false
|
|
var _leaving: bool = false
|
|
var _notice_seconds: float = 0.0
|
|
var _next_level_path: String = ""
|
|
|
|
@onready var pause_button: Button = $Overlay/Pause
|
|
@onready var scrim: ColorRect = $Overlay/Scrim
|
|
@onready var dialog: CenterContainer = $Overlay/Dialog
|
|
@onready var heading: Label = $Overlay/Dialog/Panel/Margin/Options/Heading
|
|
@onready var description: Label = $Overlay/Dialog/Panel/Margin/Options/Description
|
|
@onready var resume_button: Button = $Overlay/Dialog/Panel/Margin/Options/Resume
|
|
@onready var restart_button: Button = $Overlay/Dialog/Panel/Margin/Options/Restart
|
|
@onready var next_button: Button = $Overlay/Dialog/Panel/Margin/Options/Next
|
|
@onready var levels_button: Button = $Overlay/Dialog/Panel/Margin/Options/Levels
|
|
@onready var title_button: Button = $Overlay/Dialog/Panel/Margin/Options/Title
|
|
@onready var confirm_button: Button = $Overlay/Dialog/Panel/Margin/Options/Confirm
|
|
@onready var cancel_button: Button = $Overlay/Dialog/Panel/Margin/Options/Cancel
|
|
|
|
|
|
func _ready() -> void:
|
|
pause_button.pressed.connect(pause_game)
|
|
resume_button.pressed.connect(resume_game)
|
|
restart_button.pressed.connect(_restart_level)
|
|
next_button.pressed.connect(_load_next_level)
|
|
levels_button.pressed.connect(_select_levels)
|
|
title_button.pressed.connect(_select_title)
|
|
confirm_button.pressed.connect(_confirm_exit)
|
|
cancel_button.pressed.connect(_cancel_exit)
|
|
var level: Node = get_parent()
|
|
if level.has_signal("progress_notice"):
|
|
level.connect("progress_notice", _show_notice)
|
|
if level.has_signal("rare_items_changed"):
|
|
level.connect("rare_items_changed", _refresh_pending)
|
|
if level.has_signal("level_failed"):
|
|
level.connect("level_failed", show_defeat)
|
|
if level.has_signal("level_completed"):
|
|
level.connect("level_completed", show_victory)
|
|
_refresh_pending()
|
|
_update_screen()
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("ui_cancel") and not event.is_echo():
|
|
_handle_back()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_WM_GO_BACK_REQUEST and is_node_ready():
|
|
_handle_back()
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
# No deja el SceneTree pausado si otro sistema descarga el nivel.
|
|
if not Engine.is_editor_hint():
|
|
get_tree().paused = false
|
|
|
|
|
|
func _handle_back() -> void:
|
|
match _screen:
|
|
Screen.PLAYING:
|
|
pause_game()
|
|
Screen.PAUSED:
|
|
resume_game()
|
|
Screen.CONFIRM_EXIT:
|
|
_cancel_exit()
|
|
# No es posible cerrar una pantalla de resultado sin elegir destino.
|
|
Screen.DEFEAT, Screen.VICTORY:
|
|
pass
|
|
|
|
|
|
func pause_game() -> void:
|
|
if _leaving or _screen != Screen.PLAYING:
|
|
return
|
|
_release_virtual_actions()
|
|
_screen = Screen.PAUSED
|
|
_update_screen()
|
|
get_tree().paused = true
|
|
resume_button.grab_focus()
|
|
|
|
|
|
func resume_game() -> void:
|
|
if _leaving or _screen != Screen.PAUSED:
|
|
return
|
|
_screen = Screen.PLAYING
|
|
get_tree().paused = false
|
|
_update_screen()
|
|
pause_button.grab_focus()
|
|
|
|
|
|
func show_defeat() -> void:
|
|
if _leaving or _screen == Screen.DEFEAT or _screen == Screen.VICTORY:
|
|
return
|
|
_release_virtual_actions()
|
|
_screen = Screen.DEFEAT
|
|
_update_screen()
|
|
get_tree().paused = true
|
|
restart_button.grab_focus()
|
|
|
|
|
|
func show_victory() -> void:
|
|
if _leaving or _screen == Screen.VICTORY or _screen == Screen.DEFEAT:
|
|
return
|
|
_release_virtual_actions()
|
|
_next_level_path = _get_next_level_path()
|
|
_screen = Screen.VICTORY
|
|
_update_screen()
|
|
get_tree().paused = true
|
|
(next_button if not _next_level_path.is_empty() else levels_button).grab_focus()
|
|
|
|
|
|
func _get_next_level_path() -> String:
|
|
var level: Node = get_parent()
|
|
var current: String = level.scene_file_path
|
|
if current.is_empty():
|
|
return ""
|
|
var paths: PackedStringArray = MENU_SCRIPT.discover_level_paths()
|
|
var index: int = paths.find(current)
|
|
if index < 0 or index >= paths.size() - 1:
|
|
return ""
|
|
return paths[index + 1]
|
|
|
|
|
|
func _update_screen() -> void:
|
|
var playing: bool = _screen == Screen.PLAYING
|
|
pause_button.visible = playing
|
|
scrim.visible = not playing
|
|
dialog.visible = not playing
|
|
$Overlay/Pending.visible = playing and int(get_parent().call("get_pending_rare_count")) > 0
|
|
$Overlay/Notice.visible = playing and _notice_seconds > 0.0
|
|
if playing:
|
|
return
|
|
resume_button.visible = _screen == Screen.PAUSED
|
|
restart_button.visible = _screen == Screen.DEFEAT
|
|
next_button.visible = _screen == Screen.VICTORY
|
|
levels_button.visible = _screen != Screen.CONFIRM_EXIT
|
|
title_button.visible = _screen != Screen.CONFIRM_EXIT
|
|
confirm_button.visible = _screen == Screen.CONFIRM_EXIT
|
|
cancel_button.visible = _screen == Screen.CONFIRM_EXIT
|
|
match _screen:
|
|
Screen.PAUSED:
|
|
heading.add_theme_color_override("font_color", Color(0.88, 0.79, 0.60))
|
|
heading.text = "PAUSA"
|
|
description.text = "Partida en pausa. Puedes continuar o abandonar el intento."
|
|
Screen.CONFIRM_EXIT:
|
|
heading.add_theme_color_override("font_color", Color(1.0, 0.78, 0.48))
|
|
heading.text = "¿ABANDONAR NIVEL?"
|
|
description.text = "Perderás el avance de esta pantalla y los objetos raros aún no confirmados."
|
|
confirm_button.text = "SI, IR AL SELECTOR" if _exit_to_levels else "SI, IR AL TITULO"
|
|
Screen.DEFEAT:
|
|
heading.add_theme_color_override("font_color", Color(0.83, 0.34, 0.32))
|
|
heading.text = "HAS MUERTO..."
|
|
description.text = "El intento ha terminado. Puedes volver a empezar o elegir otro nivel."
|
|
Screen.VICTORY:
|
|
heading.add_theme_color_override("font_color", Color(0.88, 0.78, 0.52))
|
|
heading.text = "¡VICTORIA!"
|
|
if _next_level_path.is_empty():
|
|
description.text = "¡Nivel completado! No quedan mas niveles disponibles por ahora."
|
|
else:
|
|
description.text = "¡Nivel completado! Continúa la aventura o vuelve al menú."
|
|
next_button.disabled = _next_level_path.is_empty()
|
|
next_button.text = "NO HAY MAS NIVELES" if _next_level_path.is_empty() else "SIGUIENTE NIVEL"
|
|
|
|
|
|
func _select_levels() -> void:
|
|
_request_exit(true)
|
|
|
|
|
|
func _select_title() -> void:
|
|
_request_exit(false)
|
|
|
|
|
|
func _request_exit(to_levels: bool) -> void:
|
|
if _leaving:
|
|
return
|
|
_exit_to_levels = to_levels
|
|
if _screen == Screen.PAUSED:
|
|
_screen = Screen.CONFIRM_EXIT
|
|
_update_screen()
|
|
cancel_button.grab_focus()
|
|
else:
|
|
# Los resultados ya han concluido el intento; no hay progreso que perder.
|
|
_confirm_exit()
|
|
|
|
|
|
func _cancel_exit() -> void:
|
|
if _screen != Screen.CONFIRM_EXIT:
|
|
return
|
|
_screen = Screen.PAUSED
|
|
_update_screen()
|
|
resume_button.grab_focus()
|
|
|
|
|
|
func _confirm_exit() -> void:
|
|
if _leaving:
|
|
return
|
|
var session: RunSessionState = get_node_or_null("/root/RunSession") as RunSessionState
|
|
if session != null:
|
|
session.show_levels_on_menu_entry = _exit_to_levels
|
|
_change_scene(MENU_PATH)
|
|
|
|
|
|
func _restart_level() -> void:
|
|
if _screen != Screen.DEFEAT or _leaving:
|
|
return
|
|
_leaving = true
|
|
get_tree().paused = false
|
|
_release_virtual_actions()
|
|
call_deferred("_reload_scene")
|
|
|
|
|
|
func _reload_scene() -> void:
|
|
var error: Error = get_tree().reload_current_scene()
|
|
if error != OK:
|
|
_leaving = false
|
|
push_error("No se pudo reiniciar el nivel: " + error_string(error))
|
|
get_tree().paused = true
|
|
restart_button.grab_focus()
|
|
|
|
|
|
func _load_next_level() -> void:
|
|
if _screen != Screen.VICTORY or _leaving or _next_level_path.is_empty():
|
|
return
|
|
var session: RunSessionState = get_node_or_null("/root/RunSession") as RunSessionState
|
|
var level: Node = get_parent()
|
|
var player: Node = level.get_node_or_null("Player")
|
|
if session != null and player != null:
|
|
var character: CharacterData = player.get("character_data") as CharacterData
|
|
if character != null:
|
|
session.begin_level(_next_level_path, character)
|
|
_change_scene(_next_level_path)
|
|
|
|
|
|
func _change_scene(path: String) -> void:
|
|
if _leaving:
|
|
return
|
|
_leaving = true
|
|
get_tree().paused = false
|
|
_release_virtual_actions()
|
|
call_deferred("_change_scene_deferred", path)
|
|
|
|
|
|
func _change_scene_deferred(path: String) -> void:
|
|
var error: Error = get_tree().change_scene_to_file(path)
|
|
if error != OK:
|
|
_leaving = false
|
|
push_error("No se pudo cambiar de escena: " + error_string(error))
|
|
get_tree().paused = true
|
|
_update_screen()
|
|
|
|
|
|
## Alias util para escenas existentes y pruebas antiguas: ahora confirma antes de salir.
|
|
func return_to_menu() -> void:
|
|
_select_title()
|
|
|
|
|
|
func _release_virtual_actions() -> void:
|
|
for action: StringName in [&"move_left", &"move_right", &"jump", &"attack", &"ground_pound", &"special_attack"]:
|
|
Input.action_release(action)
|
|
|
|
|
|
func _refresh_pending() -> void:
|
|
var level: Node = get_parent()
|
|
var count: int = int(level.call("get_pending_rare_count")) if level.has_method("get_pending_rare_count") else 0
|
|
$Overlay/Pending.text = "RAROS PENDIENTES: %d\nCompleta el nivel para guardarlos" % count
|
|
$Overlay/Pending.visible = count > 0 and _screen == Screen.PLAYING
|
|
|
|
|
|
func _show_notice(message: String) -> void:
|
|
$Overlay/Notice.text = message
|
|
_notice_seconds = 8.0
|
|
$Overlay/Notice.visible = _screen == Screen.PLAYING
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if _notice_seconds > 0.0:
|
|
_notice_seconds -= delta
|
|
if _notice_seconds <= 0.0:
|
|
$Overlay/Notice.hide()
|