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

@@ -14,41 +14,49 @@ MonoBehaviour:
m_EditorClassIdentifier: Fruitomation::Fruitomation.Game.Items.SerializedItemInfoRegistry m_EditorClassIdentifier: Fruitomation::Fruitomation.Game.Items.SerializedItemInfoRegistry
Registry: Registry:
- Type: 0 - Type: 0
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: 23e3bfe33d8c6004c85fd638db567ab6, Prefab: {fileID: 4062657912758122058, guid: 23e3bfe33d8c6004c85fd638db567ab6,
type: 3} type: 3}
MinMoney: 0.5 MinMoney: 0.5
MaxMoney: 1.5 MaxMoney: 1.5
- Type: 1 - Type: 1
CustomBehaviour: 1
Prefab: {fileID: 8799415981405337049, guid: c21e0b5539573904cb10d979de4a3f72, Prefab: {fileID: 8799415981405337049, guid: c21e0b5539573904cb10d979de4a3f72,
type: 3} type: 3}
MinMoney: 3 MinMoney: 3
MaxMoney: 7 MaxMoney: 7
- Type: 2 - Type: 2
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: 5e1f7f5d4294c1d4fa3e6f88dec0dfa6, Prefab: {fileID: 4062657912758122058, guid: 5e1f7f5d4294c1d4fa3e6f88dec0dfa6,
type: 3} type: 3}
MinMoney: 12 MinMoney: 12
MaxMoney: 18 MaxMoney: 18
- Type: 3 - Type: 3
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: b224b6df33529e64d83245d78e0feb9f, Prefab: {fileID: 4062657912758122058, guid: b224b6df33529e64d83245d78e0feb9f,
type: 3} type: 3}
MinMoney: 40 MinMoney: 40
MaxMoney: 60 MaxMoney: 60
- Type: 4 - Type: 4
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: 7af455f1c1666e5409156598fe669f74, Prefab: {fileID: 4062657912758122058, guid: 7af455f1c1666e5409156598fe669f74,
type: 3} type: 3}
MinMoney: 120 MinMoney: 120
MaxMoney: 160 MaxMoney: 160
- Type: 6 - Type: 6
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: 13f2740b5ae396e40940587ab2effd65, Prefab: {fileID: 4062657912758122058, guid: 13f2740b5ae396e40940587ab2effd65,
type: 3} type: 3}
MinMoney: 250 MinMoney: 250
MaxMoney: 350 MaxMoney: 350
- Type: 7 - Type: 7
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: ebddb1b81a288cc4c993a25bc9efe894, Prefab: {fileID: 4062657912758122058, guid: ebddb1b81a288cc4c993a25bc9efe894,
type: 3} type: 3}
MinMoney: 800 MinMoney: 800
MaxMoney: 900 MaxMoney: 900
- Type: 5 - Type: 5
CustomBehaviour: 1
Prefab: {fileID: 4062657912758122058, guid: 2cf42dbc57bb6374b9e6ec700aad5854, Prefab: {fileID: 4062657912758122058, guid: 2cf42dbc57bb6374b9e6ec700aad5854,
type: 3} type: 3}
MinMoney: 2000 MinMoney: 2000

View File

@@ -1,12 +1,14 @@
using JetBrains.Annotations;
using UnityEngine.Scripting;
using UnityEngine; using UnityEngine;
namespace Fruitomation.Game.Items 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 namespace Fruitomation.Game.Items
{ {
public class ItemBehaviour : MonoBehaviour public sealed class ItemBehaviour : MonoBehaviour
{ {
protected RectTransform RectTransform { get; private set; } public RectTransform RectTransform { get; private set; }
protected Rigidbody2D Body2D { get; private set; } public Rigidbody2D Body2D { get; private set; }
protected Canvas AttachedCanvas { get; private set; }
protected bool EnteredCanvas { get; private set; }
private Canvas AttachedCanvas { get; set; }
private bool EnteredCanvas { get; set; }
private CustomItemBehaviour CustomBehaviour;
private GameObject CurrentChild; private GameObject CurrentChild;
private ItemType InternalItemType; private ItemType InternalItemType;
@@ -24,8 +26,6 @@ namespace Fruitomation.Game.Items
} }
} }
protected virtual void OnInitialized() { }
public void InitBehaviour(Canvas canvas, ItemType startType) public void InitBehaviour(Canvas canvas, ItemType startType)
{ {
RectTransform = transform.GetComponent<RectTransform>(); RectTransform = transform.GetComponent<RectTransform>();
@@ -35,21 +35,29 @@ namespace Fruitomation.Game.Items
CurrentType = startType; CurrentType = startType;
EnteredCanvas = false; EnteredCanvas = false;
OnInitialized(); CustomBehaviour?.OnCreation();
} }
private void OnUpdateItemType() private void OnUpdateItemType()
{ {
if (CurrentChild is not null) if (CurrentChild is not null)
{ {
CustomBehaviour = null;
Destroy(CurrentChild); Destroy(CurrentChild);
} }
ItemInfo info = ItemInfoRegistry.Get(CurrentType); ItemInfo info = ItemInfoRegistry.Get(CurrentType);
CurrentChild = Instantiate(info.Prefab, transform); 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)) if (!GameStateController.Is(GameState.Simulation))
{ {

View File

@@ -3,6 +3,13 @@ using System;
namespace Fruitomation.Game.Items namespace Fruitomation.Game.Items
{ {
[Serializable] public enum CustomBehaviourType
{
None,
FruitBehaviour,
}
[Serializable] public enum ItemType [Serializable] public enum ItemType
{ {
Apple, Apple,
@@ -58,9 +65,23 @@ namespace Fruitomation.Game.Items
{ {
public ItemType Type; public ItemType Type;
[SerializeField] private CustomBehaviourType CustomBehaviour;
public GameObject Prefab; public GameObject Prefab;
public float MinMoney; public float MinMoney;
public float MaxMoney; 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 [CreateAssetMenu] public class SerializedItemInfoRegistry : ScriptableObject