12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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<Tuple<float, float>> 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<Tuple<float, float>>(bufferSize);
- }
- public void AddValue(float value)
- {
- if (values.Count >= bufferSize)
- {
- values.Dequeue();
- }
- values.Enqueue(new Tuple<float, float>(Time.time, value));
- }
- }
- }
|