1
This commit is contained in:
@@ -1,49 +1,49 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using System.Linq;
|
||||
using UnityEditor.Search;
|
||||
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public interface IAudioObject
|
||||
{
|
||||
float GetVolume();
|
||||
}
|
||||
public class AudioSensor : Sensor
|
||||
public class AudioSensor : MonoBehaviour,ISensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float radius;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
IAudioObject currentAudioObject;
|
||||
Collider currentCollider;
|
||||
Collider[] colliders = new Collider[32];
|
||||
public override IEnumerable<Transform> Get() => detected;
|
||||
public override UniTask Execute()
|
||||
[SerializeField] private bool autoUpdate;
|
||||
[SerializeField]private float radius;
|
||||
private readonly CacheList<Transform> cache = new();
|
||||
private void OnEnable()
|
||||
{
|
||||
var cacheList = ListPool<Transform>.Get();
|
||||
for (int i = 0; i < Physics.OverlapSphereNonAlloc(transform.position, radius, colliders, detectLayer); i++)
|
||||
Id = GetInstanceID();
|
||||
SensorQueue.Register(Id,this);
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
SensorQueue.UnRegister(Id);
|
||||
}
|
||||
public UniTask Execute(float delta)
|
||||
{
|
||||
var position = transform.position;
|
||||
cache.Clear();
|
||||
foreach (var x in AudioSensorService.QuadtreeRoot.Find(new Bounds(position, Vector3.one * radius)))
|
||||
{
|
||||
currentCollider = colliders[i];
|
||||
if (IsValid(currentCollider))
|
||||
{
|
||||
cacheList.Add(currentCollider.transform);
|
||||
}
|
||||
var distance = Vector3.Distance(position, x.Position);
|
||||
if(distance>radius) continue;
|
||||
cache.Add(x.Transform);
|
||||
}
|
||||
detected = cacheList.ToArray();
|
||||
ListPool<Transform>.Release(cacheList);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
public override bool IsValid(Collider _collider)
|
||||
{
|
||||
if (ignoreColliders.Contains(_collider) is false)
|
||||
if (Vector3.Distance(transform.position, _collider.transform.position) <= radius)
|
||||
if (_collider.TryGetComponent<IAudioObject>(out currentAudioObject))
|
||||
{
|
||||
return currentAudioObject.GetVolume() >= 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public override float GetDistance() => radius;
|
||||
public int Id { get; set; }
|
||||
public IEnumerable<Transform> Get() => cache.ValueArray;
|
||||
public bool IsValid(Collider _collider) => false;
|
||||
public float GetDistance() => radius;
|
||||
public bool AutoUpdate=>autoUpdate;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user