Files
ichni_Official/Assets/Scripts/Menu/MenuManager.cs

157 lines
6.1 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
using AK.Wwise;
2025-08-11 14:04:06 -04:00
using Ichni.Menu;
using Ichni.Menu.UI;
using Ichni.Story;
2025-06-14 14:42:49 -04:00
using Ichni.Story.UI;
2025-06-06 10:14:55 -04:00
using Ichni.UI;
2025-06-03 02:42:28 -04:00
using Sirenix.OdinInspector;
2026-03-14 03:13:10 -04:00
using SLSUtilities.WwiseAssistance;
using UniRx;
2025-06-03 02:42:28 -04:00
using UnityEngine;
using UnityEngine.SceneManagement;
2025-06-06 10:14:55 -04:00
using UnityEngine.Serialization;
2025-06-03 02:42:28 -04:00
2025-06-06 10:14:55 -04:00
namespace Ichni
2025-06-03 02:42:28 -04:00
{
public partial class MenuManager : MonoBehaviour
2025-06-06 10:14:55 -04:00
{
public static MenuManager instance;
2025-06-14 14:42:49 -04:00
public StartUIPage startUIPage;
2026-06-15 14:54:30 +08:00
public LoginPage loginPage;
2025-06-14 14:42:49 -04:00
public ChapterSelectionUIPage chapterSelectionUIPage;
public StoryUIPage storyUIPage;
public SelectionBoxUIPage selectionBoxUIPage;
2026-07-05 16:08:23 -04:00
// dialogUIPage 已随旧对话系统移除;阶段 3 将接入新的 VN 对话页面引用
2025-06-14 14:42:49 -04:00
public SongSelectionUIPage songSelectionUIPage;
2025-08-22 14:54:40 -04:00
public TransitionUIPage transitionUIPage;
2025-08-11 14:04:06 -04:00
public SettingsUIPage settingsUIPage;
2025-08-22 14:54:40 -04:00
public List<string> languageList;
public List<string> displayLanguageList;
2025-06-06 10:14:55 -04:00
}
public partial class MenuManager
2025-06-03 02:42:28 -04:00
{
AsyncOperation asyncOperation;
2025-08-27 21:45:18 -04:00
public bool isEnteringGame;
/// <summary>
/// 当前 MenuScene 是否已经完成 GameScene 预加载且尚未处于切场中。
/// TutorialFlowController 在写入“已处理”剧情变量前使用此状态进行最后一次启动前检查。
/// </summary>
public bool CanBeginGame => !isEnteringGame && asyncOperation != null;
2025-06-03 02:42:28 -04:00
private void Awake()
{
2025-06-06 10:14:55 -04:00
instance = this;
}
private void Start()
{
2025-08-27 21:45:18 -04:00
isEnteringGame = false;
InformationTransistor information = InformationTransistor.instance;
MenuReturnDestination returnDestination = information != null
? information.menuReturnDestination
: MenuReturnDestination.None;
if (returnDestination == MenuReturnDestination.SongSelection &&
information != null && information.chapter != null &&
information.song != null && information.difficulty != null)
2025-08-27 21:45:18 -04:00
{
startUIPage.mainCanvasGroup.gameObject.SetActive(false);
ChapterSelectionManager.instance.currentChapter =
information.chapter;
2026-03-14 03:13:10 -04:00
AudioManager.SetSwitch(ChapterSelectionManager.instance.currentChapter.chapterSwitch);
MenuInformationRecorder.instance.songSelectionRecords[
ChapterSelectionManager.instance.currentChapter] =
new SongSelectionRecord(information.song, information.difficulty);
2025-08-27 21:45:18 -04:00
songSelectionUIPage.FadeIn();
}
else if (returnDestination == MenuReturnDestination.Story &&
information != null && information.chapter != null)
2025-09-06 21:58:48 -04:00
{
startUIPage.mainCanvasGroup.gameObject.SetActive(false);
ChapterSelectionManager.instance.currentChapter = information.chapter;
2026-03-14 03:13:10 -04:00
AudioManager.SetSwitch(ChapterSelectionManager.instance.currentChapter.chapterSwitch);
// MenuScene 会在教程游玩后重新创建,必须显式重建章节故事树,
// 不能依赖旧场景中残留的 Block 实例。
StoryManager.instance?.OpenChapter(ChapterSelectionManager.instance.currentChapter.chapterIndex);
2025-09-06 21:58:48 -04:00
storyUIPage.FadeIn();
}
// 返回目标是一次性运行态信息。无论是否能恢复页面,都不能让它在下次重进菜单时重复生效。
information?.ClearMenuReturnDestination();
2025-08-27 21:45:18 -04:00
2025-08-22 14:54:40 -04:00
Application.targetFrameRate = SettingsManager.instance.gameSettings.targetFrame;
2025-06-06 10:14:55 -04:00
asyncOperation = SceneManager.LoadSceneAsync("GameScene");
asyncOperation.allowSceneActivation = false;
2025-06-03 02:42:28 -04:00
}
}
2025-06-06 10:14:55 -04:00
public partial class MenuManager
2025-06-03 02:42:28 -04:00
{
2025-09-06 21:58:48 -04:00
public void EnterGame()
{
EnterGame(MenuReturnDestination.SongSelection);
}
/// <summary>
/// 激活已预加载的 GameScene并写入唯一的菜单返回目标。
/// 普通选曲使用 <see cref="MenuReturnDestination.SongSelection"/>,教程使用
/// <see cref="MenuReturnDestination.Story"/>。
/// </summary>
public void EnterGame(MenuReturnDestination returnDestination)
{
InformationTransistor.instance?.SetMenuReturnDestination(returnDestination);
2025-09-06 21:58:48 -04:00
EnterGameScene();
}
/// <summary>
/// 为 TutorialBlock 等非选曲入口准备上下文并播放通用转场。
/// 运行成功后会返回 true返回 false 时不会修改剧情变量或存档,调用方应保留原状。
/// </summary>
public bool TryBeginGame(ChapterSelectionUnit chapter, SongItemData song,
DifficultyData difficulty, MenuReturnDestination returnDestination, UIPageBase sourcePage)
{
if (!CanBeginGame)
{
Debug.LogWarning("[MenuManager] GameScene 未完成预加载或菜单已在切换,忽略启动请求。");
return false;
}
if (InformationTransistor.instance == null ||
!InformationTransistor.instance.PrepareGameLaunch(chapter, song, difficulty, returnDestination))
{
return false;
}
isEnteringGame = true;
sourcePage?.FadeOut();
transitionUIPage?.FadeIn();
AudioManager.Post(AK.EVENTS.ENTERTOGAME);
Observable.Timer(TimeSpan.FromSeconds(0.6f)).Subscribe(_ => EnterGameScene());
return true;
}
2025-09-06 21:58:48 -04:00
public void EnterGameScene()
2025-06-03 02:42:28 -04:00
{
if (asyncOperation == null)
{
Debug.LogError("[MenuManager] GameScene 尚未完成预加载,无法进入游戏。");
isEnteringGame = false;
return;
}
MenuInputManager.instance?.gameInput?.Menu.Disable();
2025-06-06 10:14:55 -04:00
asyncOperation.allowSceneActivation = true;
2025-06-03 02:42:28 -04:00
}
}
}