namespace Otter { /// /// A timer that automatically counts on an increment. Useful for handling things like cooldowns. /// public class AutoTimer : Component { #region Public Fields /// /// The current value of the timer. /// public float Value; /// /// The maximum possible value of the timer. /// public float Max; /// /// The minimum possible value of the timer. /// public float Min; /// /// How much the timer increments each update. /// public float Increment = 1; #endregion #region Public Properties /// /// If the timer is currently paused. /// public bool Paused { get; private set; } /// /// If the timer is currently at its maximum value. /// public bool AtMax { get { return Value == Max; } } /// /// If the timer is currently at its minimum value. /// public bool AtMin { get { return Value == Min; } } #endregion #region Constructors /// /// Create an AutoTimer. /// /// The maximum value of the timer. public AutoTimer(float max) { Max = max; } /// /// Create an AutoTimer. /// /// The initial value of the timer. /// The minimum value of the timer. /// The maximum value of the timer. /// The value that the timer increments with each update. public AutoTimer(float value, float min, float max, float increment) { Value = value; Max = max; Min = min; Increment = increment; } #endregion #region Public Methods /// /// Update the AutoTimer. /// public override void Update() { base.Update(); if (!Paused) { Value += Increment; } Value = Util.Clamp(Value, Min, Max); } /// /// Reset the timer to 0. /// public void Reset() { Value = 0; } /// /// Pause the timer. /// public void Pause() { Paused = true; } /// /// Resume the timer if paused. /// public void Resume() { Paused = false; } /// /// Start the timer again from 0. /// public void Start() { Reset(); Paused = false; } /// /// Stop the timer and set the value to 0. /// public void Stop() { Paused = true; Reset(); } #endregion #region Operators public static implicit operator float(AutoTimer timer) { return timer.Value; } public static implicit operator int(AutoTimer timer) { return (int)timer.Value; } #endregion } }