using System.Threading.Tasks; using Godot; namespace Milimoe.GodotGame; public partial class ChapterScene : Node2D, INovelEndEvent, IMenuObject, IFadeInFadeOutBlack { [Export] public PackedScene NovelScene { get; set; } [Export] public PackedScene MenuScene { get; set; } [Export] public CanvasLayer UI { get; set; } [Export] public string ChapterInGame { get; set; } [Export] public string AreaInGame { get; set; } [Export] public string NovelName { get; set; } [Export] public string SceneName { get; set; } private CanvasLayer _novelCanvasLayer; private CanvasLayer _menuCanvasLayer; private Node _novelInterface; private Node _menuInterface; public override async void _Ready() { GameConstant.ChapterInGame = ChapterInGame; GameConstant.AreaInGame = AreaInGame; GameConstant.NovelName = NovelName; GameConstant.SceneName = SceneName; _novelCanvasLayer = GetNode("Novel"); _novelCanvasLayer.Visible = true; ColorRect colorRect = GetNode("ColorRect"); colorRect.SelfModulate = new Color(0, 0, 0, 1); colorRect.Show(); // 实例化小说界面场景 if (NovelScene != null) { _novelInterface = NovelScene.Instantiate(); _novelCanvasLayer.AddChild(_novelInterface); if (_novelInterface is NovelController novelController) { novelController.ChapterObject = this; novelController.MenuObject = this; novelController.FadeObject = this; await FadeOutBlack(1f, true); novelController.InitNovel(NovelName, SceneName); GameConstant.AutoSave(0); } } // 实例化菜单界面场景 if (MenuScene != null) { _menuCanvasLayer = GetNode("Menu"); _menuCanvasLayer.Visible = false; _menuInterface = MenuScene.Instantiate(); if (_menuInterface is MenuController menuController) { menuController.Parent = this; } _menuCanvasLayer.AddChild(_menuInterface); } } public override void _Input(InputEvent @event) { if (@event is InputEventKey keyEvent) { if (keyEvent.Pressed && keyEvent.Keycode == Key.Escape) { if (_menuCanvasLayer != null) { ChangeState(); } } } } public async void OnNovelEnd(CanvasLayer node, string novel_name, string scene_name) { node.Visible = false; await FadeInBlack(1f); await Task.Delay(500); await FadeOutBlack(0.5f, true); if (UI != null) { UI.Visible = true; if (UI.GetNode("UserInterface") is UserInterface userInterface) { if (GameConstant.Characters.Count == 1) { userInterface.CharacterStatus1.Visible = true; TextureRect image = userInterface.CharacterStatus1.GetNode("Image"); using Texture2D texture = GD.Load("res://assets/character/雷恩.png"); image.Texture = texture; } else if (GameConstant.Characters.Count == 2) { userInterface.CharacterStatus1.Visible = true; userInterface.CharacterStatus2.Visible = true; } } } } public async Task FadeInBlack(float fadeTime, bool hide = false) { // 淡入黑色遮罩 ColorRect colorRect = GetNode("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(); } public async Task FadeOutBlack(float fadeTime, bool hide = false) { // 淡出黑色遮罩 ColorRect colorRect = GetNode("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(); } public void ChangeState() { GD.Print("Change Menu State!"); if (_menuCanvasLayer != null) { _menuCanvasLayer.Visible = !_menuCanvasLayer.Visible; } } }