Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions Runtime/Components/Timers/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}
}