133 lines
3.9 KiB
C#
133 lines
3.9 KiB
C#
using System.Collections.Generic;
|
|
using BriarQueen.Framework.Managers.Player.Data;
|
|
using UnityEngine;
|
|
|
|
namespace BriarQueen.Framework.Registries
|
|
{
|
|
[CreateAssetMenu(menuName = "Briar Queen/Registries/Item Registry", fileName = "New Item Registry")]
|
|
public class ItemRegistry : ScriptableObject
|
|
{
|
|
[Header("Items")]
|
|
[SerializeField]
|
|
private List<ItemDataSo> _puzzleSlots = new();
|
|
|
|
[SerializeField]
|
|
private List<ItemDataSo> _pickupItems = new();
|
|
|
|
[SerializeField]
|
|
private List<ItemDataSo> _environmentInteractables = new();
|
|
|
|
private Dictionary<string, ItemDataSo> _entryLookup;
|
|
|
|
public IReadOnlyList<ItemDataSo> PuzzleSlots => _puzzleSlots;
|
|
public IReadOnlyList<ItemDataSo> PickupItems => _pickupItems;
|
|
public IReadOnlyList<ItemDataSo> EnvironmentInteractables => _environmentInteractables;
|
|
|
|
public void EnsureInitialized()
|
|
{
|
|
if (_entryLookup != null)
|
|
return;
|
|
|
|
RebuildLookup();
|
|
}
|
|
|
|
public void RebuildLookup()
|
|
{
|
|
_entryLookup = new Dictionary<string, ItemDataSo>();
|
|
|
|
RegistryLookupBuilder.AddEntries(
|
|
_entryLookup,
|
|
_puzzleSlots,
|
|
this,
|
|
nameof(ItemRegistry),
|
|
"Puzzle Slots",
|
|
"UniqueID",
|
|
entry => entry.UniqueID,
|
|
entry => entry,
|
|
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
|
|
|
|
RegistryLookupBuilder.AddEntries(
|
|
_entryLookup,
|
|
_pickupItems,
|
|
this,
|
|
nameof(ItemRegistry),
|
|
"Pickup Items",
|
|
"UniqueID",
|
|
entry => entry.UniqueID,
|
|
entry => entry,
|
|
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
|
|
|
|
RegistryLookupBuilder.AddEntries(
|
|
_entryLookup,
|
|
_environmentInteractables,
|
|
this,
|
|
nameof(ItemRegistry),
|
|
"Environment Interactables",
|
|
"UniqueID",
|
|
entry => entry.UniqueID,
|
|
entry => entry,
|
|
entry => RegistryLookupBuilder.HasNonEmptyKey(entry.UniqueID));
|
|
}
|
|
|
|
public bool TryGetEntry(string itemID, out ItemDataSo entry)
|
|
{
|
|
EnsureInitialized();
|
|
|
|
if (string.IsNullOrWhiteSpace(itemID))
|
|
{
|
|
entry = null;
|
|
return false;
|
|
}
|
|
|
|
return _entryLookup.TryGetValue(itemID, out entry);
|
|
}
|
|
|
|
public ItemDataSo FindItemTemplateByID(string itemID)
|
|
{
|
|
EnsureInitialized();
|
|
return string.IsNullOrWhiteSpace(itemID) ? null : _entryLookup.GetValueOrDefault(itemID);
|
|
}
|
|
|
|
public IEnumerable<ItemDataSo> GetAllItems()
|
|
{
|
|
EnsureInitialized();
|
|
return _entryLookup.Values;
|
|
}
|
|
|
|
public IEnumerable<string> GetAllKeys()
|
|
{
|
|
EnsureInitialized();
|
|
return _entryLookup.Keys;
|
|
}
|
|
|
|
public IEnumerable<ItemDataSo> GetPuzzleSlots()
|
|
{
|
|
return _puzzleSlots;
|
|
}
|
|
|
|
public IEnumerable<ItemDataSo> GetPickupItems()
|
|
{
|
|
return _pickupItems;
|
|
}
|
|
|
|
public IEnumerable<ItemDataSo> GetEnvironmentInteractables()
|
|
{
|
|
return _environmentInteractables;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public void SetDiscoveredEntries(
|
|
List<ItemDataSo> puzzleSlots,
|
|
List<ItemDataSo> pickupItems,
|
|
List<ItemDataSo> environmentInteractables)
|
|
{
|
|
_puzzleSlots = puzzleSlots ?? new List<ItemDataSo>();
|
|
_pickupItems = pickupItems ?? new List<ItemDataSo>();
|
|
_environmentInteractables = environmentInteractables ?? new List<ItemDataSo>();
|
|
|
|
RebuildLookup();
|
|
}
|
|
#endif
|
|
}
|
|
}
|