#257: Add Load Game screen to main menu with sorted save list, loading overlay during quickload round-trip, and pending_load_path cross-scene flow. F5/F6 quicksave/quickload were already wired. #561: Move debug_overlay.gd from scripts/ui/ to ui/ for consistency with all other UI components. Update scene and test references. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
117 lines
3.7 KiB
GDScript
117 lines
3.7 KiB
GDScript
extends Control
|
|
## #258: Main menu — New Game / Continue / Load Game / Quit.
|
|
## New Game: generates per-game save directory (D-085), starts game.
|
|
## Continue: loads most recent save directory.
|
|
## Load Game: shows sorted save list for manual selection (#257).
|
|
|
|
const GAME_SCENE := "res://scenes/main.tscn"
|
|
|
|
const BG_COLOR := Color(0.05, 0.05, 0.08, 1.0)
|
|
const TITLE_COLOR := Color("#c8d0e0")
|
|
const SUBTITLE_COLOR := Color("#8890a0")
|
|
const BTN_NORMAL_COLOR := Color("#e8c547")
|
|
const BTN_DISABLED_COLOR := Color("#4a5060")
|
|
const FONT_SIZE_TITLE := 36
|
|
const FONT_SIZE_SUBTITLE := 14
|
|
const FONT_SIZE_BTN := 15
|
|
|
|
@onready var _new_game_btn: Button = $VBox/NewGameBtn
|
|
@onready var _continue_btn: Button = $VBox/ContinueBtn
|
|
@onready var _load_game_btn: Button = $VBox/LoadGameBtn
|
|
@onready var _quit_btn: Button = $VBox/QuitBtn
|
|
@onready var _load_panel: Control = $LoadGamePanel
|
|
@onready var _saves_list: VBoxContainer = $LoadGamePanel/VBox/SavesScroll/SavesList
|
|
@onready var _load_back_btn: Button = $LoadGamePanel/VBox/BackBtn
|
|
|
|
|
|
func _ready() -> void:
|
|
_new_game_btn.pressed.connect(_on_new_game)
|
|
_continue_btn.pressed.connect(_on_continue)
|
|
_load_game_btn.pressed.connect(_on_load_game_browse)
|
|
_quit_btn.pressed.connect(_on_quit)
|
|
_load_back_btn.pressed.connect(_on_load_back)
|
|
_load_panel.visible = false
|
|
_refresh_continue_state()
|
|
|
|
|
|
func _refresh_continue_state() -> void:
|
|
var saves := SessionManager.list_game_dirs()
|
|
_continue_btn.disabled = saves.is_empty()
|
|
_load_game_btn.disabled = saves.is_empty()
|
|
|
|
|
|
func _on_new_game() -> void:
|
|
var game_id := SessionManager.new_game()
|
|
if game_id.is_empty():
|
|
push_error("MainMenu: new_game() failed to create save directory — cannot start")
|
|
return
|
|
get_tree().change_scene_to_file(GAME_SCENE)
|
|
|
|
|
|
func _on_continue() -> void:
|
|
var saves := SessionManager.list_game_dirs()
|
|
if saves.is_empty():
|
|
return
|
|
SessionManager.resume_game(saves[0].game_id)
|
|
get_tree().change_scene_to_file(GAME_SCENE)
|
|
|
|
|
|
func _on_load_game_browse() -> void:
|
|
_build_saves_list()
|
|
_load_panel.visible = true
|
|
|
|
|
|
func _on_load_back() -> void:
|
|
_load_panel.visible = false
|
|
|
|
|
|
func _on_quit() -> void:
|
|
get_tree().quit()
|
|
|
|
|
|
## Build the saves list panel from existing save directories, sorted by date (newest first).
|
|
func _build_saves_list() -> void:
|
|
for child in _saves_list.get_children():
|
|
child.queue_free()
|
|
|
|
var saves := SessionManager.list_game_dirs()
|
|
if saves.is_empty():
|
|
var lbl := Label.new()
|
|
lbl.text = UIStrings.get_text("menu.load_game_empty")
|
|
lbl.add_theme_color_override("font_color", Color(BTN_DISABLED_COLOR))
|
|
lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
_saves_list.add_child(lbl)
|
|
return
|
|
|
|
for save in saves:
|
|
var btn := Button.new()
|
|
btn.text = _format_save_entry(save)
|
|
btn.add_theme_font_size_override("font_size", FONT_SIZE_BTN)
|
|
btn.add_theme_color_override("font_color", BTN_NORMAL_COLOR)
|
|
btn.pressed.connect(_on_save_selected.bind(save))
|
|
_saves_list.add_child(btn)
|
|
|
|
|
|
func _on_save_selected(save: Dictionary) -> void:
|
|
var game_id: String = save.get("game_id", "")
|
|
var save_file: String = save.get("newest_save", "quicksave.sav")
|
|
SessionManager.resume_game(game_id)
|
|
# #257: Signal main.gd to send LOAD_GAME on scene startup
|
|
if not save_file.is_empty():
|
|
GameState.pending_load_path = "user://saves/" + game_id + "/" + save_file
|
|
get_tree().change_scene_to_file(GAME_SCENE)
|
|
|
|
|
|
## Format a save entry for display. game_id format: YYYYMMDD-HHMMSS-hex6.
|
|
static func _format_save_entry(save: Dictionary) -> String:
|
|
var game_id: String = save.get("game_id", "")
|
|
var parts := game_id.split("-")
|
|
if parts.size() >= 2 and parts[0].length() == 8 and parts[1].length() == 6:
|
|
var d := parts[0]
|
|
var t := parts[1]
|
|
return "%s-%s-%s %s:%s:%s" % [
|
|
d.substr(0, 4), d.substr(4, 2), d.substr(6, 2),
|
|
t.substr(0, 2), t.substr(2, 2), t.substr(4, 2),
|
|
]
|
|
return game_id
|