存储功能更新
This commit is contained in:
@@ -17,7 +17,9 @@ namespace AibisDream
|
||||
public class GameLoopManager : Singleton<GameLoopManager>
|
||||
{
|
||||
private const int MaxSaveNum = 200;
|
||||
|
||||
private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles";
|
||||
private static readonly string TestSaveFilePath = Application.streamingAssetsPath + "/TestSaveFiles";
|
||||
|
||||
#region 运行模式相关
|
||||
|
||||
[Header("可用so合集")] public TalkSceneSO[] totalSceneSos;
|
||||
@@ -30,10 +32,15 @@ namespace AibisDream
|
||||
|
||||
#endregion
|
||||
|
||||
private TalkSceneSO _currentTalkSceneSo;
|
||||
private BindProperty<TalkSceneSO> _currentTalkSceneSo;
|
||||
|
||||
private GameLoopData _data = new();
|
||||
|
||||
public void Start()
|
||||
{
|
||||
// 先注册
|
||||
Register();
|
||||
// 加载其他部分
|
||||
#if UNITY_EDITOR
|
||||
if (runMode == RunMode.SingleScene)
|
||||
{
|
||||
@@ -44,7 +51,7 @@ namespace AibisDream
|
||||
{
|
||||
// MainUIController.Instance.TVFadeOut(1);
|
||||
StartMenu.Instance.HideStartMenu();
|
||||
StartCoroutine(LoadSaveFile($"{TestSaveFilePath}/{saveFileName}"));
|
||||
LoadSaveFile($"{TestSaveFilePath}/{saveFileName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -56,6 +63,14 @@ namespace AibisDream
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Register()
|
||||
{
|
||||
// 数据类注册
|
||||
StorageSystem.Instance.RegisterData(_data);
|
||||
// 场景So事件注册
|
||||
_currentTalkSceneSo.Register(item => _data.curSceneSoName = item.name);
|
||||
}
|
||||
|
||||
private IEnumerator LoadClinicOut()
|
||||
{
|
||||
yield return SceneLoader.Instance.LoadSceneAsync("ClinicOut");
|
||||
@@ -64,47 +79,52 @@ namespace AibisDream
|
||||
|
||||
public void StartWithTalkSceneSO(TalkSceneSO sceneSO)
|
||||
{
|
||||
_currentTalkSceneSo = sceneSO;
|
||||
_currentTalkSceneSo.Value = sceneSO;
|
||||
StartCoroutine(LoadScene());
|
||||
}
|
||||
|
||||
public void StartWithSaveFile(string saveFileName)
|
||||
{
|
||||
StartMenu.Instance.HideStartMenu();
|
||||
StartCoroutine(LoadSaveFile(saveFileName));
|
||||
LoadSaveFile(saveFileName);
|
||||
}
|
||||
|
||||
private void LoadSaveFile(string savePath)
|
||||
{
|
||||
StorageSystem.Instance.Load(savePath);
|
||||
}
|
||||
|
||||
public void StartNewGame()
|
||||
{
|
||||
_currentTalkSceneSo = firstTalkSo;
|
||||
_currentTalkSceneSo.Value = firstTalkSo;
|
||||
StartCoroutine(LoadClinicScene());
|
||||
}
|
||||
|
||||
private void StartSingleSceneTest()
|
||||
{
|
||||
_currentTalkSceneSo = testTalkSo;
|
||||
_currentTalkSceneSo.Value = testTalkSo;
|
||||
StartCoroutine(LoadClinicScene());
|
||||
}
|
||||
|
||||
private IEnumerator LoadClinicScene()
|
||||
{
|
||||
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.firstSceneName);
|
||||
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.Value.firstSceneName);
|
||||
StartMenu.Instance.HideStartMenu();
|
||||
DialogController.Instance.StartDialog(_currentTalkSceneSo.yarnProject);
|
||||
DialogController.Instance.StartDialog(_currentTalkSceneSo.Value.yarnProject);
|
||||
}
|
||||
|
||||
private IEnumerator LoadScene()
|
||||
{
|
||||
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.firstSceneName);
|
||||
yield return SceneLoader.Instance.LoadSceneAsync(_currentTalkSceneSo.Value.firstSceneName);
|
||||
StartMenu.Instance.HideStartMenu();
|
||||
DialogController.Instance.StartDialog(_currentTalkSceneSo.yarnProject);
|
||||
DialogController.Instance.StartDialog(_currentTalkSceneSo.Value.yarnProject);
|
||||
}
|
||||
|
||||
public IEnumerator NextSceneSo()
|
||||
{
|
||||
if (_currentTalkSceneSo && _currentTalkSceneSo.nextScene)
|
||||
if (_currentTalkSceneSo.Value && _currentTalkSceneSo.Value.nextScene)
|
||||
{
|
||||
_currentTalkSceneSo = _currentTalkSceneSo.nextScene;
|
||||
_currentTalkSceneSo.Value = _currentTalkSceneSo.Value.nextScene;
|
||||
yield return MainUIController.Instance.FadeInSync(1);
|
||||
yield return LoadClinicScene();
|
||||
// yield return MainUIController.Instance.FadeOutSync(1);
|
||||
@@ -115,7 +135,17 @@ namespace AibisDream
|
||||
Debug.Log("没有下个场景了");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void SetSceneSoByName(string soName)
|
||||
{
|
||||
_currentTalkSceneSo.Value = Array.Find(totalSceneSos, so => so.name == soName);
|
||||
}
|
||||
|
||||
public void StartDialog()
|
||||
{
|
||||
DialogController.Instance.StartDialog(_currentTalkSceneSo.Value.yarnProject);
|
||||
}
|
||||
|
||||
#region 重新开始游戏功能
|
||||
|
||||
/// <summary>
|
||||
@@ -139,141 +169,6 @@ namespace AibisDream
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 存档相关
|
||||
|
||||
private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles";
|
||||
private static readonly string TestSaveFilePath = Application.streamingAssetsPath + "/TestSaveFiles";
|
||||
|
||||
// 维修系统相关数据
|
||||
public static readonly DataContainer FixDataContainer = new();
|
||||
|
||||
// 对话变量存储
|
||||
private VariableStorageBehaviour _dialogStorage;
|
||||
|
||||
// 当前场景
|
||||
private string _curSceneKey;
|
||||
|
||||
// 状态机
|
||||
private string _fixStateArgs;
|
||||
private FixState _fixState;
|
||||
|
||||
public override void OnSingletonInit()
|
||||
{
|
||||
_dialogStorage = FindObjectOfType<VariableStorageBehaviour>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前场景自行更新
|
||||
/// </summary>
|
||||
/// <param name="sceneName">当前scene</param>
|
||||
public void UpdateCurScene(string sceneName)
|
||||
{
|
||||
_curSceneKey = sceneName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 状态机状态更新
|
||||
/// </summary>
|
||||
/// <param name="curState"></param>
|
||||
/// <param name="curArgs"></param>
|
||||
public void UpdateFixState(FixState curState, string curArgs)
|
||||
{
|
||||
_fixState = curState;
|
||||
_fixStateArgs = curArgs;
|
||||
}
|
||||
|
||||
private IEnumerator LoadSaveFile(string savePath)
|
||||
{
|
||||
var saveFile = JsonUtil.ReadJObject(savePath);
|
||||
// 先加载对话数据
|
||||
var floatDict = saveFile["floatDict"]?.ToObject<Dictionary<string, float>>();
|
||||
var stringDict = saveFile["stringDict"]?.ToObject<Dictionary<string, string>>();
|
||||
var boolDict = saveFile["boolDict"]?.ToObject<Dictionary<string, bool>>();
|
||||
_dialogStorage.SetAllVariables(floatDict, stringDict, boolDict);
|
||||
|
||||
// 再加载场景
|
||||
_currentTalkSceneSo = Array.Find(totalSceneSos, so => so.name == saveFile["curTalkSceneSo"]?.ToString());
|
||||
|
||||
yield return SceneLoader.Instance.LoadSceneAsync(saveFile["curSceneKey"]?.ToString());
|
||||
|
||||
if (saveFile["fixDataContainer"] is not JObject fixDataJson)
|
||||
{
|
||||
Debug.Log("维修数据读取问题");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 场景加载完成之后把数据填回去
|
||||
FixDataContainer.LoadByJson(fixDataJson);
|
||||
// 初始化维修状态机
|
||||
_fixState = saveFile["fixState"]!.ToObject<FixState>();
|
||||
_fixStateArgs = saveFile["fixStateArgs"]!.ToString();
|
||||
yield return FixSystemCenter.StateMachine.SwitchState(_fixState, _fixStateArgs);
|
||||
|
||||
// 开启对话
|
||||
DialogController.Instance.LoadDialog(_currentTalkSceneSo.yarnProject);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
JObject saveFile = new JObject();
|
||||
|
||||
// 场景维修相关数据保存
|
||||
var fixData = FixDataContainer.SaveAsJson();
|
||||
saveFile["fixDataContainer"] = fixData;
|
||||
|
||||
// 保存对话变量
|
||||
var (floatDict, stringDict, boolDict) = _dialogStorage.GetAllVariables();
|
||||
saveFile["floatDict"] = JObject.FromObject(floatDict);
|
||||
saveFile["stringDict"] = JObject.FromObject(stringDict);
|
||||
saveFile["boolDict"] = JObject.FromObject(boolDict);
|
||||
|
||||
// 保存场景数据
|
||||
saveFile["curSceneKey"] = _curSceneKey;
|
||||
saveFile["curTalkSceneSo"] = _currentTalkSceneSo.name;
|
||||
|
||||
// 保存状态机数据
|
||||
saveFile["fixState"] = (int)_fixState;
|
||||
saveFile["fixStateArgs"] = _fixStateArgs;
|
||||
|
||||
// 存为Json
|
||||
JsonUtil.SaveJObject(saveFile, GetSavePath());
|
||||
#if !UNITY_EDITOR
|
||||
DeleteOldSave();
|
||||
#endif
|
||||
}
|
||||
|
||||
private string GetSavePath()
|
||||
{
|
||||
string gameStageStr;
|
||||
try
|
||||
{
|
||||
_dialogStorage.TryGetValue("$gameStage", out float gameStage);
|
||||
gameStageStr = gameStage.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_dialogStorage.TryGetValue("$gameStage", out string gameStage);
|
||||
gameStageStr = gameStage;
|
||||
}
|
||||
|
||||
return $"{SaveFilePath}/{DateTime.Now:yyyyMMdd_HHmmss}_{_currentTalkSceneSo.name}_{gameStageStr}";
|
||||
}
|
||||
|
||||
private void DeleteOldSave()
|
||||
{
|
||||
var fileList = Directory.GetFiles(SaveFilePath);
|
||||
if (fileList.Length > MaxSaveNum)
|
||||
{
|
||||
var sorted = fileList.OrderByDescending(f => f).ToArray();
|
||||
for (var i = MaxSaveNum; i < fileList.Length; i++)
|
||||
{
|
||||
File.Delete(sorted[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum RunMode
|
||||
@@ -288,7 +183,21 @@ namespace AibisDream
|
||||
[YarnCommand("auto_save")]
|
||||
public static void AutoSave()
|
||||
{
|
||||
GameLoopManager.Instance.Save();
|
||||
StorageSystem.Instance.Save();
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[LoadIndex(10)]
|
||||
public class GameLoopData : IData
|
||||
{
|
||||
public string curSceneSoName;
|
||||
|
||||
public IEnumerator Load()
|
||||
{
|
||||
GameLoopManager.Instance.SetSceneSoByName(curSceneSoName);
|
||||
GameLoopManager.Instance.StartDialog();
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user