summaryrefslogtreecommitdiff
path: root/timer/timer.lua
diff options
context:
space:
mode:
Diffstat (limited to 'timer/timer.lua')
-rw-r--r--timer/timer.lua104
1 files changed, 61 insertions, 43 deletions
diff --git a/timer/timer.lua b/timer/timer.lua
index 3566709..398f47f 100644
--- a/timer/timer.lua
+++ b/timer/timer.lua
@@ -1,60 +1,78 @@
-local timer = {}
+local Timer = {}
local MODE = {
+ NONE = 0,
EVERY = 1,
REPEATS = 2,
AFTER = 3,
}
-local timers = {}
-
-timer.update = function(sec)
- for i, pack in ipairs(timers) do
- pack.count = pack.count + sec
- if pack.count >= pack.time then
- if pack.mode == MODE.EVERY then
- pack.count = 0
- pack.callback()
- elseif pack.mode == MODE.REPEATS then
- pack.count = 0
- pack.callback()
- pack.repeats = pack.repeats + 1
- if pack.repeats >= pack.rpt then
- table.remove(timers, i)
+Timer.timers = {}
+local timer = {
+ new = function(self, _mod, _duration, _count, _callback)
+ local t = {}
+ setmetatable(t, self)
+ self.__index = self
+ t:_init(_mod, _duration, _count, _callback)
+ return t
+ end,
+ _init = function(self, _mod, _duration, _count, _callback)
+ self.tick = 0
+ self.duration = _duration
+ self.mode = _mod
+ self.count = _count
+ self.callback = _callback
+ end,
+ update = function(self, dt)
+ self.tick = self.tick + dt
+ if self.tick >= self.duration then
+ if self.mode == MODE.EVERY then
+ self.tick = 0
+ elseif self.mode == MODE.REPEATS then
+ self.tick = 0
+ self.count = self.count - 1
+ if self.count <= 0 then
+ -- remove this
+ for i, v in ipairs(Timer.timers) do
+ if v == self then
+ table.remove(Timer.timers, i)
+ break
+ end
+ end
+ end
+ elseif self.mode == MODE.AFTER then
+ -- remove this
+ for i, v in ipairs(Timer.timers) do
+ if v == self then
+ table.remove(Timer.timers, i)
+ break
+ end
end
- elseif pack.mode == MODE.AFTER then
- pack.callback()
- table.remove(timers, i)
+ end
+ if self.callback then
+ self.callback()
end
end
end
+}
+
+Timer.update = function(sec)
+ for _, t in ipairs(Timer.timers) do
+ t:update(sec)
+ end
end
-timer.every = function(sec, callback)
- local pack = {}
- pack.mode = MODE.EVERY
- pack.time = sec
- pack.callback = callback
- pack.count = 0
- table.insert(timers, pack)
+Timer.every = function(sec, callback)
+ local t = timer:new(MODE.EVERY, sec, nil, callback)
+ table.insert(Timer.timers, t)
end
-timer.repeats = function(sec, rpt, callback)
- local pack = {}
- pack.mode = MODE.REPEATS
- pack.time = sec
- pack.callback = callback
- pack.count = 0
- pack.repeats = 0
- pack.rpt = rpt
- table.insert(timers, pack)
+Timer.repeats = function(sec, rpt, callback)
+ local t = timer:new(MODE.REPEATS, sec, rpt, callback)
+ table.insert(Timer.timers, t)
end
-timer.after = function(sec, callback)
- local pack = {}
- pack.mode = MODE.AFTER
- pack.time = sec
- pack.callback = callback
- pack.count = 0
- table.insert(timers, pack)
+Timer.after = function(sec, callback)
+ local t = timer:new(MODE.AFTER, sec, nil, callback)
+ table.insert(Timer.timers, t)
end
-return timer
+return Timer \ No newline at end of file