Controls & Events: add controls events and refactoring code
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
local modInfo = Mod.getInfo('smc__core__control')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('Control')
|
||||
@@ -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
|
||||
4
mods/CORE/Controls/init.lua
Normal file
4
mods/CORE/Controls/init.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local modInfo = Mod.getInfo('smc__core__controls')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('Controls')
|
||||
@@ -1,2 +1,2 @@
|
||||
name = smc__core__control
|
||||
name = smc__core__controls
|
||||
depends = smc__system__system_loader
|
||||
88
mods/CORE/Controls/src/Controls.lua
Normal file
88
mods/CORE/Controls/src/Controls.lua
Normal file
@@ -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<Player.controls_key, boolean>
|
||||
---@alias Controls.PlayerControlsTimer table<Player.controls_key, number>
|
||||
|
||||
local ControlsPressKeys = { 'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom' }
|
||||
local ControlsMovementKeys = { 'movement_x', 'movement_y' }
|
||||
|
||||
---@type table<string, Controls.PlayerControls>
|
||||
local playersLastControls = {}
|
||||
---@type table<string, Controls.PlayerControlsTimer>
|
||||
local playersControlsTimer = {}
|
||||
---@type table<string, integer>
|
||||
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
|
||||
Reference in New Issue
Block a user