1
0
Fork 0
MineClone2/mods/CORE/controls/init.lua

76 lines
2.1 KiB
Lua
Raw Normal View History

2021-03-14 23:35:58 +00:00
local get_connected_players = minetest.get_connected_players
local clock = os.clock
2018-05-07 12:52:53 +00:00
controls = {}
controls.players = {}
controls.registered_on_press = {}
function controls.register_on_press(func)
controls.registered_on_press[#controls.registered_on_press+1] = func
end
controls.registered_on_release = {}
function controls.register_on_release(func)
controls.registered_on_release[#controls.registered_on_release+1] = func
end
controls.registered_on_hold = {}
function controls.register_on_hold(func)
controls.registered_on_hold[#controls.registered_on_hold+1]=func
end
2020-06-15 21:32:32 +00:00
local known_controls = {
jump=true,
right=true,
left=true,
LMB=true,
RMB=true,
sneak=true,
aux1=true,
down=true,
up=true,
}
2018-05-07 12:52:53 +00:00
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
2020-06-15 21:32:32 +00:00
controls.players[name] = {}
for cname,_ in pairs(known_controls) do
controls.players[name][cname] = { false }
end
2018-05-07 12:52:53 +00:00
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
controls.players[name] = nil
end)
minetest.register_globalstep(function(dtime)
2021-03-14 23:35:58 +00:00
for _, player in pairs(get_connected_players()) do
2018-05-07 12:52:53 +00:00
local player_name = player:get_player_name()
local player_controls = player:get_player_control()
2020-06-15 21:32:32 +00:00
if controls.players[player_name] then
2018-05-07 12:52:53 +00:00
for cname, cbool in pairs(player_controls) do
2020-06-15 21:32:32 +00:00
if known_controls[cname] == true then
2018-05-07 12:52:53 +00:00
--Press a key
if cbool==true and controls.players[player_name][cname][1]==false then
for _, func in pairs(controls.registered_on_press) do
func(player, cname)
end
2021-03-14 23:35:58 +00:00
controls.players[player_name][cname] = {true, clock()}
2018-05-07 12:52:53 +00:00
elseif cbool==true and controls.players[player_name][cname][1]==true then
for _, func in pairs(controls.registered_on_hold) do
2021-03-14 23:35:58 +00:00
func(player, cname, clock()-controls.players[player_name][cname][2])
2018-05-07 12:52:53 +00:00
end
--Release a key
elseif cbool==false and controls.players[player_name][cname][1]==true then
for _, func in pairs(controls.registered_on_release) do
2021-03-14 23:35:58 +00:00
func(player, cname, clock()-controls.players[player_name][cname][2])
2018-05-07 12:52:53 +00:00
end
controls.players[player_name][cname] = {false}
end
end
2020-06-15 21:32:32 +00:00
end
end
2018-05-07 12:52:53 +00:00
end
end)