Files
Fruitomation/Assets/Scripts/Game/Items/ItemInfoRegistry.cs
2026-05-01 12:35:40 +01:00

80 lines
2.2 KiB
C#

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 (type == ItemType.None)
{
if (contained)
{
Debug.LogError("ItemType.None should not be included in ItemInfoRegistry");
}
}
else if (!contained)
{
Debug.LogWarning($"Type [{type}] is not contained in the registry");
}
}
#endif // UNITY_EDITOR
}
public static ItemInfo Get(ItemType type)
{
if (type == ItemType.None)
{
Debug.LogError("ItemType.None is not a valid ItemType value for ItemInfoRegistry.Get");
return null;
}
return Instance.Dictionary[type];
}
}
}