Added item registry
This commit is contained in:
62
Assets/Scripts/Game/Items/ItemInfoRegistry.cs
Normal file
62
Assets/Scripts/Game/Items/ItemInfoRegistry.cs
Normal 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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user