From 256ab650608e9b2ab6601c34a58218db6a0c6505 Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 18 Apr 2026 12:11:53 +0100 Subject: [PATCH] Added a base class for item behaviour --- Assets/Scripts/Game/FruitSpawner.cs | 6 +- Assets/Scripts/Game/Items.meta | 3 + Assets/Scripts/Game/Items/FruitBehaviour.cs | 12 +++ .../Game/{ => Items}/FruitBehaviour.cs.meta | 0 .../ItemBehaviour.cs} | 74 ++++++++++++------- .../Scripts/Game/Items/ItemBehaviour.cs.meta | 2 + Assets/Scripts/UI/GameCursor.cs | 1 + 7 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 Assets/Scripts/Game/Items.meta create mode 100644 Assets/Scripts/Game/Items/FruitBehaviour.cs rename Assets/Scripts/Game/{ => Items}/FruitBehaviour.cs.meta (100%) rename Assets/Scripts/Game/{FruitBehaviour.cs => Items/ItemBehaviour.cs} (54%) create mode 100644 Assets/Scripts/Game/Items/ItemBehaviour.cs.meta diff --git a/Assets/Scripts/Game/FruitSpawner.cs b/Assets/Scripts/Game/FruitSpawner.cs index 6e95e6e..e1b4c7e 100644 --- a/Assets/Scripts/Game/FruitSpawner.cs +++ b/Assets/Scripts/Game/FruitSpawner.cs @@ -1,5 +1,6 @@ using PashaBibko.Pacore.Attributes; using System.Collections.Generic; +using Fruitomation.Game.Items; using Fruitomation.Global; using UnityEngine; @@ -77,10 +78,7 @@ namespace Fruitomation.Game Debug.Assert(behaviour is not null, "Could not find FruitBehaviour"); ActiveFruits.Add(behaviour); - behaviour.InitFruitBehaviour - ( - GameCanvas, this - ); + behaviour.InitBehaviour(GameCanvas); } public void RemoveFruit(FruitBehaviour fruit) => ActiveFruits.Remove(fruit); diff --git a/Assets/Scripts/Game/Items.meta b/Assets/Scripts/Game/Items.meta new file mode 100644 index 0000000..f026b19 --- /dev/null +++ b/Assets/Scripts/Game/Items.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: eaaa88442884433ca217e9dab3b15028 +timeCreated: 1776447095 \ No newline at end of file diff --git a/Assets/Scripts/Game/Items/FruitBehaviour.cs b/Assets/Scripts/Game/Items/FruitBehaviour.cs new file mode 100644 index 0000000..59b591e --- /dev/null +++ b/Assets/Scripts/Game/Items/FruitBehaviour.cs @@ -0,0 +1,12 @@ +using UnityEngine; + +namespace Fruitomation.Game.Items +{ + public class FruitBehaviour : ItemBehaviour + { + protected override void OnInitialized() + { + Body2D.linearVelocity = Random.insideUnitCircle * 2.5f; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Game/FruitBehaviour.cs.meta b/Assets/Scripts/Game/Items/FruitBehaviour.cs.meta similarity index 100% rename from Assets/Scripts/Game/FruitBehaviour.cs.meta rename to Assets/Scripts/Game/Items/FruitBehaviour.cs.meta diff --git a/Assets/Scripts/Game/FruitBehaviour.cs b/Assets/Scripts/Game/Items/ItemBehaviour.cs similarity index 54% rename from Assets/Scripts/Game/FruitBehaviour.cs rename to Assets/Scripts/Game/Items/ItemBehaviour.cs index eb16046..425628c 100644 --- a/Assets/Scripts/Game/FruitBehaviour.cs +++ b/Assets/Scripts/Game/Items/ItemBehaviour.cs @@ -1,34 +1,61 @@ -using PashaBibko.Pacore.Attributes; +using System; using Fruitomation.Global; using UnityEngine; -namespace Fruitomation.Game +namespace Fruitomation.Game.Items { - public class FruitBehaviour : MonoBehaviour + public enum ItemType { - [Header("References")] - [SerializeField] private RectTransform RectTransform; - [SerializeField] private Rigidbody2D Body2D; + Apple, + Grape, + Banana, + Kiwi, + Mango, + Pitaya, + Durian, + BuddhasHand + } + + public 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; } - [Header("Read only")] - [InspectorReadOnly, SerializeField] private Canvas AttachedCanvas; - [InspectorReadOnly, SerializeField] private FruitSpawner Spawner; - [InspectorReadOnly, SerializeField] private bool EnteredCanvas; + private ItemType InternalItemType; - public void InitFruitBehaviour(Canvas canvas, FruitSpawner spawner) + public ItemType CurrentType { + get => InternalItemType; + set + { + InternalItemType = value; + } + } + + protected virtual void OnInitialized() { } + + public void InitBehaviour(Canvas canvas) + { + RectTransform = transform.GetComponent(); + Body2D = transform.GetComponent(); + AttachedCanvas = canvas; EnteredCanvas = false; - Spawner = spawner; - - Body2D.linearVelocity = Random.insideUnitCircle * 2.5f; + + OnInitialized(); } - private void Update() + private void UpdateItem() + { + } + + protected void Update() { if (!GameStateController.Is(GameState.Simulation)) { - TriggerDestruction(); + TriggerDestruction(false); } } @@ -43,14 +70,6 @@ namespace Fruitomation.Game } } - public void TriggerDestruction() - { - MoneyController.Add((ulong)Random.Range(1, 5)); - - Spawner.RemoveFruit(this); - Destroy(gameObject); - } - private static bool IsWithinCanvas(RectTransform element, RectTransform canvas) { Vector3[] elementCorners = new Vector3[4]; @@ -77,5 +96,10 @@ namespace Fruitomation.Game return false; } + + public void TriggerDestruction(bool harvest = true) + { + Destroy(gameObject); + } } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Game/Items/ItemBehaviour.cs.meta b/Assets/Scripts/Game/Items/ItemBehaviour.cs.meta new file mode 100644 index 0000000..19e29a7 --- /dev/null +++ b/Assets/Scripts/Game/Items/ItemBehaviour.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 76050cf8c863ec84ca2c83053f33726c \ No newline at end of file diff --git a/Assets/Scripts/UI/GameCursor.cs b/Assets/Scripts/UI/GameCursor.cs index 1c6e53c..cb02c4c 100644 --- a/Assets/Scripts/UI/GameCursor.cs +++ b/Assets/Scripts/UI/GameCursor.cs @@ -1,5 +1,6 @@ using PashaBibko.Pacore.Attributes; using System.Collections.Generic; +using Fruitomation.Game.Items; using Fruitomation.Global; using Fruitomation.Game; using UnityEngine.UI;