Made item sprites able to be easily changed

This commit is contained in:
2026-04-18 13:29:21 +01:00
parent 97a1659359
commit b412c35d28
5 changed files with 46 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
using Fruitomation.Global;
using Fruitomation.Game.Items;
using Fruitomation.Global;
using UnityEngine;
namespace Fruitomation.Game

View File

@@ -47,37 +47,35 @@ namespace Fruitomation.Game
private void SpawnFruit()
{
List<(ItemType, GameObject)> unlocked = new() { (ItemType.Apple, ApplePrefab) };
List<ItemType> unlocked = new() { ItemType.Apple };
if (UpgradeManager.Is(BasicUpgrade.Grapes))
unlocked.Add((ItemType.Grape, GrapePrefab));
unlocked.Add(ItemType.Grape);
if (UpgradeManager.Is(BasicUpgrade.Bananas))
unlocked.Add((ItemType.Banana, BananaPrefab));
unlocked.Add(ItemType.Banana);
if (UpgradeManager.Is(BasicUpgrade.Kiwi))
unlocked.Add((ItemType.Kiwi, KiwiPrefab));
unlocked.Add(ItemType.Kiwi);
if (UpgradeManager.Is(BasicUpgrade.Mangoes))
unlocked.Add((ItemType.Mango, MangoPrefab));
unlocked.Add(ItemType.Mango);
if (UpgradeManager.Is(BasicUpgrade.Durian))
unlocked.Add((ItemType.Durian, DurianPrefab));
unlocked.Add(ItemType.Durian);
if (UpgradeManager.Is(BasicUpgrade.BuddhasHand))
unlocked.Add((ItemType.BuddhasHand, BuddhasHandPrefab));
unlocked.Add(ItemType.BuddhasHand);
if (UpgradeManager.Is(BasicUpgrade.Pitayas))
unlocked.Add((ItemType.Pitaya, PitayaPrefab));
unlocked.Add(ItemType.Pitaya);
GameObject parent = Instantiate(BaseItemPrefab, FruitSpawnParent);
(ItemType type, GameObject prefab) = unlocked[Random.Range(0, unlocked.Count)];
GameObject go = Instantiate(prefab, parent.transform);
ItemType type = unlocked[Random.Range(0, unlocked.Count)];
ItemBehaviour behaviour = parent.GetComponent<ItemBehaviour>();
Debug.Assert(behaviour is not null, "Could not find ItemBehaviour");
behaviour.InitBehaviour(GameCanvas, type);
}
}

View File

@@ -10,12 +10,18 @@ namespace Fruitomation.Game.Items
protected Canvas AttachedCanvas { get; private set; }
protected bool EnteredCanvas { get; private set; }
private GameObject CurrentChild;
private ItemType InternalItemType;
public ItemType CurrentType
{
get => InternalItemType;
set => InternalItemType = value;
set
{
InternalItemType = value;
OnUpdateItemType();
}
}
protected virtual void OnInitialized() { }
@@ -25,15 +31,22 @@ namespace Fruitomation.Game.Items
RectTransform = transform.GetComponent<RectTransform>();
Body2D = transform.GetComponent<Rigidbody2D>();
InternalItemType = startType;
AttachedCanvas = canvas;
CurrentType = startType;
EnteredCanvas = false;
OnInitialized();
}
private void UpdateItem()
private void OnUpdateItemType()
{
if (CurrentChild is not null)
{
Destroy(CurrentChild);
}
ItemInfo info = ItemInfoRegistry.Get(CurrentType);
CurrentChild = Instantiate(info.Prefab, transform);
}
protected void Update()

View File

@@ -18,6 +18,8 @@ namespace Fruitomation.Game.Items
[Serializable] public class ItemInfo
{
public ItemType Type;
public GameObject Prefab;
public float MinMoney;
public float MaxMoney;
}