iFactory.Cutting.Unity/Assets/BITKit/Unity/Scripts/Sensor/AudioSensor.cs

55 lines
1.8 KiB
C#
Raw Normal View History

2024-04-16 04:15:06 +08:00
using System;
2024-01-23 02:56:26 +08:00
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Pool;
using System.Linq;
2024-04-16 04:15:06 +08:00
2024-01-23 02:56:26 +08:00
namespace BITKit.Sensors
{
public interface IAudioObject
{
float GetVolume();
}
2024-04-16 04:15:06 +08:00
public class AudioSensor : MonoBehaviour,ISensor
2024-01-23 02:56:26 +08:00
{
[Header(Constant.Header.Settings)]
2024-04-16 04:15:06 +08:00
[SerializeField] private bool autoUpdate;
[SerializeField]private float radius;
private readonly CacheList<Transform> cache = new();
2024-04-22 03:48:37 +08:00
private readonly CacheList<AudioSensorService.AudioSensorData> data = new();
public AudioSensorService.AudioSensorData[] Noises => data.ValueArray;
2024-04-16 04:15:06 +08:00
private void OnEnable()
2024-01-23 02:56:26 +08:00
{
2024-04-16 04:15:06 +08:00
Id = GetInstanceID();
SensorQueue.Register(Id,this);
}
private void OnDisable()
{
SensorQueue.UnRegister(Id);
}
public UniTask Execute(float delta)
{
var position = transform.position;
cache.Clear();
2024-04-22 03:48:37 +08:00
data.Clear();
2024-04-16 04:15:06 +08:00
foreach (var x in AudioSensorService.QuadtreeRoot.Find(new Bounds(position, Vector3.one * radius)))
2024-01-23 02:56:26 +08:00
{
2024-04-22 03:48:37 +08:00
var distance = Vector3.Distance(position, x.Position);
if (distance > radius) continue;
if (x.Transform)
cache.Add(x.Transform);
data.Add(x);
2024-01-23 02:56:26 +08:00
}
2024-04-22 03:48:37 +08:00
2024-01-23 02:56:26 +08:00
return UniTask.CompletedTask;
}
2024-04-22 03:48:37 +08:00
public HashSet<int> Ignores { get; } = new();
2024-04-16 04:15:06 +08:00
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;
2024-01-23 02:56:26 +08:00
}
}