FunGame-Godot/scripts/TitleScreen.cs
2025-04-14 21:54:29 +08:00

173 lines
4.8 KiB
C#

using System.Threading.Tasks;
using Godot;
namespace Milimoe.GodotGame;
public partial class TitleScreen : Node, ISaveSelectionEvent
{
[Export]
public Button StartGameButton;
[Export]
public Button LoadGameButton;
[Export]
public Button GameSettingsButton;
[Export]
public Button ExitGameButton;
[Export]
public ConfirmationDialog StartConfirmationDialog;
[Export]
public ConfirmationDialog ExitConfirmationDialog;
[Export]
public SavedSelection SavedSelection;
public override async void _Ready()
{
ColorRect colorRect = GetNode<ColorRect>("ColorRect");
colorRect.SelfModulate = new Color(0, 0, 0, 1);
colorRect.Show();
// 检查按钮是否已连接
if (StartGameButton != null)
{
StartGameButton.Pressed += OnStartGameButtonPressed;
}
if (LoadGameButton != null)
{
LoadGameButton.Pressed += OnLoadGameButtonPressed;
}
if (GameSettingsButton != null)
{
GameSettingsButton.Pressed += OnGameSettingsButtonPressed;
}
if (ExitGameButton != null)
{
ExitGameButton.Pressed += OnExitGameButtonPressed;
}
if (StartConfirmationDialog != null)
{
StartConfirmationDialog.GetOkButton().Pressed += OnStartGameConfirmation;
}
if (ExitConfirmationDialog != null)
{
ExitConfirmationDialog.GetOkButton().Pressed += () => GetTree().Quit();
}
await FadeOutBlack(1f, true);
}
private async Task FadeInBlack(float fadeTime, bool hide = false)
{
// 淡入黑色遮罩
ColorRect colorRect = GetNode<ColorRect>("ColorRect");
colorRect.SelfModulate = new Color(0, 0, 0, 0);
colorRect.Show();
Tween tween = CreateTween();
tween.TweenProperty(colorRect, "self_modulate", new Color(0, 0, 0, 1), fadeTime);
await ToSignal(tween, "finished");
if (hide) colorRect.Hide();
}
private async Task FadeOutBlack(float fadeTime, bool hide = false)
{
// 淡出黑色遮罩
ColorRect colorRect = GetNode<ColorRect>("ColorRect");
colorRect.SelfModulate = new Color(0, 0, 0, 1);
colorRect.Show();
Tween tween = CreateTween();
tween.TweenProperty(colorRect, "self_modulate", new Color(0, 0, 0, 0), fadeTime);
await ToSignal(tween, "finished");
if (hide) colorRect.Hide();
}
private void OnStartGameButtonPressed()
{
// 开始新游戏
GD.Print("Start New Game!");
StartConfirmationDialog?.PopupCentered();
}
private void OnLoadGameButtonPressed()
{
// 载入游戏
GD.Print("Load Game!");
// 使用动画效果显示存档列表
if (SavedSelection != null)
{
SavedSelection.SetIsSave(false);
SavedSelection.Show();
SavedSelection.GetNode<ColorRect>("ColorRect").GetNode<AnimationPlayer>("AnimationPlayer").Play("flyin");
}
}
private async void OnStartGameConfirmation()
{
await FadeInBlack(1f);
await Task.Delay(1000);
//GetTree().Quit();
// 加载第一章
GD.Print("LoadTitleScene!");
GetTree().ChangeSceneToFile("res://scenes/第一章.tscn");
}
private void OnGameSettingsButtonPressed()
{
// 打开游戏设置
GD.Print("Open Game Settings!");
}
private void OnExitGameButtonPressed()
{
// 退出游戏
if (ExitConfirmationDialog != null)
{
ExitConfirmationDialog.PopupCentered(); // 弹出对话框
}
else
{
GD.PrintErr("ExitConfirmationDialog is not connected!");
GetTree().Quit(); // 如果对话框未连接,则直接退出
}
}
public async void OnSaveSelectionClosed(bool selected, int index, bool isSave)
{
if (SavedSelection != null && !SavedSelection.IsClosing)
{
// 关闭存档选择界面
SavedSelection.IsClosing = true;
SavedSelection.GetNode<ColorRect>("ColorRect").GetNode<AnimationPlayer>("AnimationPlayer").Play("flyout");
await Task.Delay(300);
SavedSelection.IsClosing = false;
SavedSelection.Hide();
if (selected && index >= 0)
{
if (!isSave)
{
// 载入游戏
GD.Print($"Selected Saved: {index}!");
await FadeInBlack(1f);
await Task.Delay(1000);
GetTree().Quit();
}
}
else
{
GD.Print("Close SavedSelection!");
}
}
}
}