Files
Techmino/Zframework/task.lua
MrZ626 391821bf16 Zframework的控件/场景/文本/任务模块支持基于时间更新
修改几处其他地方基于时间update
2021-09-25 02:59:38 +08:00

57 lines
1.2 KiB
Lua

local rem=table.remove
local assert,resume,status=assert,coroutine.resume,coroutine.status
local tasks={}
local TASK={}
function TASK.getCount()
return #tasks
end
local trigTime=0
function TASK.update(dt)
trigTime=trigTime+dt
while trigTime>1/60 do
for i=#tasks,1,-1 do
local T=tasks[i]
if status(T.thread)=='dead'then
rem(tasks,i)
else
assert(resume(T.thread))
end
end
trigTime=trigTime-1/60
end
end
function TASK.new(code,...)
local thread=coroutine.create(code)
resume(thread,...)
if status(thread)~='dead'then
tasks[#tasks+1]={
thread=thread,
code=code,
args={...},
}
end
end
function TASK.removeTask_code(code)
for i=#tasks,1,-1 do
if tasks[i].code==code then
rem(tasks,i)
end
end
end
function TASK.removeTask_iterate(func,...)
for i=#tasks,1,-1 do
if func(tasks[i],...)then
rem(tasks,i)
end
end
end
function TASK.clear()
local i=#tasks
while i>0 do
tasks[i]=nil
i=i-1
end
end
return TASK