Files

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>();
AddEntries(_puzzleSlots, "Puzzle Slots");
AddEntries(_pickupItems, "Pickup Items");
AddEntries(_environmentInteractables, "Environment Interactables");
}
private void AddEntries(List<ItemDataSo> entries, string category)
{
if (entries == null)
return;
foreach (var entry in entries)
{
if (entry == null)
continue;
if (string.IsNullOrWhiteSpace(entry.UniqueID))
{
Debug.LogWarning(
$"[ItemRegistry] Skipping {category} entry '{entry.name}' because UniqueID is empty.",
this);
continue;
}
if (_entryLookup.ContainsKey(entry.UniqueID))
{
Debug.LogError(
$"[ItemRegistry] Duplicate UniqueID detected: '{entry.UniqueID}' from entry '{entry.name}'.",
this);
continue;
}
_entryLookup.Add(entry.UniqueID, entry);
}
}
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
}
}