129 lines
4.0 KiB
Lua
129 lines
4.0 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|
local __TS__Class = ____lualib.__TS__Class
|
|
local ____exports = {}
|
|
____exports.CoroutineTaskRunner = __TS__Class()
|
|
local CoroutineTaskRunner = ____exports.CoroutineTaskRunner
|
|
CoroutineTaskRunner.name = "CoroutineTaskRunner"
|
|
function CoroutineTaskRunner.prototype.____constructor(self)
|
|
self.nextId = 1
|
|
self.tasks = {}
|
|
end
|
|
function CoroutineTaskRunner.prototype.Start(self, body, owner)
|
|
local ____self_0, ____nextId_1 = self, "nextId"
|
|
local ____self_nextId_2 = ____self_0[____nextId_1]
|
|
____self_0[____nextId_1] = ____self_nextId_2 + 1
|
|
local id = ____self_nextId_2
|
|
local co = coroutine.create(body)
|
|
local task = {id = id, co = co, owner = owner, cancelled = false}
|
|
self.tasks[#self.tasks + 1] = task
|
|
self:StepTask(task)
|
|
return id
|
|
end
|
|
function CoroutineTaskRunner.prototype.Cancel(self, taskId)
|
|
do
|
|
local i = 0
|
|
while i < #self.tasks do
|
|
if self.tasks[i + 1].id == taskId and not self.tasks[i + 1].cancelled then
|
|
self.tasks[i + 1].cancelled = true
|
|
return true
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
function CoroutineTaskRunner.prototype.Tick(self, dt)
|
|
local snapshot = {}
|
|
do
|
|
local i = 0
|
|
while i < #self.tasks do
|
|
snapshot[i + 1] = self.tasks[i + 1]
|
|
i = i + 1
|
|
end
|
|
end
|
|
do
|
|
local i = 0
|
|
while i < #snapshot do
|
|
do
|
|
local task = snapshot[i + 1]
|
|
if task.cancelled or coroutine.status(task.co) == "dead" then
|
|
task.cancelled = true
|
|
goto __continue12
|
|
end
|
|
if task.owner ~= nil and not UE.UKismetSystemLibrary.IsValid(task.owner) then
|
|
task.cancelled = true
|
|
goto __continue12
|
|
end
|
|
local cmd = task.pending
|
|
if cmd == nil then
|
|
self:StepTask(task)
|
|
elseif cmd.kind == "wait_seconds" then
|
|
cmd.remaining = cmd.remaining - dt
|
|
if cmd.remaining <= 0 then
|
|
task.pending = nil
|
|
self:StepTask(task)
|
|
end
|
|
elseif cmd.kind == "wait_until" then
|
|
if cmd.predicate() then
|
|
task.pending = nil
|
|
self:StepTask(task)
|
|
end
|
|
end
|
|
end
|
|
::__continue12::
|
|
i = i + 1
|
|
end
|
|
end
|
|
do
|
|
local i = #self.tasks - 1
|
|
while i >= 0 do
|
|
local t = self.tasks[i + 1]
|
|
if t.cancelled or coroutine.status(t.co) == "dead" then
|
|
table.remove(self.tasks, i + 1)
|
|
end
|
|
i = i - 1
|
|
end
|
|
end
|
|
end
|
|
function CoroutineTaskRunner.prototype.GetActiveTaskCount(self)
|
|
local count = 0
|
|
do
|
|
local i = 0
|
|
while i < #self.tasks do
|
|
local t = self.tasks[i + 1]
|
|
if not t.cancelled and coroutine.status(t.co) ~= "dead" then
|
|
count = count + 1
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
return count
|
|
end
|
|
function CoroutineTaskRunner.prototype.StepTask(self, task)
|
|
if task.cancelled or coroutine.status(task.co) == "dead" then
|
|
return
|
|
end
|
|
local ok, yielded = coroutine.resume(task.co)
|
|
if not ok then
|
|
task.cancelled = true
|
|
error("CoroutineTaskRunner error: " .. tostring(yielded))
|
|
end
|
|
if coroutine.status(task.co) == "dead" then
|
|
task.cancelled = true
|
|
elseif yielded ~= nil then
|
|
task.pending = yielded
|
|
end
|
|
end
|
|
function CoroutineTaskRunner.WaitSeconds(seconds)
|
|
local cmd = {kind = "wait_seconds", remaining = seconds}
|
|
coroutine.yield(cmd)
|
|
end
|
|
function CoroutineTaskRunner.WaitUntil(predicate)
|
|
if predicate() then
|
|
return
|
|
end
|
|
local cmd = {kind = "wait_until", predicate = predicate}
|
|
coroutine.yield(cmd)
|
|
end
|
|
return ____exports
|