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 bool _hasBeedStarted = false; 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 float Position { get { return Math.Abs(_timerTick) / _timerMax; } } public bool Started { get { return _hasBeedStarted; } } public bool HasTicked(float currentTimestamp) { _hasBeedStarted = true; 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; } _hasBeedStarted = false; } }