88 lines
1.6 KiB
C#
88 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public enum TimerDirection
|
|
{
|
|
TimerIncrement,
|
|
TimerDecrement
|
|
}
|
|
|
|
public class TimerHelper
|
|
{
|
|
private float _timerInterval;
|
|
private float _timerMax;
|
|
private float _timerTick;
|
|
|
|
private int _timesRun;
|
|
|
|
private bool _timerRepeats;
|
|
|
|
private TimerDirection _timerDirection;
|
|
|
|
public TimerHelper(float timeMax, bool repeating = true, TimerDirection direction = TimerDirection.TimerIncrement)
|
|
{
|
|
_timerMax = timeMax;
|
|
_timerRepeats = repeating;
|
|
_timerDirection = direction;
|
|
|
|
if (_timerDirection == TimerDirection.TimerDecrement)
|
|
{
|
|
_timerInterval = 0.0f;
|
|
_timerTick = -_timerMax;
|
|
}
|
|
else
|
|
{
|
|
_timerInterval = _timerMax;
|
|
_timerTick = 0.0f;
|
|
}
|
|
}
|
|
|
|
public float CurrentTick
|
|
{
|
|
get
|
|
{
|
|
return Math.Abs(_timerTick);
|
|
}
|
|
}
|
|
|
|
public int TimesRun
|
|
{
|
|
get
|
|
{
|
|
return _timesRun;
|
|
}
|
|
}
|
|
|
|
public bool HasTicked(float currentTimestamp)
|
|
{
|
|
if (_timerTick >= _timerInterval)
|
|
{
|
|
_timesRun++;
|
|
if (_timerRepeats)
|
|
{
|
|
RestartTimer();
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
_timerTick += currentTimestamp;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void RestartTimer()
|
|
{
|
|
if (_timerDirection == TimerDirection.TimerDecrement)
|
|
{
|
|
_timerTick = -_timerMax;
|
|
}
|
|
else
|
|
{
|
|
_timerTick = 0.0f;
|
|
}
|
|
}
|
|
|
|
}
|
|
|