using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Controller.Bicycle { struct Booster { public float Start { get; } public float End { get; } public float MaxValue { get; } public bool Increasing { get; } public Booster(float start, float end, float maxValue, bool increasing) { this.Start = start; this.End = end; this.MaxValue = maxValue; this.Increasing = increasing; } } public class BikeSpeedBooster { #region singleton private static readonly Lazy lazy = new Lazy (() => new BikeSpeedBooster()); public static BikeSpeedBooster Instance => lazy.Value; private readonly List activeBooster = new List(); #endregion private const float ZERO_SLOPE_THRES = 0.05f; public float Boost { get; private set; } public void OnSlopeChanged(float timestamp, float slope) { if (Mathf.Abs(slope) < ZERO_SLOPE_THRES) return; //TODO: what happens when zero? var duration = 0f; //TODO: calculate var maxValue = 0f; //TODO activeBooster.Add(new Booster(Time.time, Time.time + duration, maxValue, slope < 0f)); } public void OnFixedUpdate() { foreach (var b in activeBooster.Where(b => b.End <= Time.fixedTime)) { activeBooster.Remove(b); } //TODO: clamp Boost = activeBooster.Sum(b => b.MaxValue * 0.7f); //TODO: factor } } }