mirror of
https://github.com/vrld/hump.git
synced 2024-11-23 12:24:19 +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)
|
||||
local to_remove = {}
|
||||
for handle, delay in pairs(self.functions) do
|
||||
delay = delay - dt
|
||||
if delay <= 0 then
|
||||
to_remove[#to_remove+1] = handle
|
||||
|
||||
for handle in pairs(self.functions) do
|
||||
-- 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
|
||||
self.functions[handle] = delay
|
||||
handle.func(dt, delay)
|
||||
end
|
||||
for _,handle in ipairs(to_remove) do
|
||||
self.functions[handle] = nil
|
||||
handle.after(handle.after)
|
||||
|
||||
for i = 1, #to_remove do
|
||||
self.functions[to_remove[i]] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Timer:during(delay, func, after)
|
||||
local handle = {func = func, after = after or _nothing_}
|
||||
self.functions[handle] = delay
|
||||
function Timer:during(delay, during, after)
|
||||
local handle = { time = 0, during = during, after = after, limit = delay, count = 1 }
|
||||
self.functions[handle] = true
|
||||
return handle
|
||||
end
|
||||
|
||||
@ -55,16 +73,10 @@ function Timer:after(delay, func)
|
||||
return self:during(delay, _nothing_, func)
|
||||
end
|
||||
|
||||
function Timer:every(delay, func, count)
|
||||
local count, handle = count or math.huge -- exploit below: math.huge - 1 = math.huge
|
||||
|
||||
handle = self:after(delay, function(f)
|
||||
if func(func) == false then return end
|
||||
count = count - 1
|
||||
if count > 0 then
|
||||
self.functions[handle] = delay
|
||||
end
|
||||
end)
|
||||
function Timer:every(delay, after, count)
|
||||
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 }
|
||||
self.functions[handle] = true
|
||||
return handle
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user