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>();
RegistryLookupBuilder.AddEntries(
_entryLookup,
_bookEntries,
this,
nameof(CodexRegistry),
"Book Entries",
"UniqueID",
entry => entry.UniqueID,
entry => entry,
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
RegistryLookupBuilder.AddEntries(
_entryLookup,
_puzzleClues,
this,
nameof(CodexRegistry),
"Puzzle Clues",
"UniqueID",
entry => entry.UniqueID,
entry => entry,
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
RegistryLookupBuilder.AddEntries(
_entryLookup,
_photoEntries,
this,
nameof(CodexRegistry),
"Photo Entries",
"UniqueID",
entry => entry.UniqueID,
entry => entry,
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
}
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
}
}