-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.s3d
92 lines (79 loc) · 1.55 KB
/
Timer.s3d
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
#ifndef _TIMER
#define _TIMER
class Timer
{
var currentTime;
var timers;
var size;
var tps;
SetTimer(name, delta);
CheckTimer(name);
Timeout(name);
Del(name);
ClearAll();
Update();
};
function Timer::Timer(t, s)
{
size = s;
timers = Array(size);
tps = t;
currentTime = 0;
}
function Timer::SetTimer(name, delta)
{
var found = false;
var firstVoid = -1;
for(var i = 0; i < size && !found; i++)
{
if(timers[i] == Void && firstVoid == -1)
firstVoid = i;
else if(timers[i] != Void && timers[i][0] == name)
{
found = true;
timers[i][1] = currentTime;
}
}
if(!found)
timers[firstVoid] = {name, currentTime, delta};
}
function Timer::CheckTimer(name)
{
for(var i = 0; i < size; i++)
{
if(timers[i] != Void && timers[i][0] == name)
return (currentTime - timers[i][1]) / tps;
}
return 0;
}
//WARNING: do not call on non-existent timer
function Timer::Timeout(name)
{
for(var i = 0; i < size; i++)
{
if(timers[i] != Void && timers[i][0] == name)
return (currentTime - timers[i][1]) / tps >= timers[i][2];
}
return false;
}
function Timer::Del(name)
{
for(var i = 0; i < size; i++)
{
if(timers[i] != Void && timers[i][0] == name)
{
timers[i] = Void;
break;
}
}
}
function Timer::ClearAll()
{
timers = Array(size);
currentTime = 0;
}
function Timer::Update()
{
currentTime++;
}
#endif