forked from whosit/waypoint_compass
Show waypoint when holding a compass
This commit is contained in:
parent
e793c26f64
commit
dc151342fd
124
init.lua
124
init.lua
@ -1,47 +1,99 @@
|
||||
local player_waypoints = {}
|
||||
local update_interval = 1.31415
|
||||
|
||||
-- lua metatables? what is that? :P
|
||||
local function get_compass_meta_id(meta)
|
||||
return meta:get_string("waypoint_compass:id") ~= "" or "default_id"
|
||||
end
|
||||
|
||||
|
||||
local function get_compass_meta_name(meta)
|
||||
return meta:get_string("waypoint_compass:name") ~= "" or "destination"
|
||||
end
|
||||
|
||||
local function get_compass_meta_pos(meta)
|
||||
return {x=0, y=0, z=0}
|
||||
end
|
||||
|
||||
|
||||
minetest.register_tool("waypoint_compass:compass", {
|
||||
description = "Waypoint compass",
|
||||
short_description = "Steel Axe",
|
||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
||||
-- Display HUD waypoint on use
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not itemstack then
|
||||
return nil
|
||||
end
|
||||
local meta = itemstack:get_meta()
|
||||
local waypoint_id = meta:get_string("waypoint_compass:id")
|
||||
-- FIXME test code for checking how setting the description works
|
||||
if user and user:is_player() then
|
||||
local player = user
|
||||
local player_name = player:get_player_name()
|
||||
if (player_waypoints[player_name] and
|
||||
player_waypoints[player_name][waypoint_id]) then
|
||||
-- Waypoint is currently displayed, remove it
|
||||
local hud_id = player_waypoints[player_name][waypoint_id]
|
||||
print("hud_id remove", hud_id)
|
||||
player_waypoints[player_name][waypoint_id] = nil
|
||||
player:hud_remove(hud_id)
|
||||
else
|
||||
-- Show this waypoint
|
||||
local waypoint_name = meta:get_string("waypoint_compass:name") ~= "" or "destination"
|
||||
local hud_id = player:hud_add({
|
||||
hud_elem_type = "waypoint",
|
||||
name = waypoint_name,
|
||||
text = "m",
|
||||
precision=1,
|
||||
number = 0xFF, --0xFFFFFF,
|
||||
world_pos = {x=0, y=0, z=0},
|
||||
})
|
||||
-- store HUD elemnt id to remove it later
|
||||
if not player_waypoints[player_name] then
|
||||
player_waypoints[player_name] = {}
|
||||
end
|
||||
print("hud_id add", hud_id)
|
||||
player_waypoints[player_name][waypoint_id] = hud_id
|
||||
end
|
||||
|
||||
local meta = itemstack:get_meta()
|
||||
local pos = get_compass_meta_pos(meta)
|
||||
local new_description = string.format("%f (%f,%f,%f)", math.random(), pos.x, pos.y, pos.z)
|
||||
meta:set_string("description", new_description)
|
||||
meta:set_string("short_description", new_description)
|
||||
print("on use", new_description)
|
||||
--player:set_wielded_item(nil)
|
||||
--player:set_wielded_item(itemstack)
|
||||
return itemstack
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
|
||||
local function remove_hud_waypoint(player, player_name)
|
||||
print("hud remove")
|
||||
local hud_id = player_waypoints[player_name].hud_id
|
||||
player_waypoints[player_name] = nil
|
||||
player:hud_remove(hud_id)
|
||||
end
|
||||
|
||||
local function show_hud_waypoint(player, compass_item_meta)
|
||||
local player_name = player:get_player_name()
|
||||
-- Show this waypoint
|
||||
local waypoint_name = get_compass_meta_name(compass_item_meta)
|
||||
local hud_id = player:hud_add({
|
||||
hud_elem_type = "waypoint",
|
||||
name = waypoint_name,
|
||||
text = "m",
|
||||
precision=1,
|
||||
number = 0xFF, --0xFFFFFF,
|
||||
world_pos = get_compass_meta_pos(compass_item_meta),
|
||||
})
|
||||
-- store HUD elemnt id to remove it later
|
||||
if not player_waypoints[player_name] then
|
||||
player_waypoints[player_name] = {}
|
||||
end
|
||||
print("hud_id add", hud_id)
|
||||
player_waypoints[player_name].id = get_compass_meta_id(compass_item_meta)
|
||||
player_waypoints[player_name].hud_id = hud_id
|
||||
print("id set", player_waypoints[player_name].id)
|
||||
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_id = get_compass_meta_id(meta)
|
||||
-- remove different waypoint if it exists
|
||||
print(player_waypoints[player_name] and player_waypoints[player_name].id)
|
||||
if player_waypoints[player_name] and (player_waypoints[player_name].id ~= waypoint_id) 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
|
||||
if player_waypoints[player_name] then
|
||||
remove_hud_waypoint(player, player_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
timer = 0
|
||||
end
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user