131 lines
3.8 KiB
C#
131 lines
3.8 KiB
C#
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<ChapterController>
|
|
{
|
|
private ChapterData ChapterData => GlobalDataContainer.Instance.GetData<ChapterData>();
|
|
|
|
/// <summary>
|
|
/// 获取章节列表用于显示
|
|
/// </summary>
|
|
/// <returns>章节列表</returns>
|
|
public List<ChapterVo> 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,
|
|
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<string> unlockedChapterList;
|
|
|
|
public ChapterData()
|
|
{
|
|
Directory.CreateDirectory(ConstRef.SaveFilePath);
|
|
LoadUnlockChapters();
|
|
}
|
|
|
|
private void LoadUnlockChapters()
|
|
{
|
|
unlockedChapterList = File.Exists(ConstRef.ChapterProgressPath)
|
|
? JsonUtil.ReadBeanArray<string>(ConstRef.ChapterProgressPath).ToList()
|
|
: new List<string> { 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 chapter;
|
|
public string desc;
|
|
public Sprite pic;
|
|
public bool hasSaveFile;
|
|
public TalkSceneSO chapterSo;
|
|
}
|
|
} |