add ability to add "measuring point" on sneak+punch

This commit is contained in:
whosit 2021-06-01 15:45:33 +03:00
parent ffbb4c4015
commit ceaa5df331

View File

@ -8,6 +8,7 @@ local POINT_TO_LIQUIDS = true
local COMPASS_RANGE = 180
local WAYPOINT_ICON = "waypoint_compass_waypoint.png"
local WAYPOINT_SCALE = {x=-1/16*9,y=-1}
local COMPASS_MEASURE_TIMEOUT = 8 -- time until "measuring point" disappears
local waypoint_lib = dofile(minetest.get_modpath("waypoint_compass") .. "/waypoint_lib.lua")
@ -364,6 +365,49 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end)
local function compass_meausure_callback(itemstack, user, __pointed_thing)
if not user or not user:is_player() then
return
end
if not user:get_player_control()["sneak"] then
return
end
local _, pointed_thing = waypoint_lib.get_pointed_position(user, COMPASS_RANGE, POINT_TO_OBJECTS, POINT_TO_LIQUIDS)
if not pointed_thing or pointed_thing.type == "nothing" then
return
end
local point_pos = nil
if pointed_thing.type == "node" then
point_pos = TARGET_ABOVE and pointed_thing.above or pointed_thing.under
else
return
end
local meta = itemstack:get_meta()
local compass_pos = get_compass_meta_pos(meta)
if not compass_pos then
return
end
local distance = vector.length(vector.subtract(compass_pos, point_pos))
local label = ("distance to waypoint: %.2fm"):format(distance)
local label_color = 0x00FF00
local icon = WAYPOINT_ICON
local icon_scale = WAYPOINT_SCALE
local icon_color = "#FFFFFF"
local waypoint = waypoint_lib.IconWaypointHUD:new(point_pos, label, label_color, icon, icon_scale, icon_color)
waypoint:show(user)
local player_name = user:get_player_name()
minetest.after(COMPASS_MEASURE_TIMEOUT, function()
local player = minetest.get_player_by_name(player_name)
if player then
waypoint:hide(player)
end
end)
end
minetest.register_tool(
"waypoint_compass:compass", {
description = DEFAULT_DESCRIPTION,
@ -373,6 +417,11 @@ minetest.register_tool(
color = "#f6d0a5",
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
range = 2.0, -- TODO what's the good range?
on_use = function(itemstack, user, pointed_thing)
compass_meausure_callback(itemstack, user, pointed_thing)
-- return nil since compass in unchanged
return nil
end,
on_place = function(itemstack, placer, pointed_thing)
return compass_use_callback(itemstack, placer, pointed_thing)
end,