-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.lua
More file actions
67 lines (58 loc) · 1.41 KB
/
timer.lua
File metadata and controls
67 lines (58 loc) · 1.41 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
-----------------------------
-- Levak ©2014 --------------
-- http://levak.free.fr/ ----
-- levak92@gmail.com --------
-----------------------------
---------- AnimIt : Timer extension
-- Bootstrap of timer.start
local _tstart = timer.start
function timer.start(ms)
if not timer.isRunning then
_tstart(ms)
end
timer.isRunning = true
end
-- Bootstrap of timer.stop
local _tstop = timer.stop
function timer.stop()
timer.isRunning = false
_tstop()
end
-- Function to call in on.timer
function timer.update(no_stop)
local j = 1
while j <= #timer.tasks do -- for each task
if timer.tasks[j][2]() then -- delete it if has ended
table.remove(timer.tasks, j)
else
j = j + 1
end
end
if not no_stop and #timer.tasks <= 0 then
timer.stop()
end
end
---------------------
----- Internals -----
---------------------
if platform.hw then
timer.multiplier = platform.hw() < 4 and 4 or 1
else
timer.multiplier = 4
end
timer.tasks = {}
function timer.addTask(object, queueID, task)
table.insert(timer.tasks, {object, task, queueID})
end
function timer.purgeTasks(object, queueID, complete)
local j = 1
while j <= #timer.tasks do
if timer.tasks[j][1] == object
and (not queueID or timer.tasks[j][3] == queueID) then
timer.tasks[j][2](complete)
table.remove(timer.tasks, j)
else
j = j + 1
end
end
end