Skip to content

Commit

Permalink
allow players to toggle control methods
Browse files Browse the repository at this point in the history
special(aux1)+sneak+right-click:
  toggle inverted up/down
sneak+right-click:
  toggle mouse/keyboard control
  server setting must allow use of both for this to work.

inverted and pro keyboard control seems to work.
TODO: mouse inverted up/down needs to be figured out yet.
  • Loading branch information
SwissalpS committed Apr 11, 2024
1 parent e248fcd commit 862e2f6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
83 changes: 75 additions & 8 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ local glider_uses = tonumber(minetest.settings:get(
local mouse_controls = minetest.settings:get_bool(
"glider.mouse_controls", true)

local keyboard_controls = minetest.settings:get_bool(
"glider.keyboard_controls", true)

assert(mouse_controls or keyboard_controls,
"Neither mouse nor keyboard controls enabled.")

local use_rockets = has_tnt and minetest.settings:get_bool(
"glider.use_rockets", true)

Expand Down Expand Up @@ -144,6 +150,16 @@ local function equip_sound(pos)
}, true)
end

local function player_controls(driver)
local meta = driver:get_meta()
local inverted = 1 == meta:get_int("glider.inv")
if mouse_controls and keyboard_controls then
return 0 == meta:get_int("glider.keyC"), inverted
else
return mouse_controls, inverted
end
end

local function rot_to_dir(rot)
return vector_new(
-math_cos(rot.x) * math_sin(rot.y),
Expand Down Expand Up @@ -280,15 +296,32 @@ local on_step = function(self, dtime, moveresult)
rot.z = -angle
else
local control = driver:get_player_control()
if control.up and not control.down then
rot.x = rot.x + dtime
elseif control.down and not control.up then
rot.x = rot.x - dtime
-- ignore if both directions are pressed
if control.up or control.down then
if inverted then
-- inverted controls
if control.up then
rot.x = rot.x + dtime
elseif control.down then
rot.x = rot.x - dtime
end
else
-- standard pilot controls: forward pushes
-- nose down, back pulls up
if control.up then
rot.x = rot.x - dtime
elseif control.down then
rot.x = rot.x + dtime
end
end
end
if control.left and not control.right then
rot.z = rot.z - 2 * dtime
elseif control.right and not control.left then
rot.z = rot.z + 2 * dtime
-- ignore if both directions are pressed
if control.left or control.right then
if control.left then
rot.z = rot.z - 2 * dtime
elseif control.right then
rot.z = rot.z + 2 * dtime
end
end

if rot.z ~= 0 then
Expand Down Expand Up @@ -388,6 +421,39 @@ local on_use = function(itemstack, driver, pt) --luacheck: no unused args
end
end

local function on_place(_, driver)
if type(driver) ~= "userdata" then
return -- Real players only
end

local meta = driver:get_meta()
local control = driver:get_player_control()

if control.aux1 and control.sneak then
-- change inverted up/down
-- read and toggle in one line
local inverted = 0 == meta:get_int("glider.inv")
meta:set_int("glider.inv", inverted and 1 or 0)

minetest.chat_send_player(driver:get_player_name(),
inverted
and "Inverted up/down activated (novice)."
or "Normal up/down activated (pro pilot).")

elseif mouse_controls and keyboard_controls
and control.sneak
then
-- toggle mouse/keyboard control
-- read and toggle in one line
local key_c = 0 == meta:get_int("glider.keyC")
meta:set_int("glider.keyC", key_c and 1 or 0)

minetest.chat_send_player(driver:get_player_name(),
key_c
and "Keyboard controls activated."
or "Mouse controls activated.")
end
end

minetest.register_entity("glider:hangglider", {
physical = true,
Expand All @@ -409,6 +475,7 @@ minetest.register_tool("glider:glider", {
description = "Delta Glider",
inventory_image = "glider_glider.png",
on_use = on_use,
on_secondary_use = on_place,
})

local mp = minetest.get_modpath("glider")
Expand Down
12 changes: 10 additions & 2 deletions settingtypes.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# When enabled the player will control the glider via looking around.
# When disabled the player will control the glider via the movement keys.
# Allow players to use mouse to control glider by looking around.
# Note: if disabled make sure keyboard_controls are enabled.
# If both are enabled, players can toggle by placing with glider
# in hand (right-click).
glider.mouse_controls (Mouse controls) bool true

# Allow players to use keyboard to control the glider.
# Note: if disabled make sure mouse_controls are enabled.
# If both are enabled, players can toggle by placing with glider
# in hand (right-click).
glider.keyboard_controls (Keyboard controls) bool true

# The number of times the glider can be used before breaking. Set to 0 for infinite uses.
glider.uses (Glider uses) int 250

Expand Down

0 comments on commit 862e2f6

Please sign in to comment.