-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScratchyTimer.cs
88 lines (78 loc) · 2.25 KB
/
ScratchyTimer.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
using UnityEngine;
using System.Collections;
public delegate void TimerCallback();
public class ScratchyTimer : ScratchyPromise
{
public bool Repeat = false;
public float Elapsed = 0;
public float Duration = 0;
public int Count = 0;
public int CountLimit = -1;
private TimerCallback callback;
public ScratchyTimer()
{
this.Active = false;
}
public ScratchyTimer(float duration, bool repeat, TimerCallback callback)
{
this.Init(duration, repeat, callback);
}
public void Init(float duration, bool repeat, TimerCallback callback)
{
this.Active = true;
this.Duration = duration;
this.Repeat = repeat;
this.callback = callback;
this.Elapsed = 0;
}
public void Update(float deltaTime)
{
if (Active == true && Duration > 0)
{
Elapsed += deltaTime;
if (Elapsed >= Duration)
{
bool skip = false;
if (this.whileTest != null)
{
this.Active = this.whileTest();
}
if (this.whenTest != null)
{
if (this.whenTest() == false)
{
skip = true;
}
}
if (this.Active && !skip)
{
callback();
if (this.CountLimit > -1)
{
this.Count++;
if (this.Count >= CountLimit)
{
this.Repeat = false;
this.Active = false;
}
}
}
if (Repeat)
{
while (this.Elapsed > this.Duration)
{
this.Elapsed -= this.Duration;
}
}
else
{
this.Active = false;
if (this.thenFunction != null)
{
this.thenFunction();
}
}
}
}
}
}