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 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);

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 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
}
[Header("Read only")]
[InspectorReadOnly, SerializeField] private Canvas AttachedCanvas;
[InspectorReadOnly, SerializeField] private FruitSpawner Spawner;
[InspectorReadOnly, SerializeField] private bool EnteredCanvas;
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; }
public void InitFruitBehaviour(Canvas canvas, FruitSpawner spawner)
private ItemType InternalItemType;
public ItemType CurrentType
{
AttachedCanvas = canvas;
EnteredCanvas = false;
Spawner = spawner;
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
get => InternalItemType;
set
{
InternalItemType = value;
}
}
private void Update()
protected virtual void OnInitialized() { }
public void InitBehaviour(Canvas canvas)
{
RectTransform = transform.GetComponent<RectTransform>();
Body2D = transform.GetComponent<Rigidbody2D>();
AttachedCanvas = canvas;
EnteredCanvas = false;
OnInitialized();
}
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);
}
}
}

View File

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

View File

@@ -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;