BikeSpeedBooster.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Controller.Bicycle
  6. {
  7. struct Booster
  8. {
  9. public float Start { get; }
  10. public float End { get; }
  11. public float MaxValue { get; }
  12. public bool Increasing { get; }
  13. public Booster(float start, float end, float maxValue, bool increasing)
  14. {
  15. this.Start = start;
  16. this.End = end;
  17. this.MaxValue = maxValue;
  18. this.Increasing = increasing;
  19. }
  20. }
  21. public class BikeSpeedBooster
  22. {
  23. #region singleton
  24. private static readonly Lazy<BikeSpeedBooster>
  25. lazy =
  26. new Lazy<BikeSpeedBooster>
  27. (() => new BikeSpeedBooster());
  28. public static BikeSpeedBooster Instance => lazy.Value;
  29. private readonly List<Booster> activeBooster = new List<Booster>();
  30. #endregion
  31. private const float ZERO_SLOPE_THRES = 0.05f;
  32. public float Boost { get; private set; }
  33. public void OnSlopeChanged(float timestamp, float slope)
  34. {
  35. if (Mathf.Abs(slope) < ZERO_SLOPE_THRES) return; //TODO: what happens when zero?
  36. var duration = 0f; //TODO: calculate
  37. var maxValue = 0f; //TODO
  38. activeBooster.Add(new Booster(Time.time, Time.time + duration, maxValue, slope < 0f));
  39. }
  40. public void OnFixedUpdate()
  41. {
  42. foreach (var b in activeBooster.Where(b => b.End <= Time.fixedTime))
  43. {
  44. activeBooster.Remove(b);
  45. }
  46. //TODO: clamp
  47. Boost = activeBooster.Sum(b => b.MaxValue * 0.7f); //TODO: factor
  48. }
  49. }
  50. }