Callbacks -> Events. Fihish Control

This commit is contained in:
Van
2025-12-29 18:51:22 +03:00
parent 96289375b1
commit 7773e25a81
9 changed files with 94 additions and 55 deletions

View File

@@ -25,7 +25,7 @@
"globals": [ "globals": [
"core", "core",
"table", "table",
"Callbacks", "Events",
"VoxelArea", "VoxelArea",
"VoxelManip", "VoxelManip",
"vector", "vector",

View File

@@ -21,7 +21,7 @@ core.register_globalstep(function(deltaTime)
if timer >= callback.timeDelay then if timer >= callback.timeDelay then
callback.callbackFunc(deltaTime) callback.callbackFunc(deltaTime)
callback.timer = 0.0 callback.timer = timer - callback.timeDelay
else else
callback.timer = timer callback.timer = timer
end end
@@ -51,7 +51,7 @@ core.register_globalstep(function(deltaTime)
if timer >= callback.timeDelay then if timer >= callback.timeDelay then
callback.callbackFunc(deltaTime, player) callback.callbackFunc(deltaTime, player)
callback.timer = 0.0 callback.timer = timer - callback.timeDelay
else else
callback.timer = timer callback.timer = timer
end end

View File

@@ -1,33 +0,0 @@
Callbacks = {}
---@alias Callbacks.callbackFunc fun(callbackName, ...)
---@type table<string, Callbacks.callbackFunc[]>
local registeredCallbacks = {}
---@param callbackName string
function Callbacks.registerCallback(callbackName)
assert(registeredCallbacks[callbackName] == nil, ('Callback %s is already registered.'):format(callbackName))
registeredCallbacks[callbackName] = {}
end
---@param callbackName string
---@param callbackFunc Callbacks.callbackFunc
function Callbacks.subscribe(callbackName, callbackFunc)
local callbacks = registeredCallbacks[callbackName]
assert(callbacks ~= nil, ('Callback %s does not exist'):format(callbackName))
table.insert(callbacks, callbackFunc)
end
---@param callbackName string
---@param ... any
function Callbacks.call(callbackName, ...)
local callbacks = registeredCallbacks[callbackName]
assert(callbacks ~= nil, ('Callback %s does not exist'):format(callbackName))
for _, func in ipairs(callbacks) do
func(callbackName, ...)
end
end

View File

@@ -1,2 +0,0 @@
name = smc__api__callbacks
depends = smc__api__table

33
mods/API/Events/init.lua Normal file
View File

@@ -0,0 +1,33 @@
Events = {}
---@alias Events.callback fun(eventName, ...)
---@type table<string, Events.callback[]>
local registeredEvents = {}
---@param eventName string
function Events.registerEvent(eventName)
assert(registeredEvents[eventName] == nil, ('Event %s is already registered.'):format(eventName))
registeredEvents[eventName] = {}
end
---@param eventName string
---@param callback Events.callback
function Events.subscribe(eventName, callback)
local callbacks = registeredEvents[eventName]
assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName))
table.insert(callbacks, callback)
end
---@param eventName string
---@param ... any
function Events.call(eventName, ...)
local callbacks = registeredEvents[eventName]
assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName))
for _, func in ipairs(callbacks) do
func(eventName, ...)
end
end

2
mods/API/Events/mod.conf Normal file
View File

@@ -0,0 +1,2 @@
name = smc__api__events
depends = smc__api__table

View File

@@ -1,49 +1,84 @@
---@class Callbacks.Control ---@class Events.Control
local Control = {} local Control = {}
-- control.on_press -- control.on_press
Callbacks.registerCallback('control.on_press') Events.registerEvent('control.on_press')
---@param callbackFunc fun(player:Player, controlName:string) ---@param callbackFunc fun(player:Player, controlName:string)
function Control.subcribeOnPress(callbackFunc) function Control.subcribeOnPress(callbackFunc)
Callbacks.subscribe('control.on_press', callbackFunc) Events.subscribe('control.on_press', callbackFunc)
end end
-- control.on_release -- control.on_release
Callbacks.registerCallback('control.on_release') Events.registerEvent('control.on_release')
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number) ---@param callbackFunc fun(player:Player, controlName:string, holdTime:number)
function Control.subcribeOnRelease(callbackFunc) function Control.subcribeOnRelease(callbackFunc)
Callbacks.subscribe('control.on_release', callbackFunc) Events.subscribe('control.on_release', callbackFunc)
end end
-- control.on_hold -- control.on_hold
Callbacks.registerCallback('control.on_hold') Events.registerEvent('control.on_hold')
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number) ---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number)
function Control.subcribeOnHold(callbackFunc) function Control.subcribeOnHold(callbackFunc)
Callbacks.subscribe('control.on_hold', callbackFunc) Events.subscribe('control.on_hold', callbackFunc)
end end
-- control.on_wield_change -- control.on_wield_change
Callbacks.registerCallback('control.on_wield_change') Events.registerEvent('control.on_wield_change')
---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number) ---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number)
function Control.subcribeOnWieldChange(callbackFunc) function Control.subcribeOnWieldChange(callbackFunc)
Callbacks.subscribe('control.on_wield_change', callbackFunc) Events.subscribe('control.on_wield_change', callbackFunc)
end end
local ControlKeys = {
-- --- Call events ---
local ControlPressKeys = {
'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom' 'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom'
} }
local ControlMovementKeys = {
'movement_x', 'movement_y'
}
local playersLastControl = {} local playersLastControl = {}
local playersControlTimer = {} local playersControlTimer = {}
--TODO: on_join & on_leave core.register_on_joinplayer(function(player, last_login)
local playerName = player:get_player_name()
if playerName == nil then
return
end
local nowControl = player:get_player_control()
if nowControl == nil then
return
end
local controlTimer = {}
for _, key in ipairs(ControlPressKeys) do
controlTimer[key] = 0.0
end
playersLastControl[playerName] = nowControl
playersControlTimer[playerName] = controlTimer
end)
core.register_on_leaveplayer(function(player, timed_out)
local playerName = player:get_player_name()
if playerName == nil then
return
end
playersLastControl[playerName] = nil
playersControlTimer[playerName] = nil
end)
Api.registerGlobalStepForEachPlayer(0, function(player, deltaTime) Api.registerGlobalStepForEachPlayer(0, function(player, deltaTime)
if player == nil then if player == nil then
@@ -70,32 +105,36 @@ Api.registerGlobalStepForEachPlayer(0, function(player, deltaTime)
return return
end end
local control for _, key in ipairs(ControlPressKeys) do
for _, key in ipairs(ControlKeys) do
local lastPressed = lastControl[key] local lastPressed = lastControl[key]
local nowPressed = nowControl[key] local nowPressed = nowControl[key]
-- on_press -- on_press
if not lastPressed and nowPressed then if not lastPressed and nowPressed then
Callbacks.call('control.on_press', key) Events.call('control.on_press', key)
controlTimer[key] = 0 controlTimer[key] = 0
-- on_release -- on_release
elseif lastPressed and not nowPressed then elseif lastPressed and not nowPressed then
Callbacks.call('control.on_release', key, controlTimer[key]) Events.call('control.on_release', key, controlTimer[key])
controlTimer[key] = 0 controlTimer[key] = 0
-- on_hold -- on_hold
elseif lastPressed and nowPressed then elseif lastPressed and nowPressed then
Callbacks.call('control.on_hold', key, controlTimer[key], deltaTime) Events.call('control.on_hold', key, controlTimer[key], deltaTime)
controlTimer[key] = controlTimer[key] + deltaTime controlTimer[key] = controlTimer[key] + deltaTime
end end
end end
playersLastControl[playerName] = nowControl --TODO: add player movement callbacks
--TODO: add mouse movement callbacks on dir
playersLastControl[playerName] = nowControl
playersControlTimer[playerName] = controlTimer
end) end)
return Control return Control