make hud update instant on using the compass

This commit is contained in:
whosit 2021-05-16 20:56:23 +03:00
parent ef4adb9613
commit 3633a0cabf

107
init.lua
View File

@ -5,7 +5,7 @@ local waypoint_color = 0xFC7D0A
local target_above = false
local point_to_objects = false -- unimplemented
local point_to_liquids = true
local compass_range = 96
local compass_range = 180
-- lua metatables? what is that? :P
-- local function get_compass_meta_id(meta)
@ -46,7 +46,7 @@ end
-- return minetest.get_position_from_hash(meta:get_int("waypoint_compass:position"))
-- end
local function set_waypoint_at_pointed_place(itemstack, user, pointed_thing)
local function set_waypoint_at_pointed_place(itemstack, pointed_thing)
if pointed_thing and pointed_thing.type == "node" then
local pointed_pos = target_above and pointed_thing.above or pointed_thing.under
local meta = itemstack:get_meta()
@ -75,31 +75,9 @@ local function raycast_crosshair(player, range)
end
minetest.register_tool(
"waypoint_compass:compass", {
description = "Waypoint compass",
short_description = "Waypoint compass",
inventory_image = "Waypoint_compass_inventory_image.png",
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
range = 2.0, -- TODO what's the good range?
on_place = function(itemstack, placer, pointed_thing)
set_waypoint_at_pointed_place(itemstack, placer, pointed_thing)
return itemstack
end,
on_secondary_use = function(itemstack, user, pointed_thing)
if pointed_thing.type == "nothing" then
if user:is_player() then
pointed_thing = raycast_crosshair(user, compass_range)
end
end
set_waypoint_at_pointed_place(itemstack, user, pointed_thing)
return itemstack
end,
})
local function remove_hud_waypoint(player, player_name)
---print("hud remove")
local function hide_hud_waypoint(player)
local player_name = player:get_player_name()
local hud_id = player_waypoints[player_name].hud_id
player_waypoints[player_name] = nil
player:hud_remove(hud_id)
@ -128,33 +106,68 @@ local function show_hud_waypoint(player, compass_item_meta)
end
-- if item is a compass, then show stored waypoint
local function update_hud_waypoint(player, itemstack)
if not player:is_player() then
return
end
local player_name = player:get_player_name()
-- player is holding compass
if (itemstack and itemstack:get_name() == "waypoint_compass:compass") then
local meta = itemstack:get_meta()
local waypoint_pos = get_compass_meta_pos(meta)
-- remove different waypoint if it exists
if player_waypoints[player_name] and
(player_waypoints[player_name].pos ~= waypoint_pos) then
hide_hud_waypoint(player)
end
-- show new waypoint
if not player_waypoints[player_name] then
show_hud_waypoint(player, meta)
end
else
-- not holding it anymore
if player_waypoints[player_name] then
hide_hud_waypoint(player)
end
end
end
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer > update_interval then
for _,player in pairs(minetest.get_connected_players()) do
local player_name = player:get_player_name()
local item = player:get_wielded_item()
-- player is holding compass
if (item and item:get_name() == "waypoint_compass:compass") then
local meta = item:get_meta()
local waypoint_pos = get_compass_meta_pos(meta)
-- remove different waypoint if it exists
if player_waypoints[player_name] and
(player_waypoints[player_name].pos ~= waypoint_pos) then
remove_hud_waypoint(player, player_name)
end
-- show new waypoint
if not player_waypoints[player_name] then
show_hud_waypoint(player, meta)
end
else
-- not holding it anymore
if player_waypoints[player_name] then
remove_hud_waypoint(player, player_name)
end
end
local itemstack = player:get_wielded_item()
update_hud_waypoint(player, itemstack)
end
timer = 0
end
end)
minetest.register_tool(
"waypoint_compass:compass", {
description = "Waypoint compass",
short_description = "Waypoint compass",
inventory_image = "Waypoint_compass_inventory_image.png",
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
range = 2.0, -- TODO what's the good range?
on_place = function(itemstack, placer, pointed_thing)
set_waypoint_at_pointed_place(itemstack, pointed_thing)
update_hud_waypoint(placer, itemstack)
return itemstack
end,
on_secondary_use = function(itemstack, user, pointed_thing)
if pointed_thing.type == "nothing" then
if user and user:is_player() then
pointed_thing = raycast_crosshair(user, compass_range)
end
end
set_waypoint_at_pointed_place(itemstack, pointed_thing)
update_hud_waypoint(user, itemstack)
return itemstack
end,
})