Added custom item behaviour

This commit is contained in:
2026-04-18 14:03:28 +01:00
parent 529a83dafc
commit 88cf5b7ba9
6 changed files with 64 additions and 13 deletions

View File

@@ -1,12 +1,14 @@
using JetBrains.Annotations;
using UnityEngine.Scripting;
using UnityEngine;
namespace Fruitomation.Game.Items
{
public class FruitBehaviour : ItemBehaviour
[UsedImplicitly, Preserve] public class FruitBehaviour : CustomItemBehaviour
{
protected override void OnInitialized()
public override void OnCreation()
{
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
AttachedItemBehaviour.Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
}
}
}

View File

@@ -0,0 +1,9 @@
namespace Fruitomation.Game.Items
{
public abstract class CustomItemBehaviour
{
public ItemBehaviour AttachedItemBehaviour;
public abstract void OnCreation();
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 77bf10644007434586ed2f8b9e640469
timeCreated: 1776516125

View File

@@ -3,13 +3,15 @@ using UnityEngine;
namespace Fruitomation.Game.Items
{
public class ItemBehaviour : MonoBehaviour
public sealed class ItemBehaviour : MonoBehaviour
{
protected RectTransform RectTransform { get; private set; }
protected Rigidbody2D Body2D { get; private set; }
protected Canvas AttachedCanvas { get; private set; }
protected bool EnteredCanvas { get; private set; }
public RectTransform RectTransform { get; private set; }
public Rigidbody2D Body2D { get; private set; }
private Canvas AttachedCanvas { get; set; }
private bool EnteredCanvas { get; set; }
private CustomItemBehaviour CustomBehaviour;
private GameObject CurrentChild;
private ItemType InternalItemType;
@@ -23,8 +25,6 @@ namespace Fruitomation.Game.Items
OnUpdateItemType();
}
}
protected virtual void OnInitialized() { }
public void InitBehaviour(Canvas canvas, ItemType startType)
{
@@ -35,21 +35,29 @@ namespace Fruitomation.Game.Items
CurrentType = startType;
EnteredCanvas = false;
OnInitialized();
CustomBehaviour?.OnCreation();
}
private void OnUpdateItemType()
{
if (CurrentChild is not null)
{
CustomBehaviour = null;
Destroy(CurrentChild);
}
ItemInfo info = ItemInfoRegistry.Get(CurrentType);
CurrentChild = Instantiate(info.Prefab, transform);
CustomBehaviour = info.GetCustomBehaviour();
if (CustomBehaviour is not null)
{
CustomBehaviour.AttachedItemBehaviour = this;
CustomBehaviour.OnCreation();
}
}
protected void Update()
private void Update()
{
if (!GameStateController.Is(GameState.Simulation))
{

View File

@@ -3,6 +3,13 @@ using System;
namespace Fruitomation.Game.Items
{
[Serializable] public enum CustomBehaviourType
{
None,
FruitBehaviour,
}
[Serializable] public enum ItemType
{
Apple,
@@ -57,10 +64,24 @@ namespace Fruitomation.Game.Items
[Serializable] public class ItemInfo
{
public ItemType Type;
[SerializeField] private CustomBehaviourType CustomBehaviour;
public GameObject Prefab;
public float MinMoney;
public float MaxMoney;
public CustomItemBehaviour GetCustomBehaviour()
{
return CustomBehaviour switch
{
CustomBehaviourType.None => null,
CustomBehaviourType.FruitBehaviour => new FruitBehaviour(),
var _ => throw new ArgumentOutOfRangeException()
};
}
}
[CreateAssetMenu] public class SerializedItemInfoRegistry : ScriptableObject