test: add comprehensive ts2lua-unreal TS/Lua 5.4 test suites and upgrade to v0.2.2
This commit is contained in:
1 parent
ee2e62bb34
commit
3dd61c32a9
75 files changed
+9929
-10
No files matched your search
@@ -0,0 +1,130 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.BTCondition = __TS__Class()
|
||||
local BTCondition = ____exports.BTCondition
|
||||
BTCondition.name = "BTCondition"
|
||||
function BTCondition.prototype.____constructor(self, check)
|
||||
self.check = check
|
||||
end
|
||||
function BTCondition.prototype.Tick(self, ctx)
|
||||
return self.check(ctx) and 0 or 1
|
||||
end
|
||||
function BTCondition.prototype.Reset(self)
|
||||
end
|
||||
____exports.BTAction = __TS__Class()
|
||||
local BTAction = ____exports.BTAction
|
||||
BTAction.name = "BTAction"
|
||||
function BTAction.prototype.____constructor(self, action)
|
||||
self.action = action
|
||||
end
|
||||
function BTAction.prototype.Tick(self, ctx)
|
||||
return self.action(ctx)
|
||||
end
|
||||
function BTAction.prototype.Reset(self)
|
||||
end
|
||||
____exports.BTSequence = __TS__Class()
|
||||
local BTSequence = ____exports.BTSequence
|
||||
BTSequence.name = "BTSequence"
|
||||
function BTSequence.prototype.____constructor(self, children)
|
||||
self.runningIndex = 0
|
||||
self.children = children
|
||||
end
|
||||
function BTSequence.prototype.Tick(self, ctx)
|
||||
while self.runningIndex < #self.children do
|
||||
local status = self.children[self.runningIndex + 1]:Tick(ctx)
|
||||
if status == 2 then
|
||||
return 2
|
||||
end
|
||||
if status == 1 then
|
||||
self:Reset()
|
||||
return 1
|
||||
end
|
||||
self.runningIndex = self.runningIndex + 1
|
||||
end
|
||||
self:Reset()
|
||||
return 0
|
||||
end
|
||||
function BTSequence.prototype.Reset(self)
|
||||
self.runningIndex = 0
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.children do
|
||||
self.children[i + 1]:Reset()
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
____exports.BTSelector = __TS__Class()
|
||||
local BTSelector = ____exports.BTSelector
|
||||
BTSelector.name = "BTSelector"
|
||||
function BTSelector.prototype.____constructor(self, children)
|
||||
self.runningIndex = 0
|
||||
self.children = children
|
||||
end
|
||||
function BTSelector.prototype.Tick(self, ctx)
|
||||
while self.runningIndex < #self.children do
|
||||
local status = self.children[self.runningIndex + 1]:Tick(ctx)
|
||||
if status == 2 then
|
||||
return 2
|
||||
end
|
||||
if status == 0 then
|
||||
self:Reset()
|
||||
return 0
|
||||
end
|
||||
self.runningIndex = self.runningIndex + 1
|
||||
end
|
||||
self:Reset()
|
||||
return 1
|
||||
end
|
||||
function BTSelector.prototype.Reset(self)
|
||||
self.runningIndex = 0
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.children do
|
||||
self.children[i + 1]:Reset()
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
____exports.BTInverter = __TS__Class()
|
||||
local BTInverter = ____exports.BTInverter
|
||||
BTInverter.name = "BTInverter"
|
||||
function BTInverter.prototype.____constructor(self, child)
|
||||
self.child = child
|
||||
end
|
||||
function BTInverter.prototype.Tick(self, ctx)
|
||||
local st = self.child:Tick(ctx)
|
||||
if st == 0 then
|
||||
return 1
|
||||
end
|
||||
if st == 1 then
|
||||
return 0
|
||||
end
|
||||
return 2
|
||||
end
|
||||
function BTInverter.prototype.Reset(self)
|
||||
self.child:Reset()
|
||||
end
|
||||
____exports.BTCooldown = __TS__Class()
|
||||
local BTCooldown = ____exports.BTCooldown
|
||||
BTCooldown.name = "BTCooldown"
|
||||
function BTCooldown.prototype.____constructor(self, child, cooldownSeconds)
|
||||
self.lastSuccessTime = -9999
|
||||
self.child = child
|
||||
self.cooldownSeconds = cooldownSeconds
|
||||
end
|
||||
function BTCooldown.prototype.Tick(self, ctx)
|
||||
if ctx.time - self.lastSuccessTime < self.cooldownSeconds then
|
||||
return 1
|
||||
end
|
||||
local st = self.child:Tick(ctx)
|
||||
if st == 0 then
|
||||
self.lastSuccessTime = ctx.time
|
||||
end
|
||||
return st
|
||||
end
|
||||
function BTCooldown.prototype.Reset(self)
|
||||
self.child:Reset()
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,180 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.AttributeAndBuffComponent = __TS__Class()
|
||||
local AttributeAndBuffComponent = ____exports.AttributeAndBuffComponent
|
||||
AttributeAndBuffComponent.name = "AttributeAndBuffComponent"
|
||||
function AttributeAndBuffComponent.prototype.____constructor(self, initialStats)
|
||||
self.baseStats = {}
|
||||
self.buffs = {}
|
||||
self.listeners = {}
|
||||
for k, v in pairs(initialStats) do
|
||||
self.baseStats[k] = v
|
||||
end
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.OnAttributeChanged(self, cb)
|
||||
self.listeners[#self.listeners + 1] = cb
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.GetBase(self, attr)
|
||||
return self.baseStats[attr] or 0
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.SetBase(self, attr, val)
|
||||
local clamped = val
|
||||
if attr == "HP" then
|
||||
clamped = math.max(
|
||||
0,
|
||||
math.min(
|
||||
val,
|
||||
self:GetFinal("MaxHP")
|
||||
)
|
||||
)
|
||||
end
|
||||
self.baseStats[attr] = clamped
|
||||
self:Notify(
|
||||
attr,
|
||||
self:GetFinal(attr)
|
||||
)
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.GetFinal(self, attr)
|
||||
local baseVal = self.baseStats[attr] or 0
|
||||
local flat = 0
|
||||
local pct = 0
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.buffs do
|
||||
do
|
||||
local b = self.buffs[i + 1]
|
||||
local mods = b.spec.modifiers
|
||||
if mods == nil then
|
||||
goto __continue10
|
||||
end
|
||||
do
|
||||
local j = 0
|
||||
while j < #mods do
|
||||
do
|
||||
local m = mods[j + 1]
|
||||
if m.attr ~= attr then
|
||||
goto __continue13
|
||||
end
|
||||
if m.op == 0 then
|
||||
flat = flat + m.value * b.stacks
|
||||
else
|
||||
pct = pct + m.value * b.stacks
|
||||
end
|
||||
end
|
||||
::__continue13::
|
||||
j = j + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
::__continue10::
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return (baseVal + flat) * (1 + pct)
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.ApplyBuff(self, spec)
|
||||
local policy = spec.stackPolicy or 0
|
||||
local maxStacks = spec.maxStacks or 1
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.buffs do
|
||||
local existing = self.buffs[i + 1]
|
||||
if existing.spec.id == spec.id then
|
||||
if policy == 2 then
|
||||
return
|
||||
end
|
||||
existing.remaining = spec.duration
|
||||
if policy == 1 and existing.stacks < maxStacks then
|
||||
existing.stacks = existing.stacks + 1
|
||||
end
|
||||
return
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
self.buffs[#self.buffs + 1] = {spec = spec, stacks = 1, remaining = spec.duration, tickAccumulator = 0}
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.RemoveBuff(self, buffId)
|
||||
do
|
||||
local i = #self.buffs - 1
|
||||
while i >= 0 do
|
||||
if self.buffs[i + 1].spec.id == buffId then
|
||||
table.remove(self.buffs, i + 1)
|
||||
return true
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.GetBuffStacks(self, buffId)
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.buffs do
|
||||
if self.buffs[i + 1].spec.id == buffId then
|
||||
return self.buffs[i + 1].stacks
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.Tick(self, dt)
|
||||
do
|
||||
local i = #self.buffs - 1
|
||||
while i >= 0 do
|
||||
local b = self.buffs[i + 1]
|
||||
b.remaining = b.remaining - dt
|
||||
if b.spec.tickInterval ~= nil and b.spec.tickInterval > 0 and b.spec.hpPerTick ~= nil then
|
||||
b.tickAccumulator = b.tickAccumulator + dt
|
||||
while b.tickAccumulator >= b.spec.tickInterval do
|
||||
b.tickAccumulator = b.tickAccumulator - b.spec.tickInterval
|
||||
local delta = b.spec.hpPerTick * b.stacks
|
||||
self:SetBase(
|
||||
"HP",
|
||||
self:GetBase("HP") + delta
|
||||
)
|
||||
end
|
||||
end
|
||||
if b.remaining <= 0 then
|
||||
table.remove(self.buffs, i + 1)
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.ApplyDamageFrom(self, attacker, skillMultiplier, isCrit, critMultiplier)
|
||||
if critMultiplier == nil then
|
||||
critMultiplier = 1.5
|
||||
end
|
||||
local atk = attacker:GetFinal("Attack")
|
||||
local def = math.max(
|
||||
0,
|
||||
self:GetFinal("Defense")
|
||||
)
|
||||
local raw = atk * skillMultiplier * (isCrit and critMultiplier or 1)
|
||||
local mitigated = math.floor(raw * (100 / (100 + def)) + 0.5)
|
||||
local nextHp = math.max(
|
||||
0,
|
||||
self:GetBase("HP") - mitigated
|
||||
)
|
||||
self:SetBase("HP", nextHp)
|
||||
return {
|
||||
rawDamage = raw,
|
||||
mitigatedDamage = mitigated,
|
||||
isCrit = isCrit,
|
||||
targetHpAfter = nextHp,
|
||||
targetKilled = nextHp <= 0
|
||||
}
|
||||
end
|
||||
function AttributeAndBuffComponent.prototype.Notify(self, attr, val)
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.listeners do
|
||||
self.listeners[i + 1](attr, val)
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,128 @@
|
||||
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
|
||||
@@ -0,0 +1,88 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.StateMachine = __TS__Class()
|
||||
local StateMachine = ____exports.StateMachine
|
||||
StateMachine.name = "StateMachine"
|
||||
function StateMachine.prototype.____constructor(self, ctx)
|
||||
self.states = {}
|
||||
self.transitions = {}
|
||||
self.currentState = nil
|
||||
self.stateElapsed = 0
|
||||
self.paused = false
|
||||
self.history = {}
|
||||
self.ctx = ctx
|
||||
end
|
||||
function StateMachine.prototype.RegisterState(self, id, handler)
|
||||
self.states[id] = handler
|
||||
end
|
||||
function StateMachine.prototype.AddTransition(self, from, to, condition)
|
||||
self.transitions[#self.transitions + 1] = {from = from, to = to, condition = condition}
|
||||
end
|
||||
function StateMachine.prototype.Start(self, initialState)
|
||||
if self.states[initialState] == nil then
|
||||
return false
|
||||
end
|
||||
self.currentState = initialState
|
||||
self.stateElapsed = 0
|
||||
self.history[#self.history + 1] = initialState
|
||||
local handler = self.states[initialState]
|
||||
if handler and handler.OnEnter then
|
||||
handler.OnEnter(self.ctx, nil)
|
||||
end
|
||||
return true
|
||||
end
|
||||
function StateMachine.prototype.ChangeState(self, nextState)
|
||||
if self.currentState == nextState or self.states[nextState] == nil then
|
||||
return false
|
||||
end
|
||||
local prev = self.currentState
|
||||
if prev ~= nil then
|
||||
local prevHandler = self.states[prev]
|
||||
if prevHandler and prevHandler.OnExit then
|
||||
prevHandler.OnExit(self.ctx, nextState)
|
||||
end
|
||||
end
|
||||
self.currentState = nextState
|
||||
self.stateElapsed = 0
|
||||
self.history[#self.history + 1] = nextState
|
||||
local nextHandler = self.states[nextState]
|
||||
if nextHandler and nextHandler.OnEnter then
|
||||
nextHandler.OnEnter(self.ctx, prev)
|
||||
end
|
||||
return true
|
||||
end
|
||||
function StateMachine.prototype.Update(self, dt)
|
||||
if self.paused or self.currentState == nil then
|
||||
return
|
||||
end
|
||||
self.stateElapsed = self.stateElapsed + dt
|
||||
do
|
||||
local i = 0
|
||||
while i < #self.transitions do
|
||||
local tr = self.transitions[i + 1]
|
||||
if (tr.from == "*" or tr.from == self.currentState) and tr.to ~= self.currentState and tr.condition(self.ctx) then
|
||||
self:ChangeState(tr.to)
|
||||
break
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
local handler = self.states[self.currentState]
|
||||
if handler and handler.OnUpdate then
|
||||
handler.OnUpdate(self.ctx, dt)
|
||||
end
|
||||
end
|
||||
function StateMachine.prototype.SetPaused(self, paused)
|
||||
self.paused = paused
|
||||
end
|
||||
function StateMachine.prototype.GetCurrentState(self)
|
||||
return self.currentState
|
||||
end
|
||||
function StateMachine.prototype.GetStateElapsed(self)
|
||||
return self.stateElapsed
|
||||
end
|
||||
function StateMachine.prototype.GetHistory(self)
|
||||
return self.history
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,483 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__New = ____lualib.__TS__New
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local Suite = ____TestRunner.Suite
|
||||
local Test = ____TestRunner.Test
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertNear = ____TestRunner.AssertNear
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertFalse = ____TestRunner.AssertFalse
|
||||
local AssertNil = ____TestRunner.AssertNil
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
|
||||
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
|
||||
local ____FSM = require("Tests.Gameplay.FSM")
|
||||
local StateMachine = ____FSM.StateMachine
|
||||
local ____ObjectPool = require("Tests.Gameplay.ObjectPool")
|
||||
local ObjectPool = ____ObjectPool.ObjectPool
|
||||
local ____TimerScheduler = require("Tests.Gameplay.TimerScheduler")
|
||||
local TimerScheduler = ____TimerScheduler.TimerScheduler
|
||||
local CooldownTracker = ____TimerScheduler.CooldownTracker
|
||||
local ____BuffAndAttributeSystem = require("Tests.Gameplay.BuffAndAttributeSystem")
|
||||
local AttributeAndBuffComponent = ____BuffAndAttributeSystem.AttributeAndBuffComponent
|
||||
local ____InventorySystem = require("Tests.Gameplay.InventorySystem")
|
||||
local InventorySystem = ____InventorySystem.InventorySystem
|
||||
local ____CoroutineTaskRunner = require("Tests.Gameplay.CoroutineTaskRunner")
|
||||
local CoroutineTaskRunner = ____CoroutineTaskRunner.CoroutineTaskRunner
|
||||
local ____BehaviorTree = require("Tests.Gameplay.BehaviorTree")
|
||||
local BTAction = ____BehaviorTree.BTAction
|
||||
local BTCondition = ____BehaviorTree.BTCondition
|
||||
local BTCooldown = ____BehaviorTree.BTCooldown
|
||||
local BTInverter = ____BehaviorTree.BTInverter
|
||||
local BTSelector = ____BehaviorTree.BTSelector
|
||||
local BTSequence = ____BehaviorTree.BTSequence
|
||||
function ____exports.RunGameplayTests()
|
||||
Suite(
|
||||
"Gameplay.Systems",
|
||||
function()
|
||||
Test(
|
||||
"FSM: state lifecycle, conditional transitions, pause and history",
|
||||
function()
|
||||
local ctx = {speed = 0, hp = 100, log = {}}
|
||||
local fsm = __TS__New(StateMachine, ctx)
|
||||
fsm:RegisterState(
|
||||
"Idle",
|
||||
{
|
||||
OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Idle"
|
||||
end,
|
||||
OnExit = function(c)
|
||||
c.log[#c.log + 1] = "Exit:Idle"
|
||||
end
|
||||
}
|
||||
)
|
||||
fsm:RegisterState(
|
||||
"Run",
|
||||
{
|
||||
OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Run"
|
||||
end,
|
||||
OnUpdate = function(c, dt)
|
||||
c.log[#c.log + 1] = "Run:" .. tostring(math.floor(dt * 100))
|
||||
end
|
||||
}
|
||||
)
|
||||
fsm:RegisterState(
|
||||
"Dead",
|
||||
{OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Dead"
|
||||
end}
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"Idle",
|
||||
"Run",
|
||||
function(c) return c.speed > 0 end
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"Run",
|
||||
"Idle",
|
||||
function(c) return c.speed == 0 end
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"*",
|
||||
"Dead",
|
||||
function(c) return c.hp <= 0 end
|
||||
)
|
||||
AssertTrue(fsm:Start("Idle"))
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Idle"
|
||||
)
|
||||
ctx.speed = 300
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Run"
|
||||
)
|
||||
fsm:SetPaused(true)
|
||||
ctx.hp = 0
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Run"
|
||||
)
|
||||
fsm:SetPaused(false)
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Dead"
|
||||
)
|
||||
AssertArrayEqual(
|
||||
fsm:GetHistory(),
|
||||
{"Idle", "Run", "Dead"}
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"ObjectPool: prewarm, acquire/reuse, maxCapacity cap and reset hooks",
|
||||
function()
|
||||
local pool = __TS__New(
|
||||
ObjectPool,
|
||||
{
|
||||
factory = function() return {active = false, damage = 0} end,
|
||||
onAcquire = function(b)
|
||||
b.active = true
|
||||
b.damage = 10
|
||||
end,
|
||||
onRelease = function(b)
|
||||
b.active = false
|
||||
b.damage = 0
|
||||
end,
|
||||
maxCapacity = 2
|
||||
}
|
||||
)
|
||||
pool:Prewarm(2)
|
||||
AssertEqual(
|
||||
pool:GetIdleCount(),
|
||||
2
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
2
|
||||
)
|
||||
local b1 = pool:Acquire()
|
||||
local b2 = pool:Acquire()
|
||||
local b3 = pool:Acquire()
|
||||
AssertTrue(b1.active)
|
||||
AssertEqual(b1.damage, 10)
|
||||
AssertEqual(
|
||||
pool:GetActiveCount(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
3
|
||||
)
|
||||
pool:Release(b1)
|
||||
pool:Release(b2)
|
||||
pool:Release(b3)
|
||||
AssertFalse(b1.active)
|
||||
AssertEqual(
|
||||
pool:GetActiveCount(),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetIdleCount(),
|
||||
2
|
||||
)
|
||||
local reused = pool:Acquire()
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
3
|
||||
)
|
||||
AssertTrue(reused.active)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"TimerScheduler & CooldownTracker: one-shot, repeat, self-cancel, timeScale and cooldowns",
|
||||
function()
|
||||
local scheduler = __TS__New(TimerScheduler)
|
||||
local events = {}
|
||||
scheduler:ScheduleOnce(
|
||||
0.5,
|
||||
function()
|
||||
events[#events + 1] = "once"
|
||||
end
|
||||
)
|
||||
local repId = scheduler:ScheduleRepeat(
|
||||
0.25,
|
||||
function(id)
|
||||
events[#events + 1] = "rep#" .. tostring(id)
|
||||
end,
|
||||
3
|
||||
)
|
||||
local selfCancelCount = 0
|
||||
scheduler:ScheduleRepeat(
|
||||
0.2,
|
||||
function(id)
|
||||
selfCancelCount = selfCancelCount + 1
|
||||
if selfCancelCount == 2 then
|
||||
scheduler:Cancel(id)
|
||||
end
|
||||
end
|
||||
)
|
||||
scheduler:Tick(0.5)
|
||||
AssertEqual(selfCancelCount, 2)
|
||||
AssertEqual(#events, 3)
|
||||
scheduler:SetPaused(repId, true)
|
||||
scheduler:Tick(1)
|
||||
AssertEqual(#events, 3)
|
||||
scheduler:SetPaused(repId, false)
|
||||
scheduler:SetTimeScale(0.5)
|
||||
scheduler:Tick(0.5)
|
||||
AssertEqual(#events, 4)
|
||||
AssertEqual(
|
||||
scheduler:GetActiveCount(),
|
||||
0
|
||||
)
|
||||
local cd = __TS__New(CooldownTracker)
|
||||
AssertTrue(cd:IsReady("Fireball"))
|
||||
cd:StartCooldown("Fireball", 5)
|
||||
AssertFalse(cd:IsReady("Fireball"))
|
||||
cd:Tick(2)
|
||||
AssertNear(
|
||||
cd:GetRemaining("Fireball"),
|
||||
3
|
||||
)
|
||||
cd:ReduceCooldown("Fireball", 3.5)
|
||||
AssertTrue(cd:IsReady("Fireball"))
|
||||
AssertEqual(
|
||||
cd:GetRemaining("Fireball"),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"BuffAndAttributeSystem: flat/percent modifiers, stacking, DoT tick and damage mitigation",
|
||||
function()
|
||||
local hero = __TS__New(AttributeAndBuffComponent, {MaxHP = 200, HP = 200, Attack = 50, Defense = 0})
|
||||
local boss = __TS__New(AttributeAndBuffComponent, {MaxHP = 500, HP = 500, Attack = 40, Defense = 100})
|
||||
local furyBuff = {
|
||||
id = "Fury",
|
||||
duration = 5,
|
||||
maxStacks = 3,
|
||||
stackPolicy = 1,
|
||||
modifiers = {{attr = "Attack", op = 0, value = 10}, {attr = "Attack", op = 1, value = 0.2}}
|
||||
}
|
||||
hero:ApplyBuff(furyBuff)
|
||||
hero:ApplyBuff(furyBuff)
|
||||
AssertEqual(
|
||||
hero:GetBuffStacks("Fury"),
|
||||
2
|
||||
)
|
||||
AssertNear(
|
||||
hero:GetFinal("Attack"),
|
||||
98
|
||||
)
|
||||
local dmg = boss:ApplyDamageFrom(hero, 1, true, 1.5)
|
||||
AssertEqual(dmg.mitigatedDamage, 74)
|
||||
AssertEqual(dmg.targetHpAfter, 426)
|
||||
AssertFalse(dmg.targetKilled)
|
||||
boss:ApplyBuff({id = "Poison", duration = 3.1, tickInterval = 1, hpPerTick = -15})
|
||||
boss:Tick(2.5)
|
||||
AssertEqual(
|
||||
boss:GetBase("HP"),
|
||||
396
|
||||
)
|
||||
boss:Tick(1)
|
||||
AssertEqual(
|
||||
boss:GetBase("HP"),
|
||||
381
|
||||
)
|
||||
AssertEqual(
|
||||
boss:GetBuffStacks("Poison"),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"InventorySystem: stack merge, split, swap, consume, filter and serialize/deserialize",
|
||||
function()
|
||||
local inv = __TS__New(InventorySystem, 4)
|
||||
inv:RegisterItemDef({
|
||||
itemId = "potion_hp",
|
||||
name = "Health Potion",
|
||||
category = "Consumable",
|
||||
rarity = 0,
|
||||
maxStack = 10
|
||||
})
|
||||
inv:RegisterItemDef({
|
||||
itemId = "sword_excalibur",
|
||||
name = "Excalibur",
|
||||
category = "Weapon",
|
||||
rarity = 3,
|
||||
maxStack = 1
|
||||
})
|
||||
AssertEqual(
|
||||
inv:AddItem("potion_hp", 25),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetItemCount("potion_hp"),
|
||||
25
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetUsedSlotCount(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
inv:AddItem("sword_excalibur", 2),
|
||||
1
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetItemCount("sword_excalibur"),
|
||||
1
|
||||
)
|
||||
AssertTrue(inv:ConsumeItem("potion_hp", 12))
|
||||
AssertEqual(
|
||||
inv:GetItemCount("potion_hp"),
|
||||
13
|
||||
)
|
||||
AssertNil(inv:GetSlot(2))
|
||||
AssertTrue(inv:SplitSlot(0, 2, 4))
|
||||
AssertEqual(
|
||||
inv:GetSlot(0).count,
|
||||
6
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetSlot(2).count,
|
||||
4
|
||||
)
|
||||
AssertTrue(inv:SwapSlots(0, 3))
|
||||
AssertEqual(
|
||||
inv:GetSlot(0).itemId,
|
||||
"sword_excalibur"
|
||||
)
|
||||
AssertEqual(
|
||||
#inv:FilterByCategory("Weapon"),
|
||||
1
|
||||
)
|
||||
AssertEqual(
|
||||
#inv:FilterByCategory("Consumable"),
|
||||
3
|
||||
)
|
||||
local saved = inv:Serialize()
|
||||
local restored = __TS__New(InventorySystem, 1)
|
||||
restored:Deserialize(saved)
|
||||
AssertEqual(
|
||||
restored:GetItemCount("potion_hp"),
|
||||
13
|
||||
)
|
||||
AssertEqual(
|
||||
restored:GetSlot(0).itemId,
|
||||
"sword_excalibur"
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"CoroutineTaskRunner: WaitSeconds, WaitUntil and UObject destroy auto-cancellation",
|
||||
function()
|
||||
local runner = __TS__New(CoroutineTaskRunner)
|
||||
local trace = {}
|
||||
local doorOpened = false
|
||||
runner:Start(function()
|
||||
trace[#trace + 1] = "start"
|
||||
CoroutineTaskRunner.WaitSeconds(0.5)
|
||||
trace[#trace + 1] = "after_delay"
|
||||
CoroutineTaskRunner.WaitUntil(function() return doorOpened end)
|
||||
trace[#trace + 1] = "entered_room"
|
||||
end)
|
||||
AssertArrayEqual(trace, {"start"})
|
||||
runner:Tick(0.3)
|
||||
AssertArrayEqual(trace, {"start"})
|
||||
runner:Tick(0.3)
|
||||
AssertArrayEqual(trace, {"start", "after_delay"})
|
||||
runner:Tick(1)
|
||||
AssertArrayEqual(trace, {"start", "after_delay"})
|
||||
doorOpened = true
|
||||
runner:Tick(0.016)
|
||||
AssertArrayEqual(trace, {"start", "after_delay", "entered_room"})
|
||||
AssertEqual(
|
||||
runner:GetActiveTaskCount(),
|
||||
0
|
||||
)
|
||||
local ownerActor = CreateBoundInstance({}, "AActor")
|
||||
local postDestroyExecuted = false
|
||||
runner:Start(
|
||||
function()
|
||||
CoroutineTaskRunner.WaitSeconds(1)
|
||||
postDestroyExecuted = true
|
||||
end,
|
||||
ownerActor
|
||||
)
|
||||
ownerActor:K2_DestroyActor()
|
||||
runner:Tick(2)
|
||||
AssertFalse(postDestroyExecuted)
|
||||
AssertEqual(
|
||||
runner:GetActiveTaskCount(),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"BehaviorTree: Selector/Sequence/Cooldown/Inverter with Running resumption",
|
||||
function()
|
||||
local log = {}
|
||||
local castProgress = 0
|
||||
local castUltimate = __TS__New(
|
||||
BTCooldown,
|
||||
__TS__New(
|
||||
BTSequence,
|
||||
{
|
||||
__TS__New(
|
||||
BTCondition,
|
||||
function(ctx) return ctx.bb.mana >= 50 end
|
||||
),
|
||||
__TS__New(
|
||||
BTAction,
|
||||
function(ctx)
|
||||
castProgress = castProgress + ctx.dt
|
||||
if castProgress < 1 then
|
||||
log[#log + 1] = "casting"
|
||||
return 2
|
||||
end
|
||||
castProgress = 0
|
||||
ctx.bb.mana = ctx.bb.mana - 50
|
||||
log[#log + 1] = "ult_fired"
|
||||
return 0
|
||||
end
|
||||
)
|
||||
}
|
||||
),
|
||||
5
|
||||
)
|
||||
local basicAttack = __TS__New(
|
||||
BTSequence,
|
||||
{
|
||||
__TS__New(
|
||||
BTInverter,
|
||||
__TS__New(
|
||||
BTCondition,
|
||||
function(ctx) return ctx.bb.stunned == true end
|
||||
)
|
||||
),
|
||||
__TS__New(
|
||||
BTAction,
|
||||
function()
|
||||
log[#log + 1] = "basic_attack"
|
||||
return 0
|
||||
end
|
||||
)
|
||||
}
|
||||
)
|
||||
local root = __TS__New(BTSelector, {castUltimate, basicAttack})
|
||||
local ctx = {dt = 0.5, time = 0, bb = {mana = 60, stunned = false}}
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
2
|
||||
)
|
||||
ctx.time = 0.5
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
0
|
||||
)
|
||||
AssertEqual(ctx.bb.mana, 10)
|
||||
ctx.time = 1
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
0
|
||||
)
|
||||
ctx.time = 1.5
|
||||
ctx.bb.stunned = true
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
1
|
||||
)
|
||||
AssertArrayEqual(log, {"casting", "ult_fired", "basic_attack"})
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,161 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.InventorySystem = __TS__Class()
|
||||
local InventorySystem = ____exports.InventorySystem
|
||||
InventorySystem.name = "InventorySystem"
|
||||
function InventorySystem.prototype.____constructor(self, capacity)
|
||||
self.defs = {}
|
||||
self.slots = {}
|
||||
self.capacity = capacity
|
||||
end
|
||||
function InventorySystem.prototype.RegisterItemDef(self, def)
|
||||
self.defs[def.itemId] = def
|
||||
end
|
||||
function InventorySystem.prototype.AddItem(self, itemId, amount)
|
||||
local def = self.defs[itemId]
|
||||
if def == nil or amount <= 0 then
|
||||
return amount
|
||||
end
|
||||
local remaining = amount
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity and remaining > 0 do
|
||||
local slot = self.slots[i]
|
||||
if slot ~= nil and slot.itemId == itemId and slot.count < def.maxStack then
|
||||
local space = def.maxStack - slot.count
|
||||
local add = math.min(space, remaining)
|
||||
slot.count = slot.count + add
|
||||
remaining = remaining - add
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity and remaining > 0 do
|
||||
if self.slots[i] == nil then
|
||||
local add = math.min(def.maxStack, remaining)
|
||||
self.slots[i] = {itemId = itemId, count = add}
|
||||
remaining = remaining - add
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return remaining
|
||||
end
|
||||
function InventorySystem.prototype.ConsumeItem(self, itemId, amount)
|
||||
if amount <= 0 or self:GetItemCount(itemId) < amount then
|
||||
return false
|
||||
end
|
||||
local remaining = amount
|
||||
do
|
||||
local i = self.capacity - 1
|
||||
while i >= 0 and remaining > 0 do
|
||||
local slot = self.slots[i]
|
||||
if slot ~= nil and slot.itemId == itemId then
|
||||
local take = math.min(slot.count, remaining)
|
||||
slot.count = slot.count - take
|
||||
remaining = remaining - take
|
||||
if slot.count <= 0 then
|
||||
self.slots[i] = nil
|
||||
end
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
function InventorySystem.prototype.SwapSlots(self, slotA, slotB)
|
||||
if slotA < 0 or slotA >= self.capacity or slotB < 0 or slotB >= self.capacity then
|
||||
return false
|
||||
end
|
||||
local tmp = self.slots[slotA]
|
||||
self.slots[slotA] = self.slots[slotB]
|
||||
self.slots[slotB] = tmp
|
||||
return true
|
||||
end
|
||||
function InventorySystem.prototype.SplitSlot(self, fromSlot, toSlot, splitCount)
|
||||
if fromSlot < 0 or fromSlot >= self.capacity or toSlot < 0 or toSlot >= self.capacity or fromSlot == toSlot then
|
||||
return false
|
||||
end
|
||||
local src = self.slots[fromSlot]
|
||||
if src == nil or self.slots[toSlot] ~= nil then
|
||||
return false
|
||||
end
|
||||
if splitCount <= 0 or splitCount >= src.count then
|
||||
return false
|
||||
end
|
||||
src.count = src.count - splitCount
|
||||
self.slots[toSlot] = {itemId = src.itemId, count = splitCount}
|
||||
return true
|
||||
end
|
||||
function InventorySystem.prototype.GetItemCount(self, itemId)
|
||||
local total = 0
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity do
|
||||
local slot = self.slots[i]
|
||||
if slot ~= nil and slot.itemId == itemId then
|
||||
total = total + slot.count
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return total
|
||||
end
|
||||
function InventorySystem.prototype.GetSlot(self, index)
|
||||
return self.slots[index]
|
||||
end
|
||||
function InventorySystem.prototype.GetUsedSlotCount(self)
|
||||
local used = 0
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity do
|
||||
if self.slots[i] ~= nil then
|
||||
used = used + 1
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return used
|
||||
end
|
||||
function InventorySystem.prototype.FilterByCategory(self, category)
|
||||
local out = {}
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity do
|
||||
local slot = self.slots[i]
|
||||
if slot ~= nil then
|
||||
local def = self.defs[slot.itemId]
|
||||
if def ~= nil and def.category == category then
|
||||
out[#out + 1] = slot
|
||||
end
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
function InventorySystem.prototype.Serialize(self)
|
||||
local copiedSlots = {}
|
||||
do
|
||||
local i = 0
|
||||
while i < self.capacity do
|
||||
local s = self.slots[i]
|
||||
if s ~= nil then
|
||||
copiedSlots[i] = {itemId = s.itemId, count = s.count}
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return {capacity = self.capacity, slots = copiedSlots}
|
||||
end
|
||||
function InventorySystem.prototype.Deserialize(self, data)
|
||||
self.capacity = data.capacity
|
||||
self.slots = {}
|
||||
for idx, s in pairs(data.slots) do
|
||||
self.slots[idx] = {itemId = s.itemId, count = s.count}
|
||||
end
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,61 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.ObjectPool = __TS__Class()
|
||||
local ObjectPool = ____exports.ObjectPool
|
||||
ObjectPool.name = "ObjectPool"
|
||||
function ObjectPool.prototype.____constructor(self, options)
|
||||
self.idle = {}
|
||||
self.activeCount = 0
|
||||
self.totalCreated = 0
|
||||
self.factory = options.factory
|
||||
self.onAcquire = options.onAcquire
|
||||
self.onRelease = options.onRelease
|
||||
self.maxCapacity = options.maxCapacity or 64
|
||||
end
|
||||
function ObjectPool.prototype.Prewarm(self, count)
|
||||
local target = math.min(count, self.maxCapacity)
|
||||
while #self.idle < target do
|
||||
self.totalCreated = self.totalCreated + 1
|
||||
self.idle[#self.idle + 1] = self.factory()
|
||||
end
|
||||
end
|
||||
function ObjectPool.prototype.Acquire(self)
|
||||
local obj
|
||||
if #self.idle > 0 then
|
||||
obj = table.remove(self.idle)
|
||||
else
|
||||
self.totalCreated = self.totalCreated + 1
|
||||
obj = self.factory()
|
||||
end
|
||||
self.activeCount = self.activeCount + 1
|
||||
if self.onAcquire then
|
||||
self.onAcquire(obj)
|
||||
end
|
||||
return obj
|
||||
end
|
||||
function ObjectPool.prototype.Release(self, obj)
|
||||
if self.activeCount > 0 then
|
||||
self.activeCount = self.activeCount - 1
|
||||
end
|
||||
if self.onRelease then
|
||||
self.onRelease(obj)
|
||||
end
|
||||
if #self.idle < self.maxCapacity then
|
||||
self.idle[#self.idle + 1] = obj
|
||||
end
|
||||
end
|
||||
function ObjectPool.prototype.Clear(self)
|
||||
self.idle = {}
|
||||
self.activeCount = 0
|
||||
end
|
||||
function ObjectPool.prototype.GetIdleCount(self)
|
||||
return #self.idle
|
||||
end
|
||||
function ObjectPool.prototype.GetActiveCount(self)
|
||||
return self.activeCount
|
||||
end
|
||||
function ObjectPool.prototype.GetTotalCreated(self)
|
||||
return self.totalCreated
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,159 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.TimerScheduler = __TS__Class()
|
||||
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__Class()
|
||||
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
|
||||
return ____exports
|
||||
Reference in new issue
Block a user