Added a base class for item behaviour
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
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 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,
|
||||||
[Header("Read only")]
|
Mango,
|
||||||
[InspectorReadOnly, SerializeField] private Canvas AttachedCanvas;
|
Pitaya,
|
||||||
[InspectorReadOnly, SerializeField] private FruitSpawner Spawner;
|
Durian,
|
||||||
[InspectorReadOnly, SerializeField] private bool EnteredCanvas;
|
BuddhasHand
|
||||||
|
|
||||||
public void InitFruitBehaviour(Canvas canvas, FruitSpawner spawner)
|
|
||||||
{
|
|
||||||
AttachedCanvas = canvas;
|
|
||||||
EnteredCanvas = false;
|
|
||||||
Spawner = spawner;
|
|
||||||
|
|
||||||
Body2D.linearVelocity = Random.insideUnitCircle * 2.5f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
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; }
|
||||||
|
|
||||||
|
private ItemType InternalItemType;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
OnInitialized();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user