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> OnTriggeredCollidersUpdated { get; set; } = null; public List ColliderList { get; protected set; } = new List(32); protected abstract int TriggerCount { get; } public virtual bool Check() { int triggerCount = TriggerCount; if (!IsArrayAndListIdentical(triggerCount)) { ColliderList.Clear(); for (int i = 0; i < triggerCount; i++) ColliderList.Add(nonAllocResults[i]); OnTriggeredCollidersUpdated?.Invoke(ColliderList); } previousTriggerCount = triggerCount; return triggerCount > 0; } private bool IsArrayAndListIdentical(int triggerCount) { if (previousTriggerCount != triggerCount) return false; for (int i = 0; i < triggerCount; i++) if (!ColliderList.Contains(nonAllocResults[i])) return false; return true; } } }