/// <summary> /// Starts consuming the cooldown object if possible /// </summary> public virtual void Start() { if (Ready()) { CooldownState = CooldownStates.Consuming; } }
/// <summary> /// Stops consuming the object /// </summary> public virtual void Stop() { if (CooldownState == CooldownStates.Consuming) { CooldownState = CooldownStates.PauseOnEmpty; } }
/// <summary> /// An init method that ensures the object is reset /// </summary> public virtual void Initialization() { _pauseOnEmptyWFS = new WaitForSeconds(PauseOnEmptyDuration); CurrentDurationLeft = ConsumptionDuration; CooldownState = CooldownStates.Idle; _emptyReachedTimestamp = 0f; }
/// <summary> /// Processes the object's state machine /// </summary> public virtual void Update() { if (Unlimited) { return; } switch (CooldownState) { case CooldownStates.Idle: break; case CooldownStates.Consuming: CurrentDurationLeft = CurrentDurationLeft - Time.deltaTime; if (CurrentDurationLeft <= 0f) { CurrentDurationLeft = 0f; _emptyReachedTimestamp = Time.time; CooldownState = CooldownStates.PauseOnEmpty; } break; case CooldownStates.PauseOnEmpty: if (Time.time - _emptyReachedTimestamp >= PauseOnEmptyDuration) { CooldownState = CooldownStates.Refilling; } break; case CooldownStates.Refilling: CurrentDurationLeft += (RefillDuration * Time.deltaTime) / RefillDuration; if (CurrentDurationLeft >= RefillDuration) { CurrentDurationLeft = ConsumptionDuration; CooldownState = CooldownStates.Idle; } break; } }