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
|
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
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
---@alias Events.Callback fun(eventName:string, ...)
|
---@alias Events.Callback fun(...)
|
||||||
|
|
||||||
---@alias Events.Types
|
---@alias Events.Types
|
||||||
--- ''
|
--- 'ControlsEvents'
|
||||||
|
|
||||||
---@class Events : Class
|
---@class Events : Class
|
||||||
---@field registeredEvents table<string, Events.Callback[]>
|
---@field registeredEvents table<string, Events.Callback[]>
|
||||||
Events = Class:extend({
|
Events = Class:extend({
|
||||||
@@ -29,7 +30,7 @@ function Events:call(eventName, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for _, func in ipairs(callbacks) do
|
for _, func in ipairs(callbacks) do
|
||||||
func(eventName, ...)
|
func(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
64
mods/SYSTEM/Events/src/Events/ControlsEvents.lua
Normal file
64
mods/SYSTEM/Events/src/Events/ControlsEvents.lua
Normal file
@@ -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
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
name = smc__system__system_loader
|
name = smc__system__system_loader
|
||||||
description = Include this mod in your `depends` to ensure all necessary Libs are loaded for your mod
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user