2022-12-17 14:39:54 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Trigger
|
|
|
|
{
|
2022-12-17 14:57:18 +03:00
|
|
|
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTriggerArea
|
2022-12-17 14:39:54 +03:00
|
|
|
{
|
2022-12-17 15:28:34 +03:00
|
|
|
[SerializeField] private LayerMask colliderMask = ~0;
|
|
|
|
|
2022-12-17 14:39:54 +03:00
|
|
|
protected Collider2D[] nonAllocResults = new Collider2D[32];
|
|
|
|
protected int previousTriggerCount = 0;
|
|
|
|
|
2022-12-17 15:28:34 +03:00
|
|
|
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
2022-12-17 14:39:54 +03:00
|
|
|
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
|
2022-12-17 14:57:18 +03:00
|
|
|
public List<Collider2D> ColliderList { get; protected set; } = new List<Collider2D>(32);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
|
|
|
protected abstract int TriggerCount { get; }
|
|
|
|
|
|
|
|
public virtual bool Check()
|
|
|
|
{
|
|
|
|
int triggerCount = TriggerCount;
|
|
|
|
|
2022-12-17 22:44:44 +03:00
|
|
|
if (!IsArrayAndListIdentical(triggerCount))
|
2022-12-17 14:39:54 +03:00
|
|
|
{
|
2022-12-17 14:57:18 +03:00
|
|
|
ColliderList.Clear();
|
2022-12-17 14:39:54 +03:00
|
|
|
for (int i = 0; i < triggerCount; i++)
|
2022-12-17 14:57:18 +03:00
|
|
|
ColliderList.Add(nonAllocResults[i]);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
2022-12-17 14:57:18 +03:00
|
|
|
OnTriggeredCollidersUpdated?.Invoke(ColliderList);
|
2022-12-17 14:39:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
previousTriggerCount = triggerCount;
|
|
|
|
return triggerCount > 0;
|
|
|
|
}
|
2022-12-17 22:44:44 +03:00
|
|
|
|
|
|
|
private bool IsArrayAndListIdentical(int triggerCount)
|
|
|
|
{
|
|
|
|
if (previousTriggerCount != triggerCount)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < triggerCount; i++)
|
|
|
|
if (!ColliderList.Contains(nonAllocResults[i]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-17 14:39:54 +03:00
|
|
|
}
|
|
|
|
}
|