2021-12-23 12:41:01 +03:00
|
|
|
using UnityEngine;
|
2022-03-08 10:13:27 +03:00
|
|
|
using UnityEngine.Events;
|
2021-12-23 12:41:01 +03:00
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
namespace Syntriax.Modules.Movement.ColliderTrigger
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
2022-03-08 10:13:27 +03:00
|
|
|
public class TwoDimensionalBoxChildColliderTrigger : MonoBehaviour, IColliderTrigger
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
|
|
|
[SerializeField] private LayerMask colliderMask = 0;
|
2022-03-08 10:13:27 +03:00
|
|
|
private bool _isTrigerred = false;
|
2022-11-13 17:43:47 +03:00
|
|
|
private Collider2D[] results = new Collider2D[8];
|
2021-12-23 12:41:01 +03:00
|
|
|
|
|
|
|
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
public UnityEvent<bool> OnTriggered { get; protected set; } = null;
|
|
|
|
public bool IsTrigerred
|
|
|
|
{
|
|
|
|
get => _isTrigerred;
|
|
|
|
protected set
|
|
|
|
{
|
|
|
|
bool isNewValue = _isTrigerred != value;
|
|
|
|
|
|
|
|
_isTrigerred = value;
|
|
|
|
|
|
|
|
if (isNewValue)
|
|
|
|
OnTriggered.Invoke(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Awake()
|
|
|
|
=> OnTriggered = new UnityEvent<bool>();
|
2021-12-23 12:41:01 +03:00
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
protected void FixedUpdate()
|
2022-11-13 17:43:47 +03:00
|
|
|
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0;
|
2021-12-23 12:41:01 +03:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
{
|
2022-03-08 10:13:27 +03:00
|
|
|
Gizmos.color = IsTrigerred ? Color.green : Color.red;
|
2021-12-23 12:41:01 +03:00
|
|
|
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|