mirror of
https://github.com/vrld/hump.git
synced 2024-10-15 09:04:18 +00:00
This commit is contained in:
parent
dc589adc32
commit
d77e0a2324
56
timer.lua
56
timer.lua
@ -31,23 +31,41 @@ local function _nothing_() end
|
|||||||
|
|
||||||
function Timer:update(dt)
|
function Timer:update(dt)
|
||||||
local to_remove = {}
|
local to_remove = {}
|
||||||
for handle, delay in pairs(self.functions) do
|
|
||||||
delay = delay - dt
|
for handle in pairs(self.functions) do
|
||||||
if delay <= 0 then
|
-- handle: {
|
||||||
to_remove[#to_remove+1] = handle
|
-- time = <number>,
|
||||||
|
-- after = <function>,
|
||||||
|
-- during = <function>,
|
||||||
|
-- limit = <number>,
|
||||||
|
-- count = <number>,
|
||||||
|
-- }
|
||||||
|
|
||||||
|
handle.time = handle.time + dt
|
||||||
|
handle.during(dt, handle.limit - handle.time)
|
||||||
|
|
||||||
|
while handle.time >= handle.limit and handle.count > 0 do
|
||||||
|
if handle.after(handle.after) == false then
|
||||||
|
handle.count = 0
|
||||||
|
break
|
||||||
|
end
|
||||||
|
handle.time = handle.time - handle.limit
|
||||||
|
handle.count = handle.count - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if handle.count == 0 then
|
||||||
|
table.insert(to_remove, handle)
|
||||||
end
|
end
|
||||||
self.functions[handle] = delay
|
|
||||||
handle.func(dt, delay)
|
|
||||||
end
|
end
|
||||||
for _,handle in ipairs(to_remove) do
|
|
||||||
self.functions[handle] = nil
|
for i = 1, #to_remove do
|
||||||
handle.after(handle.after)
|
self.functions[to_remove[i]] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Timer:during(delay, func, after)
|
function Timer:during(delay, during, after)
|
||||||
local handle = {func = func, after = after or _nothing_}
|
local handle = { time = 0, during = during, after = after, limit = delay, count = 1 }
|
||||||
self.functions[handle] = delay
|
self.functions[handle] = true
|
||||||
return handle
|
return handle
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -55,16 +73,10 @@ function Timer:after(delay, func)
|
|||||||
return self:during(delay, _nothing_, func)
|
return self:during(delay, _nothing_, func)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Timer:every(delay, func, count)
|
function Timer:every(delay, after, count)
|
||||||
local count, handle = count or math.huge -- exploit below: math.huge - 1 = math.huge
|
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
|
||||||
|
local handle = { time = 0, during = _nothing_, after = after, limit = delay, count = count }
|
||||||
handle = self:after(delay, function(f)
|
self.functions[handle] = true
|
||||||
if func(func) == false then return end
|
|
||||||
count = count - 1
|
|
||||||
if count > 0 then
|
|
||||||
self.functions[handle] = delay
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
return handle
|
return handle
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user