Added a base class for item behaviour

This commit is contained in:
2026-04-18 12:11:53 +01:00
parent 352184df25
commit 256ab65060
7 changed files with 69 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
using PashaBibko.Pacore.Attributes; using PashaBibko.Pacore.Attributes;
using System.Collections.Generic; using System.Collections.Generic;
using Fruitomation.Game.Items;
using Fruitomation.Global; using Fruitomation.Global;
using UnityEngine; using UnityEngine;
@@ -77,10 +78,7 @@ namespace Fruitomation.Game
Debug.Assert(behaviour is not null, "Could not find FruitBehaviour"); Debug.Assert(behaviour is not null, "Could not find FruitBehaviour");
ActiveFruits.Add(behaviour); ActiveFruits.Add(behaviour);
behaviour.InitFruitBehaviour behaviour.InitBehaviour(GameCanvas);
(
GameCanvas, this
);
} }
public void RemoveFruit(FruitBehaviour fruit) => ActiveFruits.Remove(fruit); public void RemoveFruit(FruitBehaviour fruit) => ActiveFruits.Remove(fruit);

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eaaa88442884433ca217e9dab3b15028
timeCreated: 1776447095

View File

@@ -0,0 +1,12 @@
using UnityEngine;
namespace Fruitomation.Game.Items
{
public class FruitBehaviour : ItemBehaviour
{
protected override void OnInitialized()
{
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
}
}
}

View File

@@ -1,34 +1,61 @@
using PashaBibko.Pacore.Attributes; using System;
using Fruitomation.Global; using Fruitomation.Global;
using UnityEngine; using UnityEngine;
namespace Fruitomation.Game namespace Fruitomation.Game.Items
{ {
public class FruitBehaviour : MonoBehaviour public enum ItemType
{ {
[Header("References")] Apple,
[SerializeField] private RectTransform RectTransform; Grape,
[SerializeField] private Rigidbody2D Body2D; 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")] private ItemType InternalItemType;
[InspectorReadOnly, SerializeField] private Canvas AttachedCanvas;
[InspectorReadOnly, SerializeField] private FruitSpawner Spawner;
[InspectorReadOnly, SerializeField] private bool EnteredCanvas;
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<RectTransform>();
Body2D = transform.GetComponent<Rigidbody2D>();
AttachedCanvas = canvas; AttachedCanvas = canvas;
EnteredCanvas = false; EnteredCanvas = false;
Spawner = spawner;
OnInitialized();
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
} }
private void Update() private void UpdateItem()
{
}
protected void Update()
{ {
if (!GameStateController.Is(GameState.Simulation)) 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) private static bool IsWithinCanvas(RectTransform element, RectTransform canvas)
{ {
Vector3[] elementCorners = new Vector3[4]; Vector3[] elementCorners = new Vector3[4];
@@ -77,5 +96,10 @@ namespace Fruitomation.Game
return false; return false;
} }
public void TriggerDestruction(bool harvest = true)
{
Destroy(gameObject);
}
} }
} }

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 76050cf8c863ec84ca2c83053f33726c

View File

@@ -1,5 +1,6 @@
using PashaBibko.Pacore.Attributes; using PashaBibko.Pacore.Attributes;
using System.Collections.Generic; using System.Collections.Generic;
using Fruitomation.Game.Items;
using Fruitomation.Global; using Fruitomation.Global;
using Fruitomation.Game; using Fruitomation.Game;
using UnityEngine.UI; using UnityEngine.UI;