pico8-0hh1/coroutines.lua

34 lines
511 B
Lua
Raw Normal View History

2022-06-02 17:55:52 +02:00
local coroutines={}
2022-06-02 22:39:23 +02:00
-- starts a coroutine and returns its key
function startcoroutine(fn, key)
add(coroutines, fn)
2022-06-02 17:55:52 +02:00
end
2022-06-02 22:39:23 +02:00
function stepcoroutine(fn)
coresume(fn)
2022-06-02 17:55:52 +02:00
end
2022-06-02 22:39:23 +02:00
function stopcoroutine(fn)
del(coroutines, fn)
2022-06-02 17:55:52 +02:00
end
function _coresolve()
2022-06-02 22:39:23 +02:00
for co in all(coroutines) do
2022-06-02 17:55:52 +02:00
if costatus(co)!='dead' then
2022-06-02 22:39:23 +02:00
assert(coresume(co))
2022-06-02 17:55:52 +02:00
else
2022-06-02 22:39:23 +02:00
stopcoroutine(co)
2022-06-02 17:55:52 +02:00
end
end
end
function wait(seconds)
wait_frames(seconds*60)
end
function wait_frames(frames)
for i=1,frames do
yield()
end
end