70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace BITKit.Sensors
|
|
{
|
|
public class TriggerSensor : MonoBehaviour, ISensor
|
|
{
|
|
[Header(Constant.Header.Settings)] [SerializeField]
|
|
private List<GameObject> ignores = new();
|
|
|
|
[Header(Constant.Header.Events)] public UnityEvent<Collider> onDetected = new();
|
|
public UnityEvent<Collider> onLost = new();
|
|
|
|
[Header(Constant.Header.InternalVariables)]
|
|
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
|
private List<Collider> detected = new();
|
|
|
|
private void OnTriggerEnter(Collider _collider)
|
|
{
|
|
if (_collider.gameObject.isStatic) return;
|
|
if (IsValid(_collider) is false) return;
|
|
if (detected.Contains(_collider)) return;
|
|
detected.Add(_collider);
|
|
onDetected.Invoke(_collider);
|
|
}
|
|
|
|
private void OnTriggerExit(Collider _collider)
|
|
{
|
|
if (_collider.gameObject.isStatic) return;
|
|
if (IsValid(_collider) is false) return;
|
|
if (!detected.Remove(_collider)) return;
|
|
onLost.Invoke(_collider);
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
var _collider = collision.collider;
|
|
if (!IsValid(_collider)) return;
|
|
detected.Add(_collider);
|
|
onDetected.Invoke(_collider);
|
|
}
|
|
|
|
private void OnCollisionStay(Collision collision)
|
|
{
|
|
var _collider = collision.collider;
|
|
if (detected.TryRemove(_collider))
|
|
{
|
|
onLost.Invoke(_collider);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Transform> Get() => detected.Select(x => x.transform).ToArray();
|
|
|
|
public bool IsValid(Collider _collider) => ignores.Contains(_collider.gameObject) is false;
|
|
|
|
public float GetDistance()
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public UniTask Execute()
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
} |