scheduler
This commit is contained in:
parent
34fac4aa99
commit
dfb89ab405
|
@ -1,4 +1,5 @@
|
|||
require('input')
|
||||
require('scheduler')
|
||||
bitop = require('bitop') -- TODO: tons of this is commented out because of '...'. FIX. IT.
|
||||
require('rng')
|
||||
require('easable')
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
local scheduled = {
|
||||
}
|
||||
local scheduledTicks = {
|
||||
}
|
||||
|
||||
local function getHighestKey(t)
|
||||
local s = 0
|
||||
for k in pairs(t) do
|
||||
s = math.max(s, k)
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
function schedule(when, func)
|
||||
local index = getHighestKey(scheduled) + 1
|
||||
scheduled[index] = {when, func}
|
||||
return index
|
||||
end
|
||||
|
||||
function scheduleInTicks(when, func)
|
||||
local index = getHighestKey(scheduledTicks) + 1
|
||||
scheduledTicks[index] = {when, func}
|
||||
return index
|
||||
end
|
||||
|
||||
function unschedule(i)
|
||||
if not i then
|
||||
print('warning: trying to unschedule a non-existent event')
|
||||
return
|
||||
end
|
||||
scheduled[i] = nil
|
||||
end
|
||||
|
||||
function unscheduleInTicks(i)
|
||||
if not i then
|
||||
print('warning: trying to unschedule a non-existent event')
|
||||
end
|
||||
scheduledTicks[i] = nil
|
||||
end
|
||||
|
||||
function uranium.update(dt)
|
||||
for k, s in pairs(scheduledTicks) do
|
||||
s[1] = s[1] - 1
|
||||
if s[1] <= 0 then
|
||||
s[2]()
|
||||
scheduledTicks[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
for k, s in pairs(scheduled) do
|
||||
s[1] = s[1] - dt
|
||||
if s[1] <= 0 then
|
||||
s[2]()
|
||||
scheduled[k] = nil
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue