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 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().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 pair) { return pair.Value.AutoUpdate; } } }