-
Notifications
You must be signed in to change notification settings - Fork 4
/
EnhancedTimer.lua
75 lines (63 loc) · 1.87 KB
/
EnhancedTimer.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
local EnhancedTimer = Object:extend()
local Timer = require 'libraries/hump/timer'
function EnhancedTimer:new()
self.timer = Timer()
self.tags = {}
end
function EnhancedTimer:update(dt)
if self.timer then self.timer:update(dt) end
end
function EnhancedTimer:after(tag, duration, func)
if type(tag) == 'string' then
self:cancel(tag)
self.tags[tag] = self.timer:after(duration, func)
return self.tags[tag]
else
return self.timer:after(tag, duration, func)
end
end
function EnhancedTimer:during(tag, duration, func, after)
if type(tag) == 'string' then
self:cancel(tag)
self.tags[tag] = self.timer:during(duration, func, after)
return self.tags[tag]
else
return self.timer:during(tag, duration, func, after)
end
end
function EnhancedTimer:every(tag, duration, func, count)
if type(tag) == 'string' then
self:cancel(tag)
self.tags[tag] = self.timer:every(duration, func, count)
return self.tags[tag]
else
return self.timer:every(tag, duration, func, count)
end
end
function EnhancedTimer:tween(tag, duration, table, tween_table, tween_function, after)
if type(tag) == 'string' then
self:cancel(tag)
self.tags[tag] = self.timer:tween(duration, table, tween_table, tween_function, after)
return self.tags[tag]
else
return self.timer:tween(tag, duration, table, tween_table, tween_function, after)
end
end
function EnhancedTimer:cancel(tag)
if tag then
if self.tags[tag] then
self.timer:cancel(self.tags[tag])
self.tags[tag] = nil
else self.timer:cancel(tag) end
end
end
function EnhancedTimer:clear()
self.timer:clear()
self.tags = {}
end
function EnhancedTimer:destroy()
self.timer:clear()
self.tags = {}
self.timer = nil
end
return EnhancedTimer