using System.Collections.Generic; using System.IO; using System.Linq; using AibisDream.Kit; 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.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 chapterList; public List unlockedChapterList; public ChapterData() { Directory.CreateDirectory(ConstRef.SaveFilePath); firstChapter = GameManager.Instance.firstTalkSo; LoadChapterList(); LoadUnlockChapters(); } private void LoadChapterList() { chapterList = new List(); var curChapter = firstChapter; do { chapterList.Add(curChapter); curChapter = curChapter.nextScene; } while (curChapter != null && !chapterList.Contains(curChapter)); } private void LoadUnlockChapters() { unlockedChapterList = JsonUtil.ReadBeanArray(ConstRef.ChapterProgressPath).ToList(); } public void SaveUnloadChapters() { JsonUtil.SaveArray(unlockedChapterList.ToArray(), ConstRef.ChapterProgressPath); } 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; } }