64 lines
1.3 KiB
C#
64 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Sensors
|
|
{
|
|
public class SensorQueue : MonoBehaviour
|
|
{
|
|
internal static readonly Dictionary<int,ISensor> Sensors=new();
|
|
private static bool IsDirty;
|
|
|
|
[SerializeField,ReadOnly] private int _position;
|
|
|
|
private static int[] _keys;
|
|
|
|
public static void Register(int id,ISensor sensor)
|
|
{
|
|
Sensors.Add(id,sensor);
|
|
MarkDirty();
|
|
}
|
|
public static void UnRegister(int id)
|
|
{
|
|
Sensors.Remove(id);
|
|
MarkDirty();
|
|
}
|
|
public static void MarkDirty()
|
|
{
|
|
IsDirty = true;
|
|
}
|
|
|
|
[SerializeField] private MonoBehaviour[] sensors;
|
|
|
|
private void Update()
|
|
{
|
|
if (SensorGlobalSettings.Enabled is false) return;
|
|
if(IsDirty)
|
|
{
|
|
_position = 0;
|
|
_keys = Sensors.Where(IsEnabled).Select(x=>x.Key).ToArray();
|
|
IsDirty = false;
|
|
sensors = Sensors.Values.Where(IsEnabled).OfType<MonoBehaviour>().ToArray();
|
|
}
|
|
|
|
if(Sensors.Count is 0) return;
|
|
|
|
Sensors.ElementAt(_position++).Value.Execute().Forget();
|
|
|
|
_position %= Sensors.Count;
|
|
}
|
|
private bool IsEnabled(ISensor sensor)
|
|
{
|
|
return sensor.AutoUpdate;
|
|
}
|
|
private bool IsEnabled(KeyValuePair<int,ISensor> pair)
|
|
{
|
|
return pair.Value.AutoUpdate;
|
|
}
|
|
}
|
|
|
|
}
|