RestrictionSuggestor.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace SicknessReduction.Visual.Vignetting
  6. {
  7. public interface RestrictionSuggestor
  8. {
  9. float Suggestion { get; }
  10. }
  11. public class QueueBasedRestrictionSuggestor : RestrictionSuggestor
  12. {
  13. private Queue<Tuple<float, float>> values;
  14. private int bufferSize;
  15. private float threshold;
  16. public float Suggestion
  17. {
  18. get
  19. {
  20. var perSecond = new float[values.Count];
  21. var index = 0;
  22. var previousValue = values.FirstOrDefault();
  23. foreach (var value in values)
  24. {
  25. // ReSharper disable once PossibleNullReferenceException
  26. perSecond[index] = Mathf.Abs(value.Item2 - previousValue.Item2) /
  27. (value.Item1 - previousValue.Item1);
  28. index++;
  29. }
  30. var avg = perSecond.Average();
  31. if (avg >= threshold)
  32. {
  33. return .5f; //TODO: something better than a magic number
  34. }
  35. return 0f;
  36. }
  37. }
  38. public QueueBasedRestrictionSuggestor(float threshold, int bufferSize = 10)
  39. {
  40. this.bufferSize = bufferSize;
  41. this.threshold = threshold;
  42. values = new Queue<Tuple<float, float>>(bufferSize);
  43. }
  44. public void AddValue(float value)
  45. {
  46. if (values.Count >= bufferSize)
  47. {
  48. values.Dequeue();
  49. }
  50. values.Enqueue(new Tuple<float, float>(Time.time, value));
  51. }
  52. }
  53. }