Added item registry

This commit is contained in:
2026-04-18 12:52:28 +01:00
parent 1908024332
commit cd7f16f021
29 changed files with 187 additions and 53 deletions

View File

@@ -0,0 +1,62 @@
using PashaBibko.Pacore.Attributes;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Fruitomation.Game.Items
{
public class ItemInfoRegistry : MonoBehaviour
{
[Header("References")]
[SerializeField] private SerializedItemInfoRegistry SerializedRegistry;
private Dictionary<ItemType, ItemInfo> Dictionary;
private static ItemInfoRegistry Instance;
private void Awake()
{
/* Sets as the global instance */
if (Instance is not null)
{
Debug.LogError("Multiple instances of ItemInfoRegistry found");
return;
}
LoadFromRegistry();
Instance = this;
}
private void OnDestroy()
{
if (Instance == this)
{
Instance = null;
}
}
[InspectorCallable("Load Registry")] private void LoadFromRegistry()
{
Dictionary = new Dictionary<ItemType, ItemInfo>();
foreach (ItemInfo info in SerializedRegistry.Registry)
{
Dictionary.Add(info.Type, info);
}
#if UNITY_EDITOR
ItemType[] types = Enum.GetValues(typeof(ItemType)) as ItemType[];
Debug.Assert(types != null, nameof(types) + " != null");
foreach (ItemType type in types)
{
bool contained = Dictionary.ContainsKey(type);
if (!contained)
{
Debug.LogWarning($"Type [{type}] is not contained in the registry");
}
}
#endif // UNITY_EDITOR
}
public static ItemInfo Get(ItemType type) => Instance.Dictionary[type];
}
}