From 90cf189b1a17eb7da7ef0d70ed7cd8c0340b5571 Mon Sep 17 00:00:00 2001 From: Vanic Date: Mon, 8 Jun 2026 22:07:26 +0300 Subject: [PATCH] `Controls` & `Events`: add controls events and refactoring code --- mods/CORE/Control/init.lua | 4 - mods/CORE/Control/src/Control.lua | 136 ------------------ mods/CORE/Controls/init.lua | 4 + mods/CORE/{Control => Controls}/mod.conf | 2 +- mods/CORE/Controls/src/Controls.lua | 88 ++++++++++++ mods/SYSTEM/Events/src/Events.lua | 7 +- .../Events/src/Events/ControlsEvents.lua | 64 +++++++++ mods/SYSTEM/SystemLoader/mod.conf | 2 +- 8 files changed, 162 insertions(+), 145 deletions(-) delete mode 100644 mods/CORE/Control/init.lua delete mode 100644 mods/CORE/Control/src/Control.lua create mode 100644 mods/CORE/Controls/init.lua rename mods/CORE/{Control => Controls}/mod.conf (57%) create mode 100644 mods/CORE/Controls/src/Controls.lua create mode 100644 mods/SYSTEM/Events/src/Events/ControlsEvents.lua diff --git a/mods/CORE/Control/init.lua b/mods/CORE/Control/init.lua deleted file mode 100644 index d73de9d..0000000 --- a/mods/CORE/Control/init.lua +++ /dev/null @@ -1,4 +0,0 @@ -local modInfo = Mod.getInfo('smc__core__control') -local require = modInfo.require - -return require('Control') \ No newline at end of file diff --git a/mods/CORE/Control/src/Control.lua b/mods/CORE/Control/src/Control.lua deleted file mode 100644 index a85117e..0000000 --- a/mods/CORE/Control/src/Control.lua +++ /dev/null @@ -1,136 +0,0 @@ ----@class Events.Control -local Control = {} - --- control.on_press - ----@param callbackFunc fun(player:Player, controlName:string) -function Control.subcribeOnPress(callbackFunc) - Events:subscribe('control.on_press', callbackFunc) -end - - --- control.on_release - ----@param callbackFunc fun(player:Player, controlName:string, holdTime:number) -function Control.subcribeOnRelease(callbackFunc) - Events:subscribe('control.on_release', callbackFunc) -end - - --- control.on_hold - ----@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number) -function Control.subcribeOnHold(callbackFunc) - Events:subscribe('control.on_hold', callbackFunc) -end - - --- control.on_wield_change - ----@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number) -function Control.subcribeOnWieldChange(callbackFunc) - Events:subscribe('control.on_wield_change', callbackFunc) -end - - --- --- Call events --- - -local ControlPressKeys = { - 'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom' -} - -local ControlMovementKeys = { - 'movement_x', 'movement_y' -} - -local playersLastControl = {} -local playersControlTimer = {} - -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) - -Smc.registerGlobalStepForEachPlayer(0, function(deltaTime, player) - if player == nil then - return - end - - local playerName = player:get_player_name() - if playerName == nil then - return - end - - local lastControl = playersLastControl[playerName] - if lastControl == nil then - return - end - - local controlTimer = playersControlTimer[playerName] - if controlTimer == nil then - return - end - - local nowControl = player:get_player_control() - if nowControl == nil then - return - end - - for _, key in ipairs(ControlPressKeys) do - local lastPressed = lastControl[key] - local nowPressed = nowControl[key] - - -- on_press - if not lastPressed and nowPressed then - Events:call('control.on_press', key) - - controlTimer[key] = 0 - - -- on_release - elseif lastPressed and not nowPressed then - Events:call('control.on_release', key, controlTimer[key]) - - controlTimer[key] = 0 - - -- on_hold - elseif lastPressed and nowPressed then - Events:call('control.on_hold', key, controlTimer[key], deltaTime) - - controlTimer[key] = controlTimer[key] + deltaTime - end - end - - --TODO: add player movement callbacks - - --TODO: add mouse movement callbacks on dir - - playersLastControl[playerName] = nowControl - playersControlTimer[playerName] = controlTimer -end) - -return Control diff --git a/mods/CORE/Controls/init.lua b/mods/CORE/Controls/init.lua new file mode 100644 index 0000000..eaed8ac --- /dev/null +++ b/mods/CORE/Controls/init.lua @@ -0,0 +1,4 @@ +local modInfo = Mod.getInfo('smc__core__controls') +local require = modInfo.require + +return require('Controls') diff --git a/mods/CORE/Control/mod.conf b/mods/CORE/Controls/mod.conf similarity index 57% rename from mods/CORE/Control/mod.conf rename to mods/CORE/Controls/mod.conf index 3d14d9c..668521b 100644 --- a/mods/CORE/Control/mod.conf +++ b/mods/CORE/Controls/mod.conf @@ -1,2 +1,2 @@ -name = smc__core__control +name = smc__core__controls depends = smc__system__system_loader diff --git a/mods/CORE/Controls/src/Controls.lua b/mods/CORE/Controls/src/Controls.lua new file mode 100644 index 0000000..d1b9e9a --- /dev/null +++ b/mods/CORE/Controls/src/Controls.lua @@ -0,0 +1,88 @@ +---@type Events.ControlsEvents +local ControlsEvents = Smc.requireEvents('ControlsEvents') + +---@class Controls +local Controls = {} + +---@alias Controls.PressKey 'up'|'down'|'left'|'right'|'jump'|'aux1'|'sneak'|'dig'|'place'|'zoom' +---@alias Controls.MovementKey 'movement_x'|'movement_y' + +---@alias Controls.PlayerControls table +---@alias Controls.PlayerControlsTimer table + +local ControlsPressKeys = { 'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom' } +local ControlsMovementKeys = { 'movement_x', 'movement_y' } + +---@type table +local playersLastControls = {} +---@type table +local playersControlsTimer = {} +---@type table +local playersWieldIndices = {} + +core.register_on_joinplayer(function(player, last_login) + local playerName = player:get_player_name() + local nowControls = player:get_player_control() + + local controlsTimer = {} + for _, key in ipairs(ControlsPressKeys) do + controlsTimer[key] = 0.0 + end + + playersLastControls[playerName] = nowControls + playersControlsTimer[playerName] = controlsTimer + playersWieldIndices[playerName] = player:get_wield_index() +end) + +core.register_on_leaveplayer(function(player, timed_out) + local playerName = player:get_player_name() + + playersLastControls[playerName] = nil + playersControlsTimer[playerName] = nil + playersWieldIndices[playerName] = nil +end) + +Smc.registerGlobalStepForEachPlayer(0, function(deltaTime, player) + local playerName = player:get_player_name() + + local lastControls = playersLastControls[playerName] + local controlsTimer = playersControlsTimer[playerName] + local currentControls = player:get_player_control() + + local lastWieldIndex = playersWieldIndices[playerName] + local currentWieldIndex = player:get_wield_index() + + -- Press keys + for _, key in ipairs(ControlsPressKeys) do + local lastPressed = lastControls[key] + local currentPressed = currentControls[key] + + -- on_press + if not lastPressed and currentPressed then + ControlsEvents:callOnPress(player, key) + controlsTimer[key] = 0 + -- on_release + elseif lastPressed and not currentPressed then + ControlsEvents:callOnRelease(player, key, controlsTimer[key]) + controlsTimer[key] = 0 + -- on_hold + elseif lastPressed and currentPressed then + ControlsEvents:callOnHold(player, key, controlsTimer[key], deltaTime) + controlsTimer[key] = controlsTimer[key] + deltaTime + end + end + + --TODO: add player movement callbacks + + -- Wield change + if lastWieldIndex ~= currentWieldIndex then + ControlsEvents:callOnWieldChange(player, currentWieldIndex, lastWieldIndex) + end + + --TODO: add mouse movement callbacks on dir + + playersLastControls[playerName] = currentControls + playersControlsTimer[playerName] = controlsTimer +end) + +return Controls diff --git a/mods/SYSTEM/Events/src/Events.lua b/mods/SYSTEM/Events/src/Events.lua index ecfbea6..e5f7cd8 100644 --- a/mods/SYSTEM/Events/src/Events.lua +++ b/mods/SYSTEM/Events/src/Events.lua @@ -1,7 +1,8 @@ ----@alias Events.Callback fun(eventName:string, ...) +---@alias Events.Callback fun(...) ---@alias Events.Types ---- '' +--- 'ControlsEvents' + ---@class Events : Class ---@field registeredEvents table Events = Class:extend({ @@ -29,7 +30,7 @@ function Events:call(eventName, ...) end for _, func in ipairs(callbacks) do - func(eventName, ...) + func(...) end end diff --git a/mods/SYSTEM/Events/src/Events/ControlsEvents.lua b/mods/SYSTEM/Events/src/Events/ControlsEvents.lua new file mode 100644 index 0000000..f199492 --- /dev/null +++ b/mods/SYSTEM/Events/src/Events/ControlsEvents.lua @@ -0,0 +1,64 @@ +---@alias Events.ControlsEvents.EventName 'on_press'|'on_release'|'on_hold'|'on_wield_change' + +---@class Events.ControlsEvents : Events +---@field subscribe fun(self:Events.ControlsEvents, eventName:Events.ControlsEvents.EventName, callback:Events.Callback) +---@field call fun(self:Events.ControlsEvents, eventName:Events.ControlsEvents.EventName, ...) +local ControlsEvents = Events:extend() + +-- On press -- + +---@param callback fun(player:Player, controlName:Controls.PressKey) +function ControlsEvents:subscribeOnPress(callback) + self:subscribe('on_press', callback) +end + +---@param player Player +---@param controlName Controls.PressKey +function ControlsEvents:callOnPress(player, controlName) + self:call('on_press', player, controlName) +end + +-- On release -- + +---@param callback fun(player:Player, controlName:Controls.PressKey, holdTime:number) +function ControlsEvents:subscribeOnRelease(callback) + self:subscribe('on_release', callback) +end + +---@param player Player +---@param controlName Controls.PressKey +---@param holdTime number +function ControlsEvents:callOnRelease(player, controlName, holdTime) + self:call('on_release', player, controlName, holdTime) +end + +-- On hold -- + +---@param callback fun(player:Player, controlName:Controls.PressKey, holdTime:number, dtime:number) +function ControlsEvents:subscribeOnHold(callback) + self:subscribe('on_hold', callback) +end + +---@param player Player +---@param controlName Controls.PressKey +---@param holdTime number +---@param dtime number +function ControlsEvents:callOnHold(player, controlName, holdTime, dtime) + self:call('on_hold', player, controlName, holdTime, dtime) +end + +-- On wield change -- + +---@param callback fun(player:Player, currentWieldIndex:integer, lastWieldIndex:integer) +function ControlsEvents:subscribeOnWieldChange(callback) + self:subscribe('on_wield_change', callback) +end + +---@param player Player +---@param currentWieldIndex integer +---@param lastWieldIndex integer +function ControlsEvents:callOnWieldChange(player, currentWieldIndex, lastWieldIndex) + self:call('on_wield_change', player, currentWieldIndex, lastWieldIndex) +end + +return ControlsEvents diff --git a/mods/SYSTEM/SystemLoader/mod.conf b/mods/SYSTEM/SystemLoader/mod.conf index 1fc0588..fd96ef2 100644 --- a/mods/SYSTEM/SystemLoader/mod.conf +++ b/mods/SYSTEM/SystemLoader/mod.conf @@ -1,3 +1,3 @@ name = smc__system__system_loader description = Include this mod in your `depends` to ensure all necessary Libs are loaded for your mod -depends = smc__system__ensure, smc__system__math, smc__system__mod, smc__system__require, smc__system__table, smc__system__string, smc__system__utf8, smc__system__class, smc__system__helper_lib, smc__system__profiler +depends = smc__system__ensure, smc__system__math, smc__system__mod, smc__system__require, smc__system__table, smc__system__string, smc__system__utf8, smc__system__class, smc__system__helper_lib, smc__system__profiler, smc__system__events