Files
UnrealLuaDemo/Script/lua/Tests/Gameplay/TimerScheduler.lua
T

158 lines
4.8 KiB
Lua
Raw Normal View History

local ____exports = __module.env
____exports.TimerScheduler = __TS__DefineClass("TimerScheduler", __module)
local TimerScheduler = ____exports.TimerScheduler
TimerScheduler.name = "TimerScheduler"
function TimerScheduler.prototype.____constructor(self)
self.nextId = 1
self.timers = {}
self.timeScale = 1
end
function TimerScheduler.prototype.ScheduleOnce(self, delay, callback)
return self:AddTimer(delay, 1, callback)
end
function TimerScheduler.prototype.ScheduleRepeat(self, interval, callback, maxRepeats)
if maxRepeats == nil then
maxRepeats = 0
end
return self:AddTimer(interval, maxRepeats, callback)
end
function TimerScheduler.prototype.AddTimer(self, interval, maxRepeats, callback)
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 safeInterval = math.max(0.0001, interval)
self.timers[#self.timers + 1] = {
id = id,
interval = safeInterval,
remaining = safeInterval,
maxRepeats = maxRepeats,
firedCount = 0,
paused = false,
cancelled = false,
callback = callback
}
return id
end
function TimerScheduler.prototype.Cancel(self, handleId)
do
local i = 0
while i < #self.timers do
if self.timers[i + 1].id == handleId and not self.timers[i + 1].cancelled then
self.timers[i + 1].cancelled = true
return true
end
i = i + 1
end
end
return false
end
function TimerScheduler.prototype.SetPaused(self, handleId, paused)
do
local i = 0
while i < #self.timers do
if self.timers[i + 1].id == handleId then
self.timers[i + 1].paused = paused
return
end
i = i + 1
end
end
end
function TimerScheduler.prototype.SetTimeScale(self, scale)
self.timeScale = math.max(0, scale)
end
function TimerScheduler.prototype.Tick(self, dt)
local scaledDt = dt * self.timeScale
if scaledDt <= 0 then
return
end
local snapshot = {}
do
local i = 0
while i < #self.timers do
snapshot[i + 1] = self.timers[i + 1]
i = i + 1
end
end
do
local i = 0
while i < #snapshot do
do
local t = snapshot[i + 1]
if t.cancelled or t.paused then
goto __continue20
end
t.remaining = t.remaining - scaledDt
while not t.cancelled and not t.paused and t.remaining <= 0 do
t.firedCount = t.firedCount + 1
t.callback(t.id)
if t.maxRepeats > 0 and t.firedCount >= t.maxRepeats then
t.cancelled = true
break
end
t.remaining = t.remaining + t.interval
end
end
::__continue20::
i = i + 1
end
end
do
local i = #self.timers - 1
while i >= 0 do
if self.timers[i + 1].cancelled then
table.remove(self.timers, i + 1)
end
i = i - 1
end
end
end
function TimerScheduler.prototype.GetActiveCount(self)
local count = 0
do
local i = 0
while i < #self.timers do
if not self.timers[i + 1].cancelled then
count = count + 1
end
i = i + 1
end
end
return count
end
____exports.CooldownTracker = __TS__DefineClass("CooldownTracker", __module)
local CooldownTracker = ____exports.CooldownTracker
CooldownTracker.name = "CooldownTracker"
function CooldownTracker.prototype.____constructor(self)
self.remainingMap = {}
end
function CooldownTracker.prototype.StartCooldown(self, skillId, duration)
if duration > 0 then
self.remainingMap[skillId] = duration
end
end
function CooldownTracker.prototype.IsReady(self, skillId)
local rem = self.remainingMap[skillId]
return rem == nil or rem <= 0
end
function CooldownTracker.prototype.GetRemaining(self, skillId)
return self.remainingMap[skillId] or 0
end
function CooldownTracker.prototype.ReduceCooldown(self, skillId, seconds)
local rem = self.remainingMap[skillId]
if rem ~= nil then
local next = rem - seconds
self.remainingMap[skillId] = next > 0 and next or nil
end
end
function CooldownTracker.prototype.Tick(self, dt)
for skillId, rem in pairs(self.remainingMap) do
if rem ~= nil then
local next = rem - dt
self.remainingMap[skillId] = next > 0 and next or nil
end
end
end
function __module.UpdateImports()
end