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

@@ -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;
}
}
}

View File

@@ -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);
}
}

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];
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dcb8b502e5b94a72b37a6e77e4770d05
timeCreated: 1776511113

View 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;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 92f10d0327354ede9d9b3cf77e1c2d30
timeCreated: 1776512220

View File

@@ -70,7 +70,6 @@ namespace Fruitomation.Game
UnlockedUpgrades upgrades = new();
foreach (BasicUpgrade upgrade in BasicUpgrades)
{
Debug.Log($"Restored [{upgrade}] upgrade");
upgrades.Unlocks.Add(upgrade);
}

View File

@@ -7,15 +7,15 @@ namespace Fruitomation.Global
{
private static MoneyController Instance;
[SerializeField, InspectorReadOnly("Game State")] private ulong InternalCurrentMoney;
[SerializeField, InspectorReadOnly("Game State")] private double InternalCurrentMoney;
public static ulong Current
public static double Current
{
get => Instance.InternalCurrentMoney;
set => Instance.InternalCurrentMoney = value;
}
public static void Add(ulong amount) => Instance.InternalCurrentMoney += amount;
public static void Add(double amount) => Instance.InternalCurrentMoney += amount;
private void Awake()
{

View File

@@ -46,7 +46,7 @@ namespace Fruitomation.UI
private void Update()
{
MoneyText.text = $"Current Money: ${MoneyController.Current}";
MoneyText.text = $"Current Money: ${MoneyController.Current:F1}";
}
}
}