Trigger/Runtime/Collider/ColliderTriggerAreaBase.cs

38 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTriggerArea
{
[SerializeField] private LayerMask colliderMask = ~0;
protected Collider2D[] nonAllocResults = new Collider2D[32];
protected int previousTriggerCount = 0;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
public List<Collider2D> ColliderList { get; protected set; } = new List<Collider2D>(32);
protected abstract int TriggerCount { get; }
public virtual bool Check()
{
int triggerCount = TriggerCount;
if (triggerCount != previousTriggerCount)
{
ColliderList.Clear();
for (int i = 0; i < triggerCount; i++)
ColliderList.Add(nonAllocResults[i]);
OnTriggeredCollidersUpdated?.Invoke(ColliderList);
}
previousTriggerCount = triggerCount;
return triggerCount > 0;
}
}
}