Trigger/Runtime/Collider/ColliderTriggerAreaBase.cs

50 lines
1.6 KiB
C#
Raw Normal View History

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;
2022-12-17 22:44:44 +03:00
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;
}
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 (!TriggeredTransforms.Contains(nonAllocResults[i].transform))
2022-12-17 22:44:44 +03:00
return false;
return true;
}
}
}