141 lines
4.0 KiB
C#
141 lines
4.0 KiB
C#
using PashaBibko.Pacore.Attributes;
|
|
using Fruitomation.Global;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
using Vector3 = UnityEngine.Vector3;
|
|
|
|
namespace Fruitomation.Game.Items
|
|
{
|
|
public sealed class ItemBehaviour : MonoBehaviour
|
|
{
|
|
public RectTransform RectTransform { get; private set; }
|
|
public Rigidbody2D Body2D { get; private set; }
|
|
|
|
private Canvas AttachedCanvas { get; set; }
|
|
private bool EnteredCanvas { get; set; }
|
|
|
|
private CustomItemBehaviour CustomBehaviour;
|
|
private GameObject CurrentChild;
|
|
|
|
[SerializeField, InspectorReadOnly("Item Type")]
|
|
private ItemType InternalItemType = ItemType.None;
|
|
|
|
public ItemType CurrentType
|
|
{
|
|
get => InternalItemType;
|
|
set
|
|
{
|
|
if (InternalItemType != value) // Stops unneeded changes
|
|
{
|
|
InternalItemType = value;
|
|
OnUpdateItemType();
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerator SendToGhostRealmInternal()
|
|
{
|
|
CurrentChild.SetActive(false);
|
|
Body2D.Sleep();
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
CurrentChild.SetActive(true);
|
|
Body2D.WakeUp();
|
|
}
|
|
|
|
public void SendToTheGhostRealm() => StartCoroutine(SendToGhostRealmInternal());
|
|
|
|
public void InitBehaviour(Canvas canvas, ItemType startType)
|
|
{
|
|
RectTransform = transform.GetComponent<RectTransform>();
|
|
Body2D = transform.GetComponent<Rigidbody2D>();
|
|
|
|
AttachedCanvas = canvas;
|
|
CurrentType = startType;
|
|
EnteredCanvas = false;
|
|
|
|
CustomBehaviour?.OnCreation();
|
|
}
|
|
|
|
private void OnUpdateItemType()
|
|
{
|
|
if (CurrentChild is not null)
|
|
{
|
|
CustomBehaviour = null;
|
|
Destroy(CurrentChild);
|
|
}
|
|
|
|
ItemInfo info = ItemInfoRegistry.Get(CurrentType);
|
|
CurrentChild = Instantiate(info.Prefab, transform);
|
|
|
|
CustomBehaviour = info.GetCustomBehaviour();
|
|
if (CustomBehaviour is not null)
|
|
{
|
|
CustomBehaviour.AttachedItemBehaviour = this;
|
|
CustomBehaviour.OnCreation();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!GameStateController.Is(GameState.Simulation))
|
|
{
|
|
TriggerDestruction(false);
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (IsNotWithinCanvas(RectTransform, AttachedCanvas.GetComponent<RectTransform>()))
|
|
{
|
|
TriggerDestruction();
|
|
}
|
|
}
|
|
|
|
private static bool IsNotWithinCanvas(RectTransform element, RectTransform canvas)
|
|
{
|
|
Vector3[] elementCorners = new Vector3[4];
|
|
Vector3[] canvasCorners = new Vector3[4];
|
|
|
|
element.GetWorldCorners(elementCorners);
|
|
canvas.GetWorldCorners(canvasCorners);
|
|
|
|
float aMin = float.MaxValue;
|
|
float aMax = float.MinValue;
|
|
|
|
foreach (Vector3 corner in canvasCorners)
|
|
{
|
|
aMin = Mathf.Min(aMin, corner.x);
|
|
aMax = Mathf.Max(aMax, corner.x);
|
|
}
|
|
|
|
float bMin = float.MaxValue;
|
|
float bMax = float.MinValue;
|
|
|
|
foreach (Vector3 corner in elementCorners)
|
|
{
|
|
bMin = Mathf.Min(bMin, corner.x);
|
|
bMax = Mathf.Max(bMax, corner.x);
|
|
}
|
|
|
|
return
|
|
!(bMin > (aMin - 1f)) ||
|
|
!(bMax < (aMax + 1f));
|
|
}
|
|
|
|
public void TriggerDestruction(bool harvest = true)
|
|
{
|
|
if (harvest)
|
|
{
|
|
ItemInfo info = ItemInfoRegistry.Get(CurrentType);
|
|
float money = Random.Range(info.MinMoney, info.MaxMoney);
|
|
MoneyController.Add(money);
|
|
}
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|