using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace SicknessReduction.Visual.Vignetting { public interface RestrictionSuggestor { float Suggestion { get; } } public class QueueBasedRestrictionSuggestor : RestrictionSuggestor { private Queue> values; private int bufferSize; private float threshold; public float Suggestion { get { var perSecond = new float[values.Count]; var index = 0; var previousValue = values.FirstOrDefault(); foreach (var value in values) { // ReSharper disable once PossibleNullReferenceException perSecond[index] = Mathf.Abs(value.Item2 - previousValue.Item2) / (value.Item1 - previousValue.Item1); index++; } var avg = perSecond.Average(); if (avg >= threshold) { return .5f; //TODO: something better than a magic number } return 0f; } } public QueueBasedRestrictionSuggestor(float threshold, int bufferSize = 10) { this.bufferSize = bufferSize; this.threshold = threshold; values = new Queue>(bufferSize); } public void AddValue(float value) { if (values.Count >= bufferSize) { values.Dequeue(); } values.Enqueue(new Tuple(Time.time, value)); } } }