1
This commit is contained in:
@@ -5,7 +5,6 @@ using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using System.Linq;
|
||||
using UnityEditor.Search;
|
||||
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
@@ -13,37 +12,83 @@ namespace BITKit.Sensors
|
||||
{
|
||||
float GetVolume();
|
||||
}
|
||||
public class AudioSensor : MonoBehaviour,ISensor
|
||||
|
||||
public class AudioSensor : MonoBehaviour, ISensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] private bool autoUpdate;
|
||||
[SerializeField]private float radius;
|
||||
|
||||
[Header(Constant.Header.Settings)] [SerializeField]
|
||||
private bool autoUpdate;
|
||||
|
||||
[SerializeField] private float radius;
|
||||
private readonly CacheList<Transform> cache = new();
|
||||
private readonly CacheList<AudioSensorService.AudioSensorData> data = new();
|
||||
public AudioSensorService.AudioSensorData[] Noises => data.ValueArray;
|
||||
private readonly HashSet<int> _addedSet = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Id = GetInstanceID();
|
||||
SensorQueue.Register(Id,this);
|
||||
SensorQueue.Register(Id, this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SensorQueue.UnRegister(Id);
|
||||
}
|
||||
public UniTask Execute(float delta)
|
||||
|
||||
public async UniTask Execute(float delta)
|
||||
{
|
||||
var position = transform.position;
|
||||
cache.Clear();
|
||||
foreach (var x in AudioSensorService.QuadtreeRoot.Find(new Bounds(position, Vector3.one * radius)))
|
||||
|
||||
AudioSensorService.LockHash.Add(Id);
|
||||
|
||||
try
|
||||
{
|
||||
var distance = Vector3.Distance(position, x.Position);
|
||||
if(distance>radius) continue;
|
||||
cache.Add(x.Transform);
|
||||
var position = transform.position;
|
||||
cache.Clear();
|
||||
data.Clear();
|
||||
_addedSet.Clear();
|
||||
await UniTask.SwitchToTaskPool();
|
||||
|
||||
AudioSensorService.AudioSensorData[] value;
|
||||
|
||||
value = AudioSensorService.QuadtreeRoot.Find(new Bounds(position, Vector3.one * radius)).ToArray();
|
||||
|
||||
for (var index = 0; index < value.Length; index++)
|
||||
{
|
||||
var x = value[index];
|
||||
var distance = Vector3.Distance(position, x.Position);
|
||||
if (distance > radius) continue;
|
||||
if (Ignores.Contains(x.Id)) continue;
|
||||
_addedSet.Add(index);
|
||||
// if (x.Transform)
|
||||
// cache.Add(x.Transform);
|
||||
// data.Add(x);
|
||||
}
|
||||
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
foreach (var x in _addedSet)
|
||||
{
|
||||
if (value[x].Transform)
|
||||
cache.Add(value[x].Transform);
|
||||
data.Add(value[x]);
|
||||
}
|
||||
|
||||
}
|
||||
return UniTask.CompletedTask;
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
AudioSensorService.LockHash.Remove(Id);
|
||||
//return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public HashSet<int> Ignores { get; } = new();
|
||||
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;
|
||||
public bool AutoUpdate => autoUpdate;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user