This commit is contained in:
Syntriax 2023-02-16 13:08:18 +03:00
commit d3db00722a
7 changed files with 60 additions and 17 deletions

View File

@ -2,7 +2,7 @@ using UnityEngine;
namespace Syntriax.Modules.Trigger namespace Syntriax.Modules.Trigger
{ {
public class Box2DColliderTriggerArea : ColliderTriggerAreaBase public class Box2DColliderTriggerArea : ColliderTriggerAreaBase<Collider2D>
{ {
protected override int TriggerCount protected override int TriggerCount
=> Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, nonAllocResults, ColliderMask); => Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, nonAllocResults, ColliderMask);
@ -10,7 +10,10 @@ namespace Syntriax.Modules.Trigger
#if UNITY_EDITOR #if UNITY_EDITOR
private void OnDrawGizmosSelected() private void OnDrawGizmosSelected()
{ {
if (UnityEditor.EditorApplication.isPlaying)
Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red; Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red;
else
Gizmos.color = TriggerCount > 0 ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale); Gizmos.DrawWireCube(transform.position, transform.lossyScale);
} }
#endif #endif

View File

@ -2,7 +2,7 @@ using UnityEngine;
namespace Syntriax.Modules.Trigger namespace Syntriax.Modules.Trigger
{ {
public class Box2DColliderTriggerAreaWithRotation : ColliderTriggerAreaBase public class Box2DColliderTriggerAreaWithRotation : ColliderTriggerAreaBase<Collider2D>
{ {
protected override int TriggerCount protected override int TriggerCount
=> Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, transform.rotation.eulerAngles.z, nonAllocResults, ColliderMask); => Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, transform.rotation.eulerAngles.z, nonAllocResults, ColliderMask);
@ -10,7 +10,10 @@ namespace Syntriax.Modules.Trigger
#if UNITY_EDITOR #if UNITY_EDITOR
private void OnDrawGizmosSelected() private void OnDrawGizmosSelected()
{ {
if (UnityEditor.EditorApplication.isPlaying)
Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red; Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red;
else
Gizmos.color = TriggerCount > 0 ? Color.green : Color.red;
Gizmos.matrix = transform.localToWorldMatrix; Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one); Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
} }

View File

@ -0,0 +1,22 @@
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class BoxColliderTriggerArea : ColliderTriggerAreaBase<Collider>
{
protected override int TriggerCount
=> Physics.OverlapBoxNonAlloc(transform.position, transform.localScale * .5f, nonAllocResults, transform.rotation, ColliderMask);
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
if (UnityEditor.EditorApplication.isPlaying)
Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red;
else
Gizmos.color = TriggerCount > 0 ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 850a555be97ed9b4a86942b21a6a25e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,16 +4,16 @@ using UnityEngine;
namespace Syntriax.Modules.Trigger namespace Syntriax.Modules.Trigger
{ {
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTriggerArea public abstract class ColliderTriggerAreaBase<T> : MonoBehaviour, IColliderTriggerArea where T : Component
{ {
[SerializeField] private LayerMask colliderMask = ~0; [SerializeField] private LayerMask colliderMask = ~0;
protected Collider2D[] nonAllocResults = new Collider2D[32]; protected T[] nonAllocResults = new T[32];
protected int previousTriggerCount = 0; protected int previousTriggerCount = 0;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; } public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null; public Action<List<Transform>> OnTriggeredTransformsUpdated { get; set; } = null;
public List<Collider2D> ColliderList { get; protected set; } = new List<Collider2D>(32); public List<Transform> TriggeredTransforms { get; protected set; } = new List<Transform>(32);
protected abstract int TriggerCount { get; } protected abstract int TriggerCount { get; }
@ -23,11 +23,11 @@ namespace Syntriax.Modules.Trigger
if (!IsArrayAndListIdentical(triggerCount)) if (!IsArrayAndListIdentical(triggerCount))
{ {
ColliderList.Clear(); TriggeredTransforms.Clear();
for (int i = 0; i < triggerCount; i++) for (int i = 0; i < triggerCount; i++)
ColliderList.Add(nonAllocResults[i]); TriggeredTransforms.Add(nonAllocResults[i].transform);
OnTriggeredCollidersUpdated?.Invoke(ColliderList); OnTriggeredTransformsUpdated?.Invoke(TriggeredTransforms);
} }
previousTriggerCount = triggerCount; previousTriggerCount = triggerCount;
@ -40,7 +40,7 @@ namespace Syntriax.Modules.Trigger
return false; return false;
for (int i = 0; i < triggerCount; i++) for (int i = 0; i < triggerCount; i++)
if (!ColliderList.Contains(nonAllocResults[i])) if (!TriggeredTransforms.Contains(nonAllocResults[i].transform))
return false; return false;
return true; return true;

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public class BoxGroundTriggerArea : BoxColliderTriggerArea, IGroundTriggerArea { }
}

View File

@ -12,12 +12,12 @@ namespace Syntriax.Modules.Trigger
LayerMask ColliderMask { get; set; } LayerMask ColliderMask { get; set; }
/// <summary> /// <summary>
/// Called whenever the list of <see cref="Collider2D"/> changes /// Called whenever the list of <see cref="Transform"/> changes
/// </summary> /// </summary>
/// <value>List of <see cref="Collider2D"/> that triggered the <see cref="IColliderTriggerArea"/></value> /// <value>List of <see cref="Transform"/> that their colliders triggered the <see cref="IColliderTriggerArea"/></value>
Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } Action<List<Transform>> OnTriggeredTransformsUpdated { get; set; }
/// <value>List of <see cref="Collider2D"/> that triggered the <see cref="IColliderTriggerArea"/></value> /// <value>List of <see cref="Transform"/> that their colliders triggered the <see cref="IColliderTriggerArea"/></value>
List<Collider2D> ColliderList { get; } List<Transform> TriggeredTransforms { get; }
} }
} }