knucklebones/coroutines.lua
2023-08-12 11:36:14 +02:00

49 lines
907 B
Lua

--
-- coroutines manager
--
local coroutines = {}
function addcoroutine(fn)
local c = coroutine.create(fn)
table.insert(coroutines, c)
return c
end
function startcoroutine(c)
coroutine.resume(c)
end
function stopcoroutine(c)
for k, v in ipairs(coroutines) do
if c == v then
table.remove(coroutines, k)
break
end
end
end
function _coresolve()
for k, co in ipairs(coroutines) do
if coroutine.status(co) ~= "dead" then
local _, msg = coroutine.resume(co)
assert(msg == nil, msg)
else
stopcoroutine(co)
end
end
end
--- @param frames number
function waitframes(frames)
local frame = 0
while frame < frames do
frame = frame + 1
coroutine.yield()
end
end
--- @param seconds number
function waitsecs(seconds)
return waitframes(seconds * 60)
end