Trigger/Runtime/Collider/ColliderTriggerAreaBase.cs

36 lines
1.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTriggerArea
{
protected Collider2D[] nonAllocResults = new Collider2D[32];
protected int previousTriggerCount = 0;
public LayerMask ColliderMask { get; set; } = ~0;
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;
}
}
}