mirror of
https://github.com/vrld/hump.git
synced 2024-11-23 12:24:19 +00:00
Add individual timer support (as requested by mofr)
This commit is contained in:
parent
006f040a8b
commit
7aa3073037
54
timer.lua
54
timer.lua
@ -24,46 +24,52 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local functions = {}
|
local Timer = {}
|
||||||
local function update(dt)
|
Timer.__index = Timer
|
||||||
|
|
||||||
|
local function new()
|
||||||
|
return setmetatable({functions = {}}, Timer)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Timer:update(dt)
|
||||||
local to_remove = {}
|
local to_remove = {}
|
||||||
for func, delay in pairs(functions) do
|
for func, delay in pairs(self.functions) do
|
||||||
delay = delay - dt
|
delay = delay - dt
|
||||||
if delay <= 0 then
|
if delay <= 0 then
|
||||||
to_remove[#to_remove+1] = func
|
to_remove[#to_remove+1] = func
|
||||||
end
|
end
|
||||||
functions[func] = delay
|
self.functions[func] = delay
|
||||||
end
|
end
|
||||||
for _,func in ipairs(to_remove) do
|
for _,func in ipairs(to_remove) do
|
||||||
functions[func] = nil
|
self.functions[func] = nil
|
||||||
func(func)
|
func(func)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function add(delay, func)
|
function Timer:add(delay, func)
|
||||||
assert(not functions[func], "Function already scheduled to run.")
|
assert(not self.functions[func], "Function already scheduled to run.")
|
||||||
functions[func] = delay
|
self.functions[func] = delay
|
||||||
return func
|
return func
|
||||||
end
|
end
|
||||||
|
|
||||||
local function addPeriodic(delay, func, count)
|
function Timer:addPeriodic(delay, func, count)
|
||||||
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
|
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
|
||||||
|
|
||||||
return add(delay, function(f)
|
return self:add(delay, function(f)
|
||||||
if func(func) == false then return end
|
if func(func) == false then return end
|
||||||
count = count - 1
|
count = count - 1
|
||||||
if count > 0 then
|
if count > 0 then
|
||||||
add(delay, f)
|
self:add(delay, f)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function cancel(func)
|
function Timer:cancel(func)
|
||||||
functions[func] = nil
|
self.functions[func] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function clear()
|
function Timer:clear()
|
||||||
functions = {}
|
self.functions = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local function Interpolator(length, func)
|
local function Interpolator(length, func)
|
||||||
@ -82,13 +88,17 @@ local function Oscillator(length, func)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- default timer
|
||||||
|
local default = new()
|
||||||
|
|
||||||
-- the module
|
-- the module
|
||||||
return {
|
return setmetatable({
|
||||||
update = update,
|
new = new,
|
||||||
add = add,
|
update = function(...) return default:update(...) end,
|
||||||
addPeriodic = addPeriodic,
|
add = function(...) return default:add(...) end,
|
||||||
cancel = cancel,
|
addPeriodic = function(...) return default:addPeriodic(...) end,
|
||||||
clear = clear,
|
cancel = function(...) return default:cancel(...) end,
|
||||||
|
clear = function(...) return default:clear(...) end,
|
||||||
Interpolator = Interpolator,
|
Interpolator = Interpolator,
|
||||||
Oscillator = Oscillator
|
Oscillator = Oscillator
|
||||||
}
|
}, {__call = new})
|
||||||
|
Loading…
Reference in New Issue
Block a user