-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cs
More file actions
175 lines (151 loc) · 5.39 KB
/
Timer.cs
File metadata and controls
175 lines (151 loc) · 5.39 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
namespace KongZEE.Timer
{
public class Timer
{
private MonoBehaviour monoBehaviour;
Dictionary<TimerKey, Coroutine> coroutines = new ();
private TimerKey _timerKeyDisposable;
public Timer(MonoBehaviour monoBehaviour)
{
this.monoBehaviour = monoBehaviour;
}
public void StartTimerDisposable(float seconds, UnityAction<float> action = null)
{
if (coroutines.Count > 0 && coroutines.ContainsKey(_timerKeyDisposable))
{
monoBehaviour.StopCoroutine(coroutines[_timerKeyDisposable]);
coroutines.Remove(_timerKeyDisposable);
}
_timerKeyDisposable = StartTimer(seconds, action);
}
public TimerKey StartTimer(float seconds, UnityAction<float> action = null)
{
Debug.Log($"StartTimer : {seconds}");
TimerKey timerKey = new TimerKey();
if (action != null)
{
action += time => { timerKey.elapsed = time; };
}
timerKey.time = seconds;
Coroutine coroutine = monoBehaviour.StartCoroutine(TimerRunner(() => timerKey.time, () => timerKey.paused, action));
coroutines.Add(timerKey, coroutine);
timerKey.action = action;
return timerKey;
}
public void ResumeTimer(TimerKey timerKey = null)
{
if (timerKey == null)
{
if (coroutines.Count > 0)
{
coroutines.Keys.ElementAt(0).paused = false;
}
}else if (coroutines.ContainsKey(timerKey))
{
timerKey.paused = false;
}
}
public void PauseTimer(TimerKey timerKey = null)
{
if (timerKey == null)
{
if (coroutines.Count > 0)
{
coroutines.Keys.ElementAt(0).paused = true;
}
}else if (coroutines.ContainsKey(timerKey))
{
timerKey.paused = true;
}
}
public void StopTimer(TimerKey timerKey = null)
{
if (timerKey == null)
{
if (coroutines.Count > 0)
{
monoBehaviour.StopCoroutine(coroutines.Values.ElementAt(0));
}
}else if (coroutines.ContainsKey(timerKey))
{
if (coroutines[timerKey] != null)
{
monoBehaviour.StopCoroutine(coroutines[timerKey]);
coroutines.Remove(timerKey);
}
else
{
Debug.LogWarning($"NullReferenceException: routine is null");
}
}
}
public void ResetTimer(TimerKey timerKey = null)
{
if (coroutines.Count < 0) return;
if (timerKey == null)
{
if (coroutines.Count > 0)
{
monoBehaviour.StopCoroutine(coroutines.Values.ElementAt(0));
coroutines.Remove(coroutines.Keys.ElementAt(0));
}
}else if (coroutines.ContainsKey(timerKey))
{
monoBehaviour.StopCoroutine(coroutines[timerKey]);
coroutines.Remove(timerKey);
}
TimerKey currentTimerKey = timerKey ?? coroutines.Keys.ElementAt(0);
currentTimerKey.paused = true;
currentTimerKey.elapsed = 0.0f;
currentTimerKey.action?.Invoke(0.0f);
Coroutine coroutine = monoBehaviour.StartCoroutine(TimerRunner(() => currentTimerKey.time, () => currentTimerKey.paused, currentTimerKey.action));
coroutines.Add(currentTimerKey, coroutine);
}
public void StopAllTimers()
{
foreach (var coroutine in coroutines)
{
monoBehaviour.StopCoroutine(coroutine.Value);
}
}
private IEnumerator TimerRunner(Func<float> seconds, Func<bool> shouldRunFunc, UnityAction<float> action)
{
float time = seconds?.Invoke() ?? 0.0f;
if (seconds?.Invoke() > 0)
{
while (time > 0)
{
bool pause = shouldRunFunc?.Invoke() ?? true;
if (!pause)
{
time -= Time.deltaTime;
action?.Invoke(time);
}
yield return new WaitForEndOfFrame();
}
}
else
{
time = 0f;
action?.Invoke(time);
}
}
}
[Serializable]
public class TimerKey
{
public bool paused;
public float elapsed;
public float time;
public UnityAction<float> action;
public TimerKey()
{
}
}
}