Files
A-Fairytale-Gone-Bad-Briar-…/Assets/Scripts/Framework/Registries/CodexRegistry.cs

133 lines
3.8 KiB
C#

using System.Collections.Generic;
using BriarQueen.Framework.Managers.Player.Data;
using UnityEngine;
namespace BriarQueen.Framework.Registries
{
[CreateAssetMenu(menuName = "Briar Queen/Registries/Codex Registry", fileName = "Codex Registry")]
public class CodexRegistry : ScriptableObject
{
[Header("Codex Entries")]
[SerializeField]
private List<CodexEntrySo> _bookEntries = new();
[SerializeField]
private List<CodexEntrySo> _puzzleClues = new();
[SerializeField]
private List<CodexEntrySo> _photoEntries = new();
private Dictionary<string, CodexEntrySo> _entryLookup;
public IReadOnlyList<CodexEntrySo> BookEntries => _bookEntries;
public IReadOnlyList<CodexEntrySo> PuzzleClues => _puzzleClues;
public IReadOnlyList<CodexEntrySo> PhotoEntries => _photoEntries;
public void EnsureInitialized()
{
if (_entryLookup != null)
return;
RebuildLookup();
}
public void RebuildLookup()
{
_entryLookup = new Dictionary<string, CodexEntrySo>();
AddEntries(_bookEntries, "Book Entries");
AddEntries(_puzzleClues, "Puzzle Clues");
AddEntries(_photoEntries, "Photo Entries");
}
private void AddEntries(List<CodexEntrySo> entries, string category)
{
if (entries == null)
return;
foreach (var entry in entries)
{
if (entry == null)
continue;
if (string.IsNullOrWhiteSpace(entry.UniqueID))
{
Debug.LogWarning(
$"[CodexRegistry] Skipping {category} entry '{entry.name}' because UniqueID is empty.",
this);
continue;
}
if (_entryLookup.ContainsKey(entry.UniqueID))
{
Debug.LogError(
$"[CodexRegistry] Duplicate UniqueID detected: '{entry.UniqueID}' from entry '{entry.name}'.",
this);
continue;
}
_entryLookup.Add(entry.UniqueID, entry);
}
}
public bool TryGetEntry(string entryID, out CodexEntrySo entry)
{
EnsureInitialized();
if (string.IsNullOrWhiteSpace(entryID))
{
entry = null;
return false;
}
return _entryLookup.TryGetValue(entryID, out entry);
}
public CodexEntrySo FindEntryByID(string entryID)
{
EnsureInitialized();
return string.IsNullOrWhiteSpace(entryID) ? null : _entryLookup.GetValueOrDefault(entryID);
}
public IEnumerable<CodexEntrySo> GetAllEntries()
{
EnsureInitialized();
return _entryLookup.Values;
}
public IEnumerable<string> GetAllKeys()
{
EnsureInitialized();
return _entryLookup.Keys;
}
public IEnumerable<CodexEntrySo> GetBookEntries()
{
return _bookEntries;
}
public IEnumerable<CodexEntrySo> GetPuzzleClues()
{
return _puzzleClues;
}
public IEnumerable<CodexEntrySo> GetPhotoEntries()
{
return _photoEntries;
}
#if UNITY_EDITOR
public void SetDiscoveredEntries(
List<CodexEntrySo> bookEntries,
List<CodexEntrySo> puzzleClues,
List<CodexEntrySo> photoEntries)
{
_bookEntries = bookEntries ?? new List<CodexEntrySo>();
_puzzleClues = puzzleClues ?? new List<CodexEntrySo>();
_photoEntries = photoEntries ?? new List<CodexEntrySo>();
RebuildLookup();
}
#endif
}
}