using System.Collections.Generic; using System.IO; using System.Linq; using AibisDream.Kit; using AibisDream.SaveSystem; using AibisDream.Utility; using UnityEngine; namespace AibisDream { public class ChapterController : Singleton { private ChapterData ChapterData => GlobalDataContainer.Instance.GetData(); /// /// 获取章节列表用于显示 /// /// 章节列表 public List GetChapterList() { // 获取存档并提取章节信息 var cacheChapter = string.Empty; if (ChapterData.TryGetLatestSaveSceneSoName(out var sceneSoName)) { cacheChapter = sceneSoName; } var sourceList = GameManager.Instance.GetAllSceneSos(); // 拼接章节列表 var chapterList = sourceList .Where(item => ChapterData.unlockedChapterList.Contains(item.name)) .Select(chapterSo => new ChapterVo { chapterSo = chapterSo, title = chapterSo.title, titleKey = chapterSo.titleKey, chapter = chapterSo.chapter, desc = chapterSo.description, pic = chapterSo.coverPic, hasSaveFile = cacheChapter == chapterSo.name }) .ToList(); return chapterList; } public void StartWithChapter(ChapterVo chapterVo) { GameManager.Instance.StartWithLevel(chapterVo.chapterSo); } public void StartWithSaveFile() { var slotIndex = SlotManager.GetLatestSlotIndex() ?? SlotIndex.Auto; if (!SlotDirectory.Exists(slotIndex)) { Debug.LogError("未找到存档"); return; } GameManager.Instance.StartWithSlot(slotIndex); } public void UnlockChapter(TalkSceneSO chapterSo) { if (chapterSo.name == "GeneralEnd") { return; } if (!ChapterData.unlockedChapterList.Contains(chapterSo.name)) { ChapterData.unlockedChapterList.Add(chapterSo.name); ChapterData.SaveUnloadChapters(); } } } [GlobalData] public class ChapterData { public List unlockedChapterList; public ChapterData() { Directory.CreateDirectory(ConstRef.SaveFilePath); LoadUnlockChapters(); } private void LoadUnlockChapters() { unlockedChapterList = File.Exists(ConstRef.ChapterProgressPath) ? JsonUtil.ReadBeanArray(ConstRef.ChapterProgressPath).ToList() : new List { GameManager.Instance.firstTalkSo.name }; } public void SaveUnloadChapters() { JsonUtil.SaveArray(unlockedChapterList.ToArray(), ConstRef.ChapterProgressPath); } public bool TryGetLatestSaveSceneSoName(out string sceneSoName) { sceneSoName = string.Empty; var slotIndex = SlotManager.GetLatestSlotIndex() ?? SlotIndex.Auto; if (!SlotDirectory.Exists(slotIndex)) { return false; } var meta = SlotManager.LoadMeta(slotIndex); if (meta == null || string.IsNullOrEmpty(meta.sceneSoName)) { return false; } sceneSoName = meta.sceneSoName; return true; } } public struct ChapterVo { public string title; public string titleKey; public string chapter; public string desc; public Sprite pic; public bool hasSaveFile; public TalkSceneSO chapterSo; } }