Skip to content

Commit 09d8440

Browse files
committed
Added extension support for ServiceLocator
1 parent 801143b commit 09d8440

7 files changed

Lines changed: 206 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## [0.2.0-beta] - Feb 26, 2025
4+
- Added extension support for ServiceLocator
5+
36
## [0.1.0-beta] - Feb 26, 2025
47
- Optimized Milstones
58
- Added additional test for Milestone removal

Runtime/Extensions.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Extensions/ServiceLocator.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

Runtime/Extensions/ServiceLocator/TimerService.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
{
22
"name": "com.nonatomic.timers.runtime",
33
"rootNamespace": "Nonatomic.Timers",
4-
"references": [],
4+
"references": [
5+
"GUID:d8c8bb452aae45acbab36298ba796881"
6+
],
57
"includePlatforms": [],
68
"excludePlatforms": [],
79
"allowUnsafeCode": false,
810
"overrideReferences": true,
911
"precompiledReferences": [],
1012
"autoReferenced": true,
1113
"defineConstraints": [],
12-
"versionDefines": [],
14+
"versionDefines": [
15+
{
16+
"name": "com.nonatomic.servicelocator",
17+
"expression": "0.6.1",
18+
"define": "SERVICE_LOCATOR"
19+
}
20+
],
1321
"noEngineReferences": false
1422
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.nonatomic.timers",
3-
"version": "0.1.0-beta",
3+
"version": "0.2.0-beta",
44
"displayName": "Timers",
55
"description": "Timers",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)