From 11afc8e260e55eca8bcb51a06fb14532de700753 Mon Sep 17 00:00:00 2001 From: koogelpotato <38011587+koogelpotato@users.noreply.github.com> Date: Mon, 28 Nov 2022 00:00:45 +0300 Subject: [PATCH] Allows the user to make a repeating timer The user can pick whether or not they will have a repeating or a regular timer --- Runtime/Components/Timers/Timer.cs | 40 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/Runtime/Components/Timers/Timer.cs b/Runtime/Components/Timers/Timer.cs index 3ae53c9..1e41a32 100644 --- a/Runtime/Components/Timers/Timer.cs +++ b/Runtime/Components/Timers/Timer.cs @@ -4,34 +4,38 @@ namespace DapperDino.DapperTools.Components.Timers { public class Timer { - public float RemainingSeconds { get; private set; } - - public Timer(float duration) => RemainingSeconds = duration; - + public float RemainingSeconds { get; private set;} + public bool IsRepetitive { get; private set; } + private float initialVal; public event Action OnTimerEnd; - + public Timer(float duration, bool isRepetitive) + { + RemainingSeconds = duration; + IsRepetitive = isRepetitive; + initialVal = duration; + } public void Tick(float deltaTime) { - // Stop ticking if the timer has already ended - if (RemainingSeconds == 0f) { return; } + if (RemainingSeconds == 0) { return; } - // Tick the timer down by the time it took to complete last frame RemainingSeconds -= deltaTime; - - // Check to see if the timer has finished ticking CheckForTimerEnd(); - } + } + private void CheckForTimerEnd() + { + if (RemainingSeconds > 0) { return; } - private void CheckForTimerEnd() + if(IsRepetitive) + { + RemainingSeconds = initialVal; + OnTimerEnd?.Invoke(); + } + else { - // Leave if there is still time left to tick - if (RemainingSeconds > 0f) { return; } - - // Set to zero due to duration possibly going below zero with the deltaTime subtraction RemainingSeconds = 0f; - - // Alert any listeners that the timer has ended OnTimerEnd?.Invoke(); } } + +} }