Show HUD element only if looking at rotatable node

main
copygirl 1 year ago
parent 58b7efa839
commit 52e8924a1b
  1. 19
      hud.lua
  2. 12
      rotate.lua
  3. BIN
      textures/nc_extended_rotating_hud_rotate_counter_clockwise.png

@ -4,6 +4,11 @@ local ipairs
local minetest, vector local minetest, vector
= minetest, vector = minetest, vector
local rotate = include("rotate")
local TEX_ROTATE_CLOCKWISE = "nc_extended_rotating_hud_rotate_clockwise.png"
local TEX_ROTATE_COUNTER_CLOCKWISE = "nc_extended_rotating_hud_rotate_counter_clockwise.png"
local player_huds = {} local player_huds = {}
local function do_player_rotating_checks(player) local function do_player_rotating_checks(player)
@ -11,6 +16,7 @@ local function do_player_rotating_checks(player)
local hud_index = player_huds[name] local hud_index = player_huds[name]
if hud_index then if hud_index then
-- FIXME: Only remove / update HUD elements when necessary.
player:hud_remove(hud_index) player:hud_remove(hud_index)
player_huds[name] = nil player_huds[name] = nil
end end
@ -27,13 +33,20 @@ local function do_player_rotating_checks(player)
local node = nil local node = nil
local ray = minetest.raycast(eye_pos, eye_pos + look_dir * reach, false, false) local ray = minetest.raycast(eye_pos, eye_pos + look_dir * reach, false, false)
for pointed_thing in ray do node = pointed_thing end for pointed_thing in ray do
if pointed_thing.type == "node" then
node = minetest.get_node(pointed_thing.under)
break
end
end
if node then if node and rotate.is_rotatable(node) then
local is_sneaking = player:get_player_control().sneak
local texture = is_sneaking and TEX_ROTATE_COUNTER_CLOCKWISE or TEX_ROTATE_CLOCKWISE
hud_index = player:hud_add({ hud_index = player:hud_add({
hud_elem_type = "image", hud_elem_type = "image",
name = "rotation hint", name = "rotation hint",
text = "nc_extended_rotating_hud_rotate_clockwise.png", text = texture,
position = { x = 0.5, y = 0.5 }, position = { x = 0.5, y = 0.5 },
offset = { x = 0, y = 0 }, offset = { x = 0, y = 0 },
alignment = { x = 0, y = 0 }, alignment = { x = 0, y = 0 },

@ -39,13 +39,19 @@ for up_index, up in ipairs(AXIS_LOOKUP) do
end end
end end
-- Returns whether the specified `node` is rotatable by this mod.
-- TODO: Register such that only certain nodes may be rotated.
local function is_rotatable(node)
local def = minetest.registered_nodes[node.name]
return def.paramtype2 == "facedir"
end
rotate.is_rotatable = is_rotatable
-- Rotates `node` at the specified `pos` around `axis` by `degrees` counter-clockwise. -- Rotates `node` at the specified `pos` around `axis` by `degrees` counter-clockwise.
local function rotate_node(pos, node, axis, degrees) local function rotate_node(pos, node, axis, degrees)
if degrees % 90 ~= 0 then error("degrees must be divisible by 90") end if degrees % 90 ~= 0 then error("degrees must be divisible by 90") end
if axis_vector_to_index(axis) == nil then error("axis must be an axis vector") end if axis_vector_to_index(axis) == nil then error("axis must be an axis vector") end
if not is_rotatable(node) then error("node is not rotatable") end
local def = minetest.registered_nodes[node.name]
if def.paramtype2 ~= "facedir" then error("node's paramtype2 is not 'facedir'") end
local up = AXIS_LOOKUP[1 + math.floor(node.param2 / 4)] local up = AXIS_LOOKUP[1 + math.floor(node.param2 / 4)]
local back = minetest.facedir_to_dir(node.param2) local back = minetest.facedir_to_dir(node.param2)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

Loading…
Cancel
Save