1+ #if SERVICE_LOCATOR
2+
3+ using System ;
4+ using Nonatomic . ServiceLocator ;
5+ using UnityEngine ;
6+
7+ namespace Nonatomic . Timers . Extensions . ServiceLocator
8+ {
9+ public interface ITimerService : ITimer
10+ {
11+
12+ }
13+
14+ /// <summary>
15+ /// A Unity MonoBehaviour that manages a timer, allowing it to be used within the Unity Engine's update system.
16+ /// It delegates timer functionality to a standard SimpleTimer object and synchronizes with Unity's game time.
17+ /// </summary>
18+ public class TimerService : MonoService < ITimerService > , ITimerService
19+ {
20+ public event Action OnStart ;
21+ public event Action OnResume ;
22+ public event Action OnStop ;
23+ public event Action OnComplete ;
24+ public event Action < IReadOnlyTimer > OnTick ;
25+
26+ /// <summary>
27+ /// Gets the running state of the timer.
28+ /// </summary>
29+ public bool IsRunning => _timer . IsRunning ;
30+
31+ /// <summary>
32+ /// Gets or sets the total duration of the timer in seconds.
33+ /// Exposed in the Unity Inspector to allow easy adjustments.
34+ /// </summary>
35+ public float Duration
36+ {
37+ get => _timer . Duration ;
38+ set
39+ {
40+ _timer . Duration = value ;
41+ }
42+ }
43+
44+ /// <summary>
45+ /// Gets the remaining time in seconds.
46+ /// </summary>
47+ public float TimeRemaining => _timer . TimeRemaining ;
48+
49+ /// <summary>
50+ /// Gets the elapsed time in seconds since the timer was started.
51+ /// </summary>
52+ public float TimeElapsed => _timer . TimeElapsed ;
53+
54+ /// <summary>
55+ /// Gets the progress of the timer as a fraction of the elapsed time over the total duration.
56+ /// </summary>
57+ public float ProgressElapsed => _timer . ProgressElapsed ;
58+
59+ /// <summary>
60+ /// Gets the remaining progress of the timer as a fraction.
61+ /// </summary>
62+ public float ProgressRemaining => _timer . ProgressRemaining ;
63+
64+ [ SerializeField ] private float _duration = 10f ;
65+ [ SerializeField ] private bool _useScaledTime = true ;
66+ [ SerializeField ] private bool _runOnStart = false ;
67+
68+ private SimpleTimer _timer ;
69+
70+ /// <summary>
71+ /// Gets the time as either TimeRemaining, TimeElapsed, ProgressElapsed, ProgressRemaining
72+ /// </summary>
73+ public virtual float TimeByType ( TimeType type ) => _timer . TimeByType ( type ) ;
74+
75+ /// <summary>
76+ /// Starts or restarts the timer.
77+ /// </summary>
78+ public virtual void StartTimer ( ) => _timer . StartTimer ( ) ;
79+
80+ /// <summary>
81+ ///Resumes the timer without resetting.
82+ /// </summary>
83+ public virtual void ResumeTimer ( ) => _timer . ResumeTimer ( ) ;
84+
85+ /// <summary>
86+ /// Stops the timer, pausing the countdown.
87+ /// </summary>
88+ public virtual void StopTimer ( ) => _timer . StopTimer ( ) ;
89+
90+ /// <summary>
91+ /// Resets the timer to its initial state with the full duration remaining.
92+ /// </summary>
93+ public virtual void ResetTimer ( ) => _timer . ResetTimer ( ) ;
94+
95+ /// <summary>
96+ /// Adds a milestone to the timer that will trigger a callback when a specific timer condition is met.
97+ /// </summary>
98+ public virtual void AddMilestone ( TimerMilestone milestone ) => _timer . AddMilestone ( milestone ) ;
99+
100+ /// <summary>
101+ /// Removes a specific milestone from the timer.
102+ /// </summary>
103+ public virtual void RemoveMilestone ( TimerMilestone milestone ) => _timer . RemoveMilestone ( milestone ) ;
104+
105+ /// <summary>
106+ /// Clears all milestones from the timer, ceasing any pending triggers.
107+ /// </summary>
108+ public virtual void ClearMilestones ( ) => _timer . ClearMilestones ( ) ;
109+
110+ /// <summary>
111+ /// Removes milestones that meet a specific condition.
112+ /// </summary>
113+ public virtual void RemoveMilestonesByCondition ( Predicate < TimerMilestone > condition ) => _timer . RemoveMilestonesByCondition ( condition ) ;
114+
115+ protected override void Awake ( )
116+ {
117+ base . Awake ( ) ;
118+ _timer = new SimpleTimer ( _duration ) ;
119+ ServiceReady ( ) ;
120+ }
121+
122+ protected virtual void OnEnable ( )
123+ {
124+ _timer . OnStart += HandleTimerStart ;
125+ _timer . OnResume += HandleTimerResume ;
126+ _timer . OnStop += HandleTimerStopped ;
127+ _timer . OnComplete += HandleTimerComplete ;
128+ _timer . OnTick += HandleTimerTick ;
129+ }
130+
131+ protected virtual void OnDisable ( )
132+ {
133+ _timer . OnStart -= HandleTimerStart ;
134+ _timer . OnResume -= HandleTimerResume ;
135+ _timer . OnStop -= HandleTimerStopped ;
136+ _timer . OnComplete -= HandleTimerComplete ;
137+ _timer . OnTick -= HandleTimerTick ;
138+ }
139+
140+ protected virtual void Start ( )
141+ {
142+ if ( ! _runOnStart ) return ;
143+ StartTimer ( ) ;
144+ }
145+
146+ /// <summary>
147+ /// Updates the timer based on Unity's game time, using scaled or unscaled time as configured.
148+ /// </summary>
149+ protected virtual void Update ( )
150+ {
151+ var deltaTime = _useScaledTime ? Time . deltaTime : Time . unscaledDeltaTime ;
152+ _timer ? . Update ( deltaTime ) ;
153+ }
154+
155+ /// <summary>
156+ /// Invokes the OnTick event with the current remaining time.
157+ /// </summary>
158+ /// <param name="timer">A readonly instance of the timer allowing for time queries</param>
159+ protected virtual void HandleTimerTick ( IReadOnlyTimer timer ) => OnTick ? . Invoke ( timer ) ;
160+
161+ /// <summary>
162+ /// Invokes the OnComplete event when the timer completes.
163+ /// </summary>
164+ protected virtual void HandleTimerComplete ( ) => OnComplete ? . Invoke ( ) ;
165+
166+ /// <summary>
167+ /// Invokes the OnStart event when the timer starts.
168+ /// </summary>
169+ protected virtual void HandleTimerStart ( ) => OnStart ? . Invoke ( ) ;
170+
171+ /// <summary>
172+ /// Invokes the OnResume event when the timer resumes.
173+ /// </summary>
174+ protected virtual void HandleTimerResume ( ) => OnResume ? . Invoke ( ) ;
175+
176+ /// <summary>
177+ /// Invokes the OnStop event when the timer is stopped.
178+ /// </summary>
179+ protected virtual void HandleTimerStopped ( ) => OnStop ? . Invoke ( ) ;
180+ }
181+ }
182+
183+ #endif
0 commit comments