2022-12-17 14:39:54 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Trigger
|
|
|
|
{
|
2023-02-16 13:03:30 +03:00
|
|
|
public abstract class ColliderTriggerAreaBase<T> : MonoBehaviour, IColliderTriggerArea where T : Component
|
2022-12-17 14:39:54 +03:00
|
|
|
{
|
2022-12-17 15:28:34 +03:00
|
|
|
[SerializeField] private LayerMask colliderMask = ~0;
|
|
|
|
|
2023-02-16 13:03:30 +03:00
|
|
|
protected T[] nonAllocResults = new T[32];
|
2022-12-17 14:39:54 +03:00
|
|
|
protected int previousTriggerCount = 0;
|
|
|
|
|
2022-12-17 15:28:34 +03:00
|
|
|
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
2023-02-16 13:03:30 +03:00
|
|
|
public Action<List<Transform>> OnTriggeredTransformsUpdated { get; set; } = null;
|
|
|
|
public List<Transform> TriggeredTransforms { get; protected set; } = new List<Transform>(32);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
|
|
|
protected abstract int TriggerCount { get; }
|
|
|
|
|
|
|
|
public virtual bool Check()
|
|
|
|
{
|
|
|
|
int triggerCount = TriggerCount;
|
|
|
|
|
2022-12-17 22:44:44 +03:00
|
|
|
if (!IsArrayAndListIdentical(triggerCount))
|
2022-12-17 14:39:54 +03:00
|
|
|
{
|
2023-02-16 13:03:30 +03:00
|
|
|
TriggeredTransforms.Clear();
|
2022-12-17 14:39:54 +03:00
|
|
|
for (int i = 0; i < triggerCount; i++)
|
2023-02-16 13:03:30 +03:00
|
|
|
TriggeredTransforms.Add(nonAllocResults[i].transform);
|
2022-12-17 14:39:54 +03:00
|
|
|
|
2023-02-16 13:03:30 +03:00
|
|
|
OnTriggeredTransformsUpdated?.Invoke(TriggeredTransforms);
|
2022-12-17 14:39:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
previousTriggerCount = triggerCount;
|
|
|
|
return triggerCount > 0;
|
|
|
|
}
|
2022-12-17 22:44:44 +03:00
|
|
|
|
|
|
|
private bool IsArrayAndListIdentical(int triggerCount)
|
|
|
|
{
|
|
|
|
if (previousTriggerCount != triggerCount)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < triggerCount; i++)
|
2023-02-16 13:03:30 +03:00
|
|
|
if (!TriggeredTransforms.Contains(nonAllocResults[i].transform))
|
2022-12-17 22:44:44 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-20 22:24:18 +03:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
private State.IStateEnable stateEnable = null;
|
|
|
|
public State.IStateEnable StateEnable
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
stateEnable = stateEnable ?? GetComponent<State.IStateEnable>() ?? gameObject.AddComponent<State.StateEnableMonoBehaviour>();
|
|
|
|
return stateEnable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-17 14:39:54 +03:00
|
|
|
}
|
|
|
|
}
|