Files
aibis-dream/Assets/Scripts/Game Loop/ChapterController.cs
T
2025-12-17 20:52:56 +08:00

146 lines
4.3 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using AibisDream.Kit;
using AibisDream.Utility;
using UnityEngine;
namespace AibisDream
{
public class ChapterController : Singleton<ChapterController>
{
private ChapterData ChapterData => GlobalDataContainer.Instance.GetData<ChapterData>();
/// <summary>
/// 获取章节列表用于显示
/// </summary>
/// <returns>章节列表</returns>
public List<ChapterVo> GetChapterList()
{
// 获取存档并提取章节信息
var cacheChapter = string.Empty;
if (ChapterData.TryGetSaveFilePath(out var saveFilePath))
{
cacheChapter = ParseChapterTitle(saveFilePath);
}
// 拼接章节列表
var chapterList = ChapterData.chapterList
.Where(item => ChapterData.unlockedChapterList.Contains(item.name))
.Select(chapterSo => new ChapterVo
{
chapterSo = chapterSo,
title = chapterSo.title,
chapter = chapterSo.chapter,
desc = chapterSo.description,
pic = chapterSo.coverPic,
hasSaveFile = cacheChapter == chapterSo.name
})
.ToList();
return chapterList;
}
private static string ParseChapterTitle(string filePath)
{
var fileInfo = Path.GetFileName(filePath).Split("_");
return fileInfo.Length >= 2 ? fileInfo[1] : string.Empty;
}
public void StartWithChapter(ChapterVo chapterVo)
{
GameManager.Instance.StartWithLevel(chapterVo.chapterSo);
}
public void StartWithSaveFile()
{
// 获取当前存档
if (!ChapterData.TryGetSaveFilePath(out var saveFilePath))
{
Debug.LogError("未找到存档");
}
else
{
GameManager.Instance.StartWithSaveFile(saveFilePath);
}
}
public void UnlockChapter(TalkSceneSO chapterSo)
{
if (!ChapterData.unlockedChapterList.Contains(chapterSo.name))
{
ChapterData.unlockedChapterList.Add(chapterSo.name);
ChapterData.SaveUnloadChapters();
}
}
}
[GlobalData]
public class ChapterData
{
public readonly TalkSceneSO firstChapter;
public List<TalkSceneSO> chapterList;
public List<string> unlockedChapterList;
public ChapterData()
{
Directory.CreateDirectory(ConstRef.SaveFilePath);
firstChapter = GameManager.Instance.firstTalkSo;
LoadChapterList();
LoadUnlockChapters();
}
private void LoadChapterList()
{
chapterList = new List<TalkSceneSO>();
var curChapter = firstChapter;
do
{
chapterList.Add(curChapter);
curChapter = curChapter.nextScene;
} while (curChapter != null && !chapterList.Contains(curChapter));
}
private void LoadUnlockChapters()
{
unlockedChapterList = File.Exists(ConstRef.ChapterProgressPath)
? JsonUtil.ReadBeanArray<string>(ConstRef.ChapterProgressPath).ToList()
: new List<string> { firstChapter.name };
}
public void SaveUnloadChapters()
{
#if !UNITY_ANDROID || UNITY_EDITOR
JsonUtil.SaveArray(unlockedChapterList.ToArray(), ConstRef.ChapterProgressPath);
#endif
}
public bool TryGetSaveFilePath(out string saveFilePath)
{
var files = Directory.GetFiles(ConstRef.SaveFilePath, "*.json")
.OrderByDescending(fileName => fileName)
.ToArray();
if (files.Length <= 0)
{
saveFilePath = string.Empty;
return false;
}
saveFilePath = files[0];
return true;
}
}
public struct ChapterVo
{
public string title;
public string chapter;
public string desc;
public Sprite pic;
public bool hasSaveFile;
public TalkSceneSO chapterSo;
}
}