using System.Collections.Generic;
using UnityEngine;
namespace Core.Utilities
{
///
/// Abstract based class for helping with timing in MonoBehaviours
///
public abstract class TimedBehaviour : MonoBehaviour
{
///
/// List of active timers
///
readonly List m_ActiveTimers = new List();
///
/// Adds the timer to list of active timers
///
/// the timer to be added to the list of active timers
protected void StartTimer(Timer newTimer)
{
if (m_ActiveTimers.Contains(newTimer))
{
Debug.LogWarning("Timer already exists!");
}
else
{
m_ActiveTimers.Add(newTimer);
}
}
///
/// Removes timer from list of active timers
///
/// the timer to be removed from the list of active timers
protected void PauseTimer(Timer timer)
{
if (m_ActiveTimers.Contains(timer))
{
m_ActiveTimers.Remove(timer);
}
}
///
/// Resets and removes the timer
///
/// the timer to be stopped
protected void StopTimer(Timer timer)
{
timer.Reset();
PauseTimer(timer);
}
///
/// Iterates through the list of active timers and ticks
///
protected virtual void Update()
{
for (int i = m_ActiveTimers.Count - 1; i >= 0; i--)
{
if (m_ActiveTimers[i].Tick(Time.deltaTime))
{
StopTimer(m_ActiveTimers[i]);
}
}
}
}
}