/// <summary> /// Catches TimeScaleEvents and acts on them /// </summary> /// <param name="timeScaleEvent">MMTimeScaleEvent event.</param> public virtual void OnTimeScaleEvent(MMTimeScaleMethods timeScaleMethod, float timeScale, float duration, bool lerp, float lerpSpeed, bool infinite) { TimeScaleProperties timeScaleProperty = new TimeScaleProperties(); timeScaleProperty.TimeScale = timeScale; timeScaleProperty.Duration = duration; timeScaleProperty.Lerp = lerp; timeScaleProperty.LerpSpeed = lerpSpeed; timeScaleProperty.Infinite = infinite; switch (timeScaleMethod) { case MMTimeScaleMethods.Reset: ResetTimeScale(); break; case MMTimeScaleMethods.For: SetTimeScale(timeScaleProperty); break; case MMTimeScaleMethods.Unfreeze: Unfreeze(); break; } }
/// <summary> /// On Update, applies the timescale and resets it if needed /// </summary> protected virtual void Update() { // if we have things in our stack, we handle them, otherwise we reset to the normal timescale if (_timeScaleProperties.Count > 0) { _currentProperty = _timeScaleProperties.Peek(); TargetTimeScale = _currentProperty.TimeScale; LerpSpeed = _currentProperty.LerpSpeed; LerpTimescale = _currentProperty.Lerp; _currentProperty.Duration -= Time.unscaledDeltaTime; _timeScaleProperties.Pop(); _timeScaleProperties.Push(_currentProperty); if (_currentProperty.Duration <= 0f && !_currentProperty.Infinite) { Unfreeze(); } } else { TargetTimeScale = NormalTimescale; } // we apply our timescale if (LerpTimescale) { ApplyTimeScale(Mathf.Lerp(Time.timeScale, TargetTimeScale, Time.unscaledDeltaTime * LerpSpeed)); } else { ApplyTimeScale(TargetTimeScale); } }
/// <summary> /// When getting a freeze frame event we stop the time /// </summary> /// <param name="freezeFrameEvent">Freeze frame event.</param> public virtual void OnMMFreezeFrameEvent(float duration) { _frozenTimeLeft = duration; TimeScaleProperties properties = new TimeScaleProperties(); properties.Duration = duration; properties.Lerp = false; properties.LerpSpeed = 0f; properties.TimeScale = 0f; SetTimeScale(properties); }
/// <summary> /// On Update, applies the timescale and resets it if needed /// </summary> protected virtual void Update() { // if we have things in our stack, we handle them, otherwise we reset to the normal timescale while (_timeScaleProperties.Count > 0) { _currentProperty = _timeScaleProperties.Peek(); TargetTimeScale = _currentProperty.TimeScale; LerpSpeed = _currentProperty.LerpSpeed; LerpTimescale = _currentProperty.Lerp; _currentProperty.Duration -= Time.unscaledDeltaTime; _timeScaleProperties.Pop(); _timeScaleProperties.Push(_currentProperty); if (_currentProperty.Duration > 0f || _currentProperty.Infinite) { break; // keep current property values } else { Unfreeze(); // pop current property } } if (_timeScaleProperties.Count == 0) { TargetTimeScale = NormalTimescale; LerpTimescale = DefaultLerpTimescale; LerpSpeed = DefaultLerpSpeed; } // we apply our timescale if (LerpTimescale) { if (LerpSpeed <= 0) { LerpSpeed = 1; } ApplyTimeScale(Mathf.Lerp(Time.timeScale, TargetTimeScale, Time.unscaledDeltaTime * LerpSpeed)); } else { ApplyTimeScale(TargetTimeScale); } }
/// <summary> /// Sets the time scale for the specified properties (duration, time scale, lerp or not, and lerp speed) /// </summary> /// <param name="timeScaleProperties">Time scale properties.</param> protected virtual void SetTimeScale(TimeScaleProperties timeScaleProperties) { _timeScaleProperties.Push(timeScaleProperties); }