2022-12-17 14:39:54 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Trigger
|
|
|
|
{
|
2022-12-17 14:57:18 +03:00
|
|
|
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTriggerArea
|
2022-12-17 14:39:54 +03:00
|
|
|
{
|
|
|
|
protected Collider2D[] nonAllocResults = new Collider2D[32];
|
|
|
|
protected int previousTriggerCount = 0;
|
|
|
|
|
|
|
|
public LayerMask ColliderMask { get; set; } = ~0;
|
|
|
|
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
|
2022-12-17 14:57:18 +03:00
|
|
|
public List<Collider2D> ColliderList { get; protected set; } = new List<Collider2D>(32);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
|
|
|
protected abstract int TriggerCount { get; }
|
|
|
|
|
|
|
|
public virtual bool Check()
|
|
|
|
{
|
|
|
|
int triggerCount = TriggerCount;
|
|
|
|
|
|
|
|
if (triggerCount != previousTriggerCount)
|
|
|
|
{
|
2022-12-17 14:57:18 +03:00
|
|
|
ColliderList.Clear();
|
2022-12-17 14:39:54 +03:00
|
|
|
for (int i = 0; i < triggerCount; i++)
|
2022-12-17 14:57:18 +03:00
|
|
|
ColliderList.Add(nonAllocResults[i]);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
2022-12-17 14:57:18 +03:00
|
|
|
OnTriggeredCollidersUpdated?.Invoke(ColliderList);
|
2022-12-17 14:39:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
previousTriggerCount = triggerCount;
|
|
|
|
return triggerCount > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|