261 lines
7.8 KiB
C#
261 lines
7.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class GameManager : Singleton<GameManager>, IGameManager
|
|
{
|
|
#region 运行模式相关
|
|
|
|
[Header("可用so合集")] public TalkSceneSO[] totalSceneSos;
|
|
|
|
public RunMode runMode;
|
|
|
|
[Header("生产环境数据(一般不动)")] public TalkSceneSO firstTalkSo;
|
|
[Header("单场景测试")] public TalkSceneSO testTalkSo;
|
|
[Header("存档测试")] public string saveFileName;
|
|
|
|
#endregion
|
|
|
|
private BindProperty<TalkSceneSO> _currentTalkSceneSo;
|
|
private readonly GameLoopData _data = new();
|
|
|
|
private void Start()
|
|
{
|
|
// 管理游戏开始时各部分的初始化
|
|
SettingLoader.Init();
|
|
// 数据类注册
|
|
StorageSystem.Instance.RegisterData(_data);
|
|
// 场景So事件注册
|
|
_currentTalkSceneSo = new BindProperty<TalkSceneSO>();
|
|
_currentTalkSceneSo.Register(item => _data.curSceneSoName = item.name);
|
|
// 事件广播
|
|
EnumEventSystem.Global.Send(GameLoopEnum.AppStart);
|
|
|
|
#if UNITY_EDITOR
|
|
if (runMode == RunMode.SingleScene)
|
|
{
|
|
StartWithLevel(testTalkSo);
|
|
}
|
|
else if (runMode == RunMode.SaveFile)
|
|
{
|
|
StartWithSaveFile($"{ConstRef.TestSaveFilePath}/{saveFileName}");
|
|
}
|
|
else
|
|
{
|
|
ActionKit.Sequence()
|
|
.Callback(() => UIManager.Instance.GetPanel<PlayToolPanel>().FadeInSync(0))
|
|
.Coroutine(() => SceneLoader.Instance.LoadSceneAsync("ClinicOut"))
|
|
.Callback(() => UIManager.Instance.GetPanel<PlayToolPanel>().FadeOutSync())
|
|
.Start(this);
|
|
}
|
|
#else
|
|
SceneLoader.Instance.LoadScene("ClinicOut");
|
|
runMode = RunMode.Product;
|
|
#endif
|
|
}
|
|
|
|
#region 游戏循环
|
|
|
|
public void StartNewGame()
|
|
{
|
|
// 游戏开始
|
|
EnumEventSystem.Global.Send(GameLoopEnum.GameStart);
|
|
// 序列
|
|
_currentTalkSceneSo.Value = firstTalkSo;
|
|
StartCoroutine(LoadFirstScene());
|
|
}
|
|
|
|
public void StartWithLevel(TalkSceneSO sceneSo)
|
|
{
|
|
// 游戏开始
|
|
EnumEventSystem.Global.Send(GameLoopEnum.GameStart);
|
|
|
|
_currentTalkSceneSo.Value = sceneSo;
|
|
StartCoroutine(LoadFirstScene());
|
|
}
|
|
|
|
public void StartWithSaveFile(string fileName)
|
|
{
|
|
// 游戏开始
|
|
EnumEventSystem.Global.Send(GameLoopEnum.GameStart);
|
|
|
|
StorageSystem.Instance.Load(fileName);
|
|
}
|
|
|
|
private IEnumerator LoadFirstScene()
|
|
{
|
|
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.Value.firstSceneName);
|
|
DialogController.Instance.StartDialog(_currentTalkSceneSo.Value.yarnProject);
|
|
}
|
|
|
|
public void PauseGame()
|
|
{
|
|
// 系统暂停
|
|
Time.timeScale = 0;
|
|
// 交互继续
|
|
EnumEventSystem.Global.Send(GameLoopEnum.PauseGame);
|
|
// 音频全部暂停
|
|
AudioManager.Instance.PauseAll();
|
|
// 关闭Dialog
|
|
DialogCanvasManager.Instance.CloseCanvas();
|
|
}
|
|
|
|
public void ContinueGame()
|
|
{
|
|
// 系统继续
|
|
Time.timeScale = 1;
|
|
// 交互继续
|
|
EnumEventSystem.Global.Send(GameLoopEnum.UnPauseGame);
|
|
// 音频继续
|
|
AudioManager.Instance.UnPauseAll();
|
|
// 打开Dialog
|
|
DialogCanvasManager.Instance.OpenCanvas();
|
|
}
|
|
|
|
public void QuitGame()
|
|
{
|
|
// 系统继续
|
|
Time.timeScale = 1;
|
|
// 交互继续
|
|
EnumEventSystem.Global.Send(GameLoopEnum.GameQuit);
|
|
// 卸载当前场景
|
|
StartCoroutine(RestartCoroutine());
|
|
// 音频继续
|
|
AudioManager.Instance.UnPauseAll();
|
|
// 打开Dialog
|
|
DialogCanvasManager.Instance.OpenCanvas();
|
|
}
|
|
|
|
public void QuitApp()
|
|
{
|
|
Application.Quit();
|
|
}
|
|
|
|
private IEnumerator RestartCoroutine()
|
|
{
|
|
yield return UIManager.Instance.GetPanel<PlayToolPanel>().FadeInAsync();
|
|
|
|
// 执行场景切换等功能
|
|
yield return SceneLoader.Instance.LoadSceneAsync("ClinicOut");
|
|
DialogController.Instance.StopDialog();
|
|
EnumEventSystem.Global.Send(GameLoopEnum.GameQuit);
|
|
|
|
yield return UIManager.Instance.GetPanel<PlayToolPanel>().FadeOutAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 分辨率与屏幕模式
|
|
|
|
public void SetScreenMode(string windowMode)
|
|
{
|
|
switch (windowMode)
|
|
{
|
|
case "FullScreen":
|
|
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
|
|
break;
|
|
case "Windowed":
|
|
Screen.fullScreenMode = FullScreenMode.Windowed;
|
|
Screen.SetResolution(1920, 1080, FullScreenMode.Windowed);
|
|
break;
|
|
case "MaximizedWindow":
|
|
Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
|
|
break;
|
|
default:
|
|
Debug.Log($"{windowMode} err");
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
public IEnumerator NextSceneSo()
|
|
{
|
|
if (_currentTalkSceneSo.Value && _currentTalkSceneSo.Value.nextScene)
|
|
{
|
|
_currentTalkSceneSo.Value = _currentTalkSceneSo.Value.nextScene;
|
|
yield return UIManager.Instance.GetPanel<PlayToolPanel>().FadeInAsync();
|
|
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.Value.firstSceneName);
|
|
DialogController.Instance.StartDialog(_currentTalkSceneSo.Value.yarnProject);
|
|
}
|
|
else
|
|
{
|
|
yield return null;
|
|
Debug.Log("没有下个场景了");
|
|
}
|
|
}
|
|
|
|
public void SetSceneSoByName(string soName)
|
|
{
|
|
_currentTalkSceneSo.Value = Array.Find(totalSceneSos, so => so.name == soName);
|
|
}
|
|
|
|
public void StartDialog()
|
|
{
|
|
DialogController.Instance.LoadDialog(_currentTalkSceneSo.Value.yarnProject);
|
|
}
|
|
|
|
public string GetSceneSoName()
|
|
{
|
|
return _currentTalkSceneSo.Value.name;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// GameManager应该包含什么功能?
|
|
/// 1. 游戏状态管理。开始/暂停/结束
|
|
/// 2. 全局设置 即SettingLoader
|
|
/// 3. 场景管理 有SceneLoader类,暂无必要
|
|
/// 4. 资源管理 似无必要
|
|
/// 5. 时间管理 主要供暂停使用
|
|
/// 6. 日志和调试 日志已有
|
|
/// </summary>
|
|
public interface IGameManager
|
|
{
|
|
#region 游戏循环相关
|
|
|
|
public void StartNewGame();
|
|
public void PauseGame();
|
|
public void ContinueGame();
|
|
public void QuitGame();
|
|
public void QuitApp();
|
|
|
|
#endregion
|
|
}
|
|
|
|
[Serializable]
|
|
[LoadIndex(10)]
|
|
public class GameLoopData : IData
|
|
{
|
|
public string curSceneSoName;
|
|
|
|
public IEnumerator Load()
|
|
{
|
|
GameManager.Instance.SetSceneSoByName(curSceneSoName);
|
|
GameManager.Instance.StartDialog();
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
public static class GameLoopYarnCommand
|
|
{
|
|
[YarnCommand("auto_save")]
|
|
public static void AutoSave()
|
|
{
|
|
StorageSystem.Instance.Save();
|
|
}
|
|
}
|
|
|
|
public enum RunMode
|
|
{
|
|
Product,
|
|
SingleScene,
|
|
SaveFile
|
|
}
|
|
} |