aboutsummaryrefslogtreecommitdiff
path: root/bin/timer/timer.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-12 19:52:54 +0800
committerchai <chaifix@163.com>2018-08-12 19:52:54 +0800
commit7b34bd98bb00796febd5351b9d2e75fd2c247432 (patch)
treedd2da6fa01094f864d8deb358d7f9a8fe1b32b1c /bin/timer/timer.lua
parent5fe41eca99adf4bf0fb5832033a96f98b530d4f1 (diff)
*update
Diffstat (limited to 'bin/timer/timer.lua')
-rw-r--r--bin/timer/timer.lua78
1 files changed, 0 insertions, 78 deletions
diff --git a/bin/timer/timer.lua b/bin/timer/timer.lua
deleted file mode 100644
index 398f47f..0000000
--- a/bin/timer/timer.lua
+++ /dev/null
@@ -1,78 +0,0 @@
-local Timer = {}
-local MODE = {
- NONE = 0,
- EVERY = 1,
- REPEATS = 2,
- AFTER = 3,
-}
-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
- 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 t = timer:new(MODE.EVERY, sec, nil, callback)
- table.insert(Timer.timers, t)
-end
-
-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 t = timer:new(MODE.AFTER, sec, nil, callback)
- table.insert(Timer.timers, t)
-end
-
-return Timer \ No newline at end of file