Files
simple-makes-complex/mods/SYSTEM/HelperLib/src/Lib/globalStep.lua

76 lines
1.9 KiB
Lua

---@type {timeDelay:number, callbackFunc:fun(deltaTime:number), timer:number, lastCall:number}[]
local callsWithDelay = {}
---@param timeDelay number
---@param callbackFunc fun(deltaTime:number)
function Smc.registerGlobalStepWithDelay(timeDelay, callbackFunc)
table.insert(callsWithDelay, {
timeDelay = timeDelay,
callbackFunc = callbackFunc,
timer = 0.0,
lastCall = 0.0
})
end
core.register_globalstep(function(deltaTime)
---@type number
local timer
local lastCall
for _, callback in ipairs(callsWithDelay) do
timer = callback.timer
lastCall = callback.lastCall
timer = timer + deltaTime
lastCall = lastCall + deltaTime
if timer >= callback.timeDelay then
callback.callbackFunc(lastCall)
callback.timer = timer - callback.timeDelay
callback.lastCall = 0.0
else
callback.timer = timer
callback.lastCall = lastCall
end
end
end)
---@type {timeDelay:number, callbackFunc:fun(deltaTime:number, player), timer:number, lastCall:number}[]
local callsForEachPlayer = {}
---@param timeDelay number
---@param callbackFunc fun(deltaTime:number, player:Player)
function Smc.registerGlobalStepForEachPlayer(timeDelay, callbackFunc)
table.insert(callsForEachPlayer, {
timeDelay = timeDelay,
callbackFunc = callbackFunc,
timer = 0.0,
lastCall = 0.0,
})
end
core.register_globalstep(function(deltaTime)
---@type number
local timer
local lastCall
for _, player in ipairs(core.get_connected_players()) do
for _, callback in ipairs(callsForEachPlayer) do
timer = callback.timer
lastCall = callback.lastCall
timer = timer + deltaTime
lastCall = lastCall + deltaTime
if timer >= callback.timeDelay then
callback.callbackFunc(lastCall, player)
callback.timer = timer - callback.timeDelay
callback.lastCall = 0.0
else
callback.timer = timer
callback.lastCall = lastCall
end
end
end
end)