Api: rename ApiLoader -> Api

This commit is contained in:
Van
2026-01-12 20:18:02 +03:00
parent 0e8edc4f05
commit 299dad5e11
16 changed files with 17 additions and 17 deletions

6
mods/API/Api/src/Api.lua Normal file
View File

@@ -0,0 +1,6 @@
Api = {}
local modInfo = Mod.getInfo('smc__api__api')
local require = modInfo.require
require('Api.globalStep')

View File

@@ -0,0 +1,75 @@
---@type {timeDelay:number, callbackFunc:fun(deltaTime:number), timer:number, lastCall:number}[]
local callsWithDelay = {}
---@param timeDelay number
---@param callbackFunc fun(deltaTime:number)
function Api.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 Api.registerGlobalStepForEachPlayer(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 _, 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)