1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
@@ -10,6 +11,7 @@ namespace BITKit.Sensors
|
||||
public class SensorQueue : MonoBehaviour
|
||||
{
|
||||
internal static readonly Dictionary<int,ISensor> Sensors=new();
|
||||
internal static readonly ConcurrentDictionary<int, float> LastDetectedTime = new();
|
||||
private static bool IsDirty;
|
||||
|
||||
[SerializeField,ReadOnly] private int _position;
|
||||
@@ -32,10 +34,25 @@ namespace BITKit.Sensors
|
||||
}
|
||||
|
||||
[SerializeField] private MonoBehaviour[] sensors;
|
||||
|
||||
private void Update()
|
||||
[SerializeReference,SubclassSelector] private ITicker ticker;
|
||||
|
||||
private bool _isBusy;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
ticker.Add(OnTick);
|
||||
destroyCancellationToken.Register(Dispose);
|
||||
}
|
||||
private void Dispose()
|
||||
{
|
||||
ticker.Remove(OnTick);
|
||||
}
|
||||
private async void OnTick(float obj)
|
||||
{
|
||||
if (_isBusy) return;
|
||||
if (SensorGlobalSettings.Enabled is false) return;
|
||||
|
||||
_isBusy = true;
|
||||
if(IsDirty)
|
||||
{
|
||||
_position = 0;
|
||||
@@ -44,11 +61,25 @@ namespace BITKit.Sensors
|
||||
sensors = Sensors.Values.Where(IsEnabled).OfType<MonoBehaviour>().ToArray();
|
||||
}
|
||||
|
||||
if(Sensors.Count is 0) return;
|
||||
|
||||
Sensors.ElementAt(_position++).Value.Execute().Forget();
|
||||
if (Sensors.Count is 0)
|
||||
{
|
||||
_isBusy = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var current = Sensors.ElementAt(_position++).Value;
|
||||
var currentUpdateTime = LastDetectedTime.GetOrAdd(current.Id,Time.time);
|
||||
await current.Execute(Time.time-currentUpdateTime);
|
||||
float UpdateValueFactory(int key, float old) => Time.time;
|
||||
LastDetectedTime.AddOrUpdate(current.Id,Time.time,UpdateValueFactory);
|
||||
|
||||
if (destroyCancellationToken.IsCancellationRequested) {
|
||||
_isBusy = false;
|
||||
return;
|
||||
}
|
||||
|
||||
_position %= Sensors.Count;
|
||||
_isBusy = false;
|
||||
}
|
||||
private bool IsEnabled(ISensor sensor)
|
||||
{
|
||||
@@ -60,4 +91,4 @@ namespace BITKit.Sensors
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user