#region usings using System; using SM.Base.Contexts; #endregion namespace SM.Base.Time { /// /// Timer-System /// public class Timer : Stopwatch { /// /// Creates a timer with specified seconds. /// /// public Timer(float seconds) { Target = seconds; } /// /// Creates a timer with a time span. /// /// public Timer(TimeSpan timeSpan) { Target = (float) timeSpan.TotalSeconds; } /// /// The target time in seconds. /// public float Target { get; private set; } /// /// The already elapsed time but normalized to the target. /// public float ElapsedNormalized { get; private set; } /// /// The event, that is triggered when the timer stops. /// public event Action EndAction; /// public override void Start() { base.Start(); Reset(); } private protected override void Tick(UpdateContext context) { base.Tick(context); ElapsedNormalized = Elapsed / Target; if (ElapsedNormalized >= 1) Stopping(context); } /// /// Occurs, when the timer tries to stop. /// protected virtual void Stopping(UpdateContext context) { EndAction?.Invoke(this, context); Stop(); } /// /// This will trigger /// /// protected void TriggerEndAction(UpdateContext context) { EndAction?.Invoke(this, context); } } }