Added item registry
This commit is contained in:
@@ -4,9 +4,12 @@ namespace Fruitomation.Game.Items
|
||||
{
|
||||
public class FruitBehaviour : ItemBehaviour
|
||||
{
|
||||
[SerializeField] private ItemType OverridenItemType;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
|
||||
CurrentType = OverridenItemType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,8 @@
|
||||
using System;
|
||||
using Fruitomation.Global;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Fruitomation.Game.Items
|
||||
{
|
||||
public enum ItemType
|
||||
{
|
||||
Apple,
|
||||
Grape,
|
||||
Banana,
|
||||
Kiwi,
|
||||
Mango,
|
||||
Pitaya,
|
||||
Durian,
|
||||
BuddhasHand
|
||||
}
|
||||
|
||||
public class ItemBehaviour : MonoBehaviour
|
||||
{
|
||||
protected RectTransform RectTransform { get; private set; }
|
||||
@@ -99,6 +86,13 @@ namespace Fruitomation.Game.Items
|
||||
|
||||
public void TriggerDestruction(bool harvest = true)
|
||||
{
|
||||
if (harvest)
|
||||
{
|
||||
ItemInfo info = ItemInfoRegistry.Get(CurrentType);
|
||||
float money = Random.Range(info.MinMoney, info.MaxMoney);
|
||||
MoneyController.Add(money);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Game/Items/ItemInfoRegistry.cs.meta
Normal file
3
Assets/Scripts/Game/Items/ItemInfoRegistry.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dcb8b502e5b94a72b37a6e77e4770d05
|
||||
timeCreated: 1776511113
|
||||
29
Assets/Scripts/Game/Items/SerializedItemInfoRegistry.cs
Normal file
29
Assets/Scripts/Game/Items/SerializedItemInfoRegistry.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Fruitomation.Game.Items
|
||||
{
|
||||
[Serializable] public enum ItemType
|
||||
{
|
||||
Apple,
|
||||
Grape,
|
||||
Banana,
|
||||
Kiwi,
|
||||
Mango,
|
||||
Pitaya,
|
||||
Durian,
|
||||
BuddhasHand
|
||||
}
|
||||
|
||||
[Serializable] public class ItemInfo
|
||||
{
|
||||
public ItemType Type;
|
||||
public float MinMoney;
|
||||
public float MaxMoney;
|
||||
}
|
||||
|
||||
[CreateAssetMenu] public class SerializedItemInfoRegistry : ScriptableObject
|
||||
{
|
||||
[SerializeField] public ItemInfo[] Registry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92f10d0327354ede9d9b3cf77e1c2d30
|
||||
timeCreated: 1776512220
|
||||
@@ -70,7 +70,6 @@ namespace Fruitomation.Game
|
||||
UnlockedUpgrades upgrades = new();
|
||||
foreach (BasicUpgrade upgrade in BasicUpgrades)
|
||||
{
|
||||
Debug.Log($"Restored [{upgrade}] upgrade");
|
||||
upgrades.Unlocks.Add(upgrade);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user