Files
ichni_Official/Assets/Scripts/Menu/MenuInformationRecorder.cs

94 lines
3.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Ichni.Menu
{
public class MenuInformationRecorder : SerializedMonoBehaviour
{
public static MenuInformationRecorder instance;
public Dictionary<ChapterSelectionUnit, SongSelectionRecord> songSelectionRecords;
private void Awake()
{
if (instance == null)
{
instance = this;
songSelectionRecords = new Dictionary<ChapterSelectionUnit, SongSelectionRecord>();
}
else
{
Destroy(gameObject);
}
}
/// <summary>
/// 取得当前 Chapter 的选曲缓存;缓存缺失时创建“第一首有效歌曲 + 难度位置 0”的默认记录。
/// <para>此方法只保证缓存对象可用,不保证该歌曲或难度仍能游玩。歌曲失效与难度回退由选曲 UI 在
/// 每次进入页面时完成,避免把运行期缓存变成持久化存档。</para>
/// </summary>
public bool TryGetRecordOfThisChapter(out SongSelectionRecord record)
{
record = null;
ChapterSelectionUnit currentChapter = ChapterSelectionManager.instance.currentChapter;
if (currentChapter == null || currentChapter.songs == null)
{
return false;
}
if (songSelectionRecords.TryGetValue(currentChapter, out SongSelectionRecord cachedRecord) && cachedRecord != null)
{
record = cachedRecord;
return true;
}
SongItemData firstValidSong = currentChapter.songs.Find(song => song != null);
if (firstValidSong == null)
{
return false;
}
record = new SongSelectionRecord(firstValidSong, 0);
songSelectionRecords[currentChapter] = record;
return true;
}
/// <summary>
/// 将缓存修正为当前 Chapter 中实际存在的歌曲,并保留调用方提供的难度列表偏好位置。
/// 这里只写内存缓存,玩家重启游戏后不会恢复该选择。
/// </summary>
public void SetRecordForChapter(ChapterSelectionUnit chapter, SongItemData song, int difficultyListIndex)
{
if (chapter == null || song == null)
{
return;
}
songSelectionRecords[chapter] = new SongSelectionRecord(song, difficultyListIndex);
}
}
public class SongSelectionRecord
{
public SongItemData song;
public int difficultyListIndex;
/// <summary>
/// 使用难度在歌曲列表中的位置构建缓存。
/// 这是选曲 UI 的临时偏好,不等同于 DifficultyData.saveDifficultyId 的游戏记录稳定 ID。
/// </summary>
public SongSelectionRecord(SongItemData song, int difficultyListIndex)
{
this.song = song;
this.difficultyListIndex = difficultyListIndex;
}
public SongSelectionRecord(SongItemData song, DifficultyData difficulty)
: this(song, song?.difficultyDataList?.IndexOf(difficulty) ?? 0)
{
}
}
}