Trigger/Runtime/Collider/ColliderTriggerAreaBase.cs

37 lines
1.0 KiB
C#

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