-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerClass.lua
161 lines (150 loc) · 3.89 KB
/
TimerClass.lua
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
-- v012621_0
local Heartbeat = game:GetService("RunService").Heartbeat
local timerClass = {}
timerClass.__index = timerClass
-- Constructor
function timerClass.new(startTime)
local timer = {
_startTime = type(startTime)=="number" and startTime or 60,
_compensation = 0,
_running = false,
_loopEnabled = false,
_InternalStop = Instance.new("BindableEvent"),
_Started = Instance.new("BindableEvent"),
_Paused = Instance.new("BindableEvent"),
_Stopped = Instance.new("BindableEvent"),
_Ended = Instance.new("BindableEvent"),
_WasSet = Instance.new("BindableEvent"),
}
timer._timeLeft = timer._startTime
timer.Started = timer._Started.Event
timer.Paused = timer._Paused.Event
timer.Stopped = timer._Stopped.Event
timer.Ended = timer._Ended.Event
timer.WasSet = timer._WasSet.Event
setmetatable(timer,timerClass)
return timer
end
-- Methods
function timerClass:Start(compensationMode)
if not self:IsRunning() then
local TimerLoop, InternalStop, StopFlag, LastTick
if compensationMode then
self._timeLeft = self._timeLeft - self._compensation
end
InternalStop = self._InternalStop.Event:Connect(function(StopTag)
StopFlag = StopTag
end)
TimerLoop = Heartbeat:Connect(function()
local DeltaTime = os.clock()-LastTick
LastTick = os.clock()
if not StopFlag then
self._timeLeft = self._timeLeft-DeltaTime
end
local TimedOut = self._timeLeft <= 0
if TimedOut or StopFlag then
self._running = false
InternalStop:Disconnect()
TimerLoop:Disconnect()
if (StopFlag == "End") or TimedOut then
if TimedOut then
self._compensation = math.abs(self._timeLeft)
end
self._timeLeft = self._startTime
self._Ended:Fire()
if self._loopEnabled and TimedOut then
while (self._compensation >= self._startTime) and self._loopEnabled do
self._Started:Fire()
self._Ended:Fire()
self._compensation -= self._startTime
end
if self._loopEnabled then
self:Start(true)
end
end
elseif StopFlag == "Pause" then
self._Paused:Fire()
elseif StopFlag == "Stop" then
self._timeLeft = self._startTime
self._Stopped:Fire()
end
end
end)
self._running = true
LastTick = os.clock()
self._Started:Fire()
end
end
function timerClass:Pause()
if self:IsRunning() then
self._InternalStop:Fire("Pause")
end
end
function timerClass:Stop()
if self:IsRunning() then
self._InternalStop:Fire("Stop")
elseif self._timeLeft < self._startTime then
self._timeLeft = self._startTime
self._Stopped:Fire()
end
end
function timerClass:End()
if self:IsRunning() then
self._InternalStop:Fire("End")
elseif self._timeLeft < self._startTime then
self._timeLeft = self._startTime
self._Ended:Fire()
end
end
function timerClass:Set(startTime,noReset)
self._startTime = type(startTime)=="number" and startTime or self._startTime
if noReset then
self._timeLeft = math.min(self._timeLeft,self._startTime)
else
self._timeLeft = self._startTime
end
self._WasSet:Fire()
end
function timerClass:EnableLooping()
self._loopEnabled = true
end
function timerClass:DisableLooping()
self._loopEnabled = false
end
function timerClass:GetTimeLeft()
return self._timeLeft
end
function timerClass:GetStartTime()
return self._startTime
end
function timerClass:IsRunning()
return self._running
end
-- Other functions
function timerClass.Schedule(startTime,callback,loop)
if not (type(callback) == "function") then
return
end
local timer = timerClass.new(startTime)
local EndEvent
if loop then
EndEvent = timer.Ended:Connect(function()
callback()
timer:Start(true)
end)
else
EndEvent = timer.Ended:Connect(callback)
end
timer:Start()
return EndEvent, timer
end
function timerClass.Wait(n)
local initialTick = os.clock()
local currentTick
repeat
Heartbeat:Wait()
currentTick = os.clock()
until currentTick-initialTick >= (n or 0)
return currentTick-initialTick
end
return timerClass