scheduler

This commit is contained in:
Jill 2022-09-19 21:40:08 +03:00
parent 34fac4aa99
commit dfb89ab405
2 changed files with 58 additions and 0 deletions

View File

@ -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')

57
stdlib/scheduler.lua Normal file
View File

@ -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