using System; using UnityEngine; namespace OM { /// /// A simple timer class that triggers an action after a specified duration. /// public class OMTimer : IOMUpdater { /// /// Creates a new OMTimer instance. /// /// /// /// /// /// public static OMTimer Create(float duration,Action onComplete,bool timeIndependent = false,bool persist = false) { return new OMTimer(duration,onComplete,timeIndependent,persist); } private float _duration; private readonly bool _persist; private readonly Action _onComplete; private readonly bool _timeIndependent = false; /// /// Constructor for the OMTimer class. /// /// /// /// /// private OMTimer(float duration, Action onComplete, bool timeIndependent, bool persist) { _duration = duration; _timeIndependent = timeIndependent; _onComplete = onComplete; _persist = persist; OMUpdaterRuntime.AddUpdater(this); } /// /// Gets the delta time based on the time-independent setting. /// /// private float GetDeltaTime() { return _timeIndependent ? Time.unscaledDeltaTime : Time.deltaTime; } /// /// Checks if the timer is set to persist across scenes. /// /// public bool IsDontDestroyOnLoad() { return _persist; } /// /// Checks if the timer has completed its duration. /// /// public bool IsUpdaterCompleted() { return _duration <= 0; } /// /// Updates the timer by subtracting the delta time from the duration. /// public void OnUpdate() { if(IsUpdaterCompleted()) return; _duration -= GetDeltaTime(); if (_duration <= 0) { _onComplete?.Invoke(); OMUpdaterRuntime.RemoveUpdater(this); } } /// /// Stops the timer and resets the duration to zero. /// public void Stop() { _duration = 0; } } }