159 lines
7.7 KiB
GDScript
159 lines
7.7 KiB
GDScript
# 文件名: start.gd
|
||
# 场景的根节点脚本,通常用于处理主菜单或启动界面的逻辑。
|
||
extends Node2D
|
||
|
||
# --- 节点引用 ---
|
||
@onready var new_game_button: Button = $UI/Select_List/New
|
||
@onready var load_game_button: Button = $UI/Select_List/Load
|
||
# @onready var error_message_label = $UI/ErrorMessageLabel # 如果有错误提示标签
|
||
|
||
# --- 场景路径 ---
|
||
# const GameScene_path = "res://Entity/Level/GameScene.tscn" # 不再由此脚本直接切换
|
||
const LOADING_SCENE_PATH = "res://Entity/Level/LoadingScene.tscn" # <--- 新增:LoadingScene的路径,请确保正确!
|
||
|
||
# --- Godot 生命周期方法 ---
|
||
|
||
func _ready():
|
||
print("Start Scene: Initializing...")
|
||
# --- 连接 UI 信号 ---
|
||
if new_game_button:
|
||
if not new_game_button.is_connected("pressed", Callable(self, "_on_new_game_pressed")):
|
||
new_game_button.pressed.connect(self._on_new_game_pressed)
|
||
print("Start Scene: Connected 'pressed' signal for New Game button.")
|
||
else:
|
||
printerr("Start Scene Error: New Game button node not found at path specified in @onready var.")
|
||
|
||
if load_game_button:
|
||
if not load_game_button.is_connected("pressed", Callable(self, "_on_load_game_pressed")):
|
||
load_game_button.pressed.connect(self._on_load_game_pressed)
|
||
print("Start Scene: Connected 'pressed' signal for Load Game button.")
|
||
# 示例:根据存档是否存在禁用加载按钮 (需要 SaveLoadManager Autoload)
|
||
# if ProjectSettings.has_setting("autoload/SaveLoadManager"):
|
||
# load_game_button.disabled = !SaveLoadManager.does_save_exist(1) # 假设检查槽位1
|
||
# else:
|
||
# printerr("Start Scene Warning: SaveLoadManager Autoload not found, cannot check save existence for button state.")
|
||
else:
|
||
printerr("Start Scene Error: Load Game button node not found at path specified in @onready var.")
|
||
|
||
# --- 移除 InitializationManager 信号连接 ---
|
||
# 不再需要在此处连接 InitializationManager 的信号,这些将由 LoadingScene 处理。
|
||
# 例如,以下代码需要被移除或注释掉:
|
||
# if ProjectSettings.has_setting("autoload/InitializationManager"):
|
||
# if not InitializationManager.is_connected("new_game_initialized", Callable(self, "_on_initialization_complete")):
|
||
# InitializationManager.new_game_initialized.connect(self._on_initialization_complete)
|
||
# if not InitializationManager.is_connected("game_loaded", Callable(self, "_on_initialization_complete")):
|
||
# InitializationManager.game_loaded.connect(self._on_initialization_complete)
|
||
# else:
|
||
# printerr("Start Scene Error: InitializationManager Autoload is not configured...")
|
||
|
||
|
||
# 当此节点从场景树中移除时调用
|
||
func _exit_tree():
|
||
# --- 移除 InitializationManager 信号断开 ---
|
||
# 同样,不再需要在此处断开 InitializationManager 的信号。
|
||
# if ProjectSettings.has_setting("autoload/InitializationManager"):
|
||
# if InitializationManager.is_connected("new_game_initialized", Callable(self, "_on_initialization_complete")):
|
||
# InitializationManager.new_game_initialized.disconnect(Callable(self, "_on_initialization_complete"))
|
||
# if InitializationManager.is_connected("game_loaded", Callable(self, "_on_initialization_complete")):
|
||
# InitializationManager.game_loaded.disconnect(Callable(self, "_on_initialization_complete"))
|
||
pass # 如果没有其他需要在退出时清理的,可以留空或移除此函数
|
||
|
||
|
||
# --- UI 信号回调函数 ---
|
||
|
||
# 当“新游戏”按钮被按下时调用
|
||
func _on_new_game_pressed():
|
||
print("Start Scene: 'New Game' button pressed. Transitioning to Loading Scene...")
|
||
if new_game_button: new_game_button.disabled = true # 禁用按钮防止重复点击
|
||
if load_game_button: load_game_button.disabled = true
|
||
|
||
# 检查 LoadingContext Autoload 是否存在
|
||
if not ProjectSettings.has_setting("autoload/LoadingContext"):
|
||
printerr("Start Scene CRITICAL Error: LoadingContext Autoload not configured!")
|
||
# 重新启用按钮,让用户可以重试或意识到问题
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
# (可选) 显示错误信息给用户
|
||
# if error_message_label: error_message_label.text = "错误:无法准备加载流程!"
|
||
return
|
||
|
||
# 设置 LoadingContext
|
||
LoadingContext.action_to_perform = "new_game"
|
||
LoadingContext.save_data_for_loading = {} # 确保对于新游戏,存档数据为空
|
||
|
||
# 切换到 LoadingScene
|
||
var err = get_tree().change_scene_to_file(LOADING_SCENE_PATH)
|
||
if err != OK:
|
||
printerr("Start Scene CRITICAL ERROR: Failed to switch to Loading Scene! Error code: " + str(err))
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
# (可选) 显示错误信息给用户
|
||
# if error_message_label: error_message_label.text = "错误:无法打开加载界面!"
|
||
|
||
|
||
# 当“加载游戏”按钮被按下时调用
|
||
func _on_load_game_pressed():
|
||
var slot_to_load = 1 # <-- 您可以根据需要修改或添加选择存档槽的逻辑
|
||
print("Start Scene: 'Load Game' button pressed. Requesting load for slot " + str(slot_to_load) + "...")
|
||
|
||
if new_game_button: new_game_button.disabled = true
|
||
if load_game_button: load_game_button.disabled = true
|
||
|
||
# 检查必要的 Autoloads
|
||
if not ProjectSettings.has_setting("autoload/SaveLoadManager"):
|
||
printerr("Start Scene CRITICAL Error: SaveLoadManager Autoload not configured!")
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
return
|
||
if not ProjectSettings.has_setting("autoload/LoadingContext"):
|
||
printerr("Start Scene CRITICAL Error: LoadingContext Autoload not configured!")
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
return
|
||
|
||
# 1. 尝试从 SaveLoadManager 加载数据
|
||
var loaded_data: Variant = SaveLoadManager.load_game(slot_to_load)
|
||
|
||
# 2. 检查加载是否成功
|
||
if loaded_data == null:
|
||
printerr("Start Scene: Failed to load save data from slot " + str(slot_to_load) + " (SaveLoadManager returned null).")
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
# (可选) 显示错误信息给用户
|
||
# if error_message_label: error_message_label.text = "加载存档失败!"
|
||
return
|
||
|
||
if not loaded_data is Dictionary:
|
||
printerr("Start Scene Error: Loaded data from SaveLoadManager is not a Dictionary. Type:", typeof(loaded_data))
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
return
|
||
|
||
# 3. 设置 LoadingContext
|
||
LoadingContext.action_to_perform = "load_game"
|
||
LoadingContext.save_data_for_loading = loaded_data # 将加载到的字典传递过去
|
||
|
||
# 4. 切换到 LoadingScene
|
||
var err = get_tree().change_scene_to_file(LOADING_SCENE_PATH)
|
||
if err != OK:
|
||
printerr("Start Scene CRITICAL ERROR: Failed to switch to Loading Scene! Error code: " + str(err))
|
||
if new_game_button: new_game_button.disabled = false
|
||
if load_game_button: load_game_button.disabled = false
|
||
|
||
|
||
# --- InitializationManager 信号回调函数 ---
|
||
# func _on_initialization_complete(): # <--- 此方法不再需要,可以安全移除或注释掉
|
||
# print("Start Scene: Received initialization complete signal (New Game or Load).")
|
||
# print("--------------------------------------------------")
|
||
# print("Start Scene: Initialization successful!")
|
||
# print("--------------------------------------------------")
|
||
# Go_To_GameScene()
|
||
|
||
|
||
# --- 辅助函数 ---
|
||
# func Go_To_GameScene(): # <--- 此方法不再需要,因为场景切换由 LoadingScene 处理
|
||
# print("Start Scene: Attempting to switch to Game Scene:", GameScene_path)
|
||
# var err = get_tree().change_scene_to_file(GameScene_path)
|
||
# if err != OK:
|
||
# printerr("Start Scene CRITICAL ERROR: Failed to switch to main game scene!")
|