-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cs
216 lines (192 loc) · 6.72 KB
/
Timer.cs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace WESL
{
/// <summary>
/// Timer class extending Unity's built in time class to time set durations and indefinitely time
/// </summary>
public class Timer : ITimer, IEquatable<Timer>, IComparable<Timer>
{
private float _startTime, _duration = 0;
/// <summary>
/// The time that the timer started at.
/// </summary>
public float startTime => (_duration >= 0) ? _startTime : _startTime + _duration;
/// <summary>
/// Duration of the timer.
/// </summary>
public float duration => Mathf.Abs(_duration);
/// <summary>
/// The time that the timer will end.
/// </summary>
public float endTime => (_duration >= 0) ? _startTime + _duration : _startTime;
public override string ToString() => GetTime().ToString();
public virtual float getGlobalTime() => Time.time;
/// <summary>
/// Create a Timer that will count indefinitely.
/// </summary>
public Timer ()
{
_startTime = getGlobalTime();
}
/// <summary>
/// Create a Timer set to time to the duration.
/// </summary>
/// <param name="duration">Timer will time to this duration: from 0, to infinity.</param>
public Timer (float duration)
{
_startTime = getGlobalTime();
_duration = duration;
}
/// <summary>
/// Returns the time elapsed since the Timer was started.
/// </summary>
/// <returns>Time elapsed since the Timer was started.</returns>
public float GetTime ()
{
return (_duration != 0) ? Mathf.Clamp(Mathf.Abs(getGlobalTime() - _startTime), 0, duration) : Mathf.Abs(getGlobalTime() - _startTime);
}
/// <summary>
/// Returns the percentage of the duration elapsed since the Timer was started.
/// </summary>
/// <returns>Percentage of the duration elapsed since the Timer was started.</returns>
public float GetPercentage()
{
return (_duration != 0) ? Mathf.Clamp(Mathf.Abs((getGlobalTime() - _startTime) / duration), 0, 1) : 0;
}
/// <summary>
/// Returns the start time of the timer.
/// </summary>
/// <returns>Start time of timer.</returns>
public float GetStartTime()
{
return _startTime;
}
/// <summary>
/// Returns the duration of the timer.
/// </summary>
/// <returns>Duration of the timer.</returns>
public float GetDuration()
{
return _duration;
}
/// <summary>
/// Returns a boolean indicating if the Timer is done.
/// </summary>
/// <returns>A boolean indicating if the Timer is done.</returns>
public bool IsDone() {
if (_duration > 0)
{
return getGlobalTime() - _startTime >= _duration;
} else if (_duration < 0)
{
return getGlobalTime() - _startTime >= 0;
} else
{
return false;
}
}
/// <summary>
/// Returns a boolean indicating if the Timer is reversed.
/// </summary>
/// <returns>A boolean indicating if the Timer is reversed.</returns>
public bool IsReversed()
{
if (_duration >= 0) return false; else return true;
}
/// <summary>
/// Resets the Timer so that the elapsed time is 0.
/// </summary>
public void Reset()
{
if (_duration >= 0)
_startTime = getGlobalTime();
else
_startTime = getGlobalTime() + duration;
}
/// <summary>
/// Resets the Timer so that the elapsed time is 0 with a new duration.
/// </summary>
/// <param name="duration">Set Timer duration: from 0, to infinity.</param>
public void Reset (float duration)
{
if (_duration >= 0)
{
_startTime = getGlobalTime();
_duration = duration;
} else if (_duration < 0)
{
_startTime = getGlobalTime() + duration;
_duration = -duration;
}
}
/// <summary>
/// Set the time of the Timer.
/// </summary>
/// <param name="time">From 0, to duration.</param>
public void SetTime(float time)
{
time = Mathf.Clamp(time, 0, duration);
if (_duration >= 0) _startTime = getGlobalTime() - time;
else _startTime = getGlobalTime() + time;
}
/// <summary>
/// Set the percentage of the Timer.
/// </summary>
/// <param name="percentage">From 0, to 1.</param>
public void SetPercentage (float percentage)
{
if (_duration != 0) _startTime = getGlobalTime() - Mathf.Clamp(percentage, 0, 1) * _duration;
}
/// <summary>
/// Reverse the direction of the timer with the same duration.
/// </summary>
public void Reverse ()
{
if (_duration > 0) _startTime = getGlobalTime() + _duration;
else _startTime = getGlobalTime();
_duration *= -1;
}
/// <summary>
/// Reverse the direction of the timer and set the duration.
/// </summary>
/// <param name="duration">Set the duration of the Timer: from 0, to infinity</param>
public void Reverse(float duration)
{
if (_duration >= 0)
{
_startTime = getGlobalTime() + duration;
_duration = -duration;
}
else
{
_startTime = getGlobalTime();
_duration = duration;
}
}
public override bool Equals(object obj)
{
return Equals(obj as Timer);
}
public bool Equals(Timer other)
{
return other != null &&
_startTime == other._startTime &&
_duration == other._duration;
}
public int CompareTo(Timer other)
{
return (_duration >= other._duration) ? ((_duration == other._duration) ? ((_startTime > other.startTime) ? 1 : -1) : 1 ) : -1;
}
public static bool operator ==(Timer timer1, Timer timer2)
{
return EqualityComparer<Timer>.Default.Equals(timer1, timer2);
}
public static bool operator !=(Timer timer1, Timer timer2)
{
return !(timer1 == timer2);
}
}
}