Added a base class for item behaviour
This commit is contained in:
@@ -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);
|
||||
|
||||
3
Assets/Scripts/Game/Items.meta
Normal file
3
Assets/Scripts/Game/Items.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eaaa88442884433ca217e9dab3b15028
|
||||
timeCreated: 1776447095
|
||||
12
Assets/Scripts/Game/Items/FruitBehaviour.cs
Normal file
12
Assets/Scripts/Game/Items/FruitBehaviour.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<RectTransform>();
|
||||
Body2D = transform.GetComponent<Rigidbody2D>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Game/Items/ItemBehaviour.cs.meta
Normal file
2
Assets/Scripts/Game/Items/ItemBehaviour.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76050cf8c863ec84ca2c83053f33726c
|
||||
Reference in New Issue
Block a user