50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Syntriax.Modules.Trigger
|
|
{
|
|
public abstract class ColliderTriggerAreaBase<T> : MonoBehaviour, IColliderTriggerArea where T : Component
|
|
{
|
|
[SerializeField] private LayerMask colliderMask = ~0;
|
|
|
|
protected T[] nonAllocResults = new T[32];
|
|
protected int previousTriggerCount = 0;
|
|
|
|
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
|
public Action<List<Transform>> OnTriggeredTransformsUpdated { get; set; } = null;
|
|
public List<Transform> TriggeredTransforms { get; protected set; } = new List<Transform>(32);
|
|
|
|
protected abstract int TriggerCount { get; }
|
|
|
|
public virtual bool Check()
|
|
{
|
|
int triggerCount = TriggerCount;
|
|
|
|
if (!IsArrayAndListIdentical(triggerCount))
|
|
{
|
|
TriggeredTransforms.Clear();
|
|
for (int i = 0; i < triggerCount; i++)
|
|
TriggeredTransforms.Add(nonAllocResults[i].transform);
|
|
|
|
OnTriggeredTransformsUpdated?.Invoke(TriggeredTransforms);
|
|
}
|
|
|
|
previousTriggerCount = triggerCount;
|
|
return triggerCount > 0;
|
|
}
|
|
|
|
private bool IsArrayAndListIdentical(int triggerCount)
|
|
{
|
|
if (previousTriggerCount != triggerCount)
|
|
return false;
|
|
|
|
for (int i = 0; i < triggerCount; i++)
|
|
if (!TriggeredTransforms.Contains(nonAllocResults[i].transform))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|