-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopwatch.cs
More file actions
186 lines (159 loc) · 5.96 KB
/
Stopwatch.cs
File metadata and controls
186 lines (159 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using KongZEE.Timer.Delay;
using UnityEngine;
using UnityEngine.Events;
namespace KongZEE.Timer
{
[Serializable]
public class Stopwatch
{
private MonoBehaviour monoBehaviour;
Dictionary<StopwatchKey, Coroutine> coroutines = new();
private StopwatchKey _stopwatchKeyDisposable;
public Stopwatch(MonoBehaviour monoBehaviour)
{
this.monoBehaviour = monoBehaviour;
}
public void StartTimerDisposable(UnityAction<float> action = null)
{
if (coroutines.Count > 0 && coroutines.ContainsKey(_stopwatchKeyDisposable))
{
monoBehaviour.StopCoroutine(coroutines[_stopwatchKeyDisposable]);
coroutines.Remove(_stopwatchKeyDisposable);
}
_stopwatchKeyDisposable = StartTimer(0, action);
}
public StopwatchKey StartTimer(UnityAction<float> action = null)
{
return StartTimer(0, action);
}
public StopwatchKey StartTimer(float startTime = 0, UnityAction<float> action = null)
{
StopwatchKey stopwatchKey = new StopwatchKey();
stopwatchKey.time = startTime;
if (action != null)
{
action += time => { stopwatchKey.time = time;};
}
Coroutine coroutine = monoBehaviour.StartCoroutine(StopwatchRunner(() => stopwatchKey.time, () => stopwatchKey.paused, () => stopwatchKey.stop, action));
coroutines.Add(stopwatchKey, coroutine);
stopwatchKey.action = action;
return stopwatchKey;
}
public void ResumeTimer(StopwatchKey stopwatchKey = null)
{
if (stopwatchKey == null)
{
if (coroutines.Count > 0)
{
coroutines.Keys.ElementAt(0).paused = false;
}
}
else if (coroutines.ContainsKey(stopwatchKey))
{
stopwatchKey.paused = false;
}
}
public void PauseTimer(StopwatchKey stopwatchKey = null)
{
if (stopwatchKey == null)
{
if (coroutines.Count > 0)
{
coroutines.Keys.ElementAt(0).paused = true;
Debug.Log($"Pause Timer Name : {monoBehaviour.gameObject.name}");
}
}
else if (coroutines.ContainsKey(stopwatchKey))
{
stopwatchKey.paused = true;
Debug.Log($"Pause Timer Name : {monoBehaviour.gameObject.name}");
}
}
public void StopTimer(StopwatchKey stopwatchKey = null)
{
if (stopwatchKey == null)
{
if (coroutines.Count > 0)
{
monoBehaviour.StopCoroutine(coroutines.Values.ElementAt(0));
}
}
else if (coroutines.ContainsKey(stopwatchKey))
{
stopwatchKey.stop = true;
stopwatchKey.paused = true;
stopwatchKey.action = null;
monoBehaviour.StopCoroutine(coroutines[stopwatchKey]);
coroutines.Remove(stopwatchKey);
}
}
public void ResetTimer(StopwatchKey stopwatchKey = null)
{
if (coroutines.Count < 0) return;
if (stopwatchKey == null)
{
if (coroutines.Count > 0)
{
monoBehaviour.StopCoroutine(coroutines.Values.ElementAt(0));
coroutines.Remove(coroutines.Keys.ElementAt(0));
}
}
else if (coroutines.ContainsKey(stopwatchKey))
{
monoBehaviour.StopCoroutine(coroutines[stopwatchKey]);
coroutines.Remove(stopwatchKey);
}
StopwatchKey currentDelayKey = stopwatchKey ?? coroutines.Keys.ElementAt(0);
currentDelayKey.paused = false;
currentDelayKey.stop = false;
currentDelayKey.action?.Invoke(0.0f);
currentDelayKey.time = 0.0f;
Coroutine coroutine = monoBehaviour.StartCoroutine(StopwatchRunner(() => currentDelayKey.time, () => currentDelayKey.paused, () => currentDelayKey.stop, currentDelayKey.action));
coroutines.Add(currentDelayKey, coroutine);
}
public void StopAllTimers()
{
foreach (var coroutine in coroutines)
{
monoBehaviour.StopCoroutine(coroutine.Value);
}
}
private IEnumerator StopwatchRunner(Func<float> startTime, Func<bool> pauseFunc, Func<bool> stopFunc, UnityAction<float> action)
{
bool pause = pauseFunc?.Invoke() ?? true;
while (!stopFunc?.Invoke() ?? true)
{
pause = pauseFunc?.Invoke() ?? true;
float time = startTime?.Invoke() ?? 0.0f;
if (!pause)
{
time += Time.deltaTime;
action?.Invoke(time);
}
yield return null;
}
}
}
[Serializable]
public class StopwatchKey
{
public bool paused;
public bool stop;
public float time;
public UnityAction<float> action;
public StopwatchKey()
{
}
public StopwatchKey(bool paused, bool stop, float time, UnityAction<float> action)
{
this.paused = paused;
this.stop = stop;
this.time = time;
this.action = action;
}
}
}