123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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<BikeSpeedBooster>
- lazy =
- new Lazy<BikeSpeedBooster>
- (() => new BikeSpeedBooster());
- public static BikeSpeedBooster Instance => lazy.Value;
- private readonly List<Booster> activeBooster = new List<Booster>();
- #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
- }
- }
- }
|