Improve HUD updating

master
MoNTE48 2020-03-25 10:17:13 +01:00
parent 4603acd955
commit 9c24eadf4a
1 changed files with 25 additions and 30 deletions

55
hud.lua
View File

@ -19,43 +19,38 @@ minetest.register_playerstep(function(_, playernames)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if not player or not player:is_player() then return end if not player or not player:is_player() then return end
local pos = vector.round(player:get_pos()) local pos = vector.round(player:get_pos())
pos = vector.apply(pos, function(p) if minetest.is_valid_pos(pos) then
return math.max(math.min(p, 2147483), -2147483) local areaStrings = {}
end)
local areaStrings = {}
for id, area in pairs(areas:getAreasAtPos(pos)) do for id, area in pairs(areas:getAreasAtPos(pos)) do
table.insert(areaStrings, ("%s [%u] (%s%s)") areaStrings[#areaStrings+1] = ("%s [%u] (%s%s)")
:format(area.name, id, area.owner, :format(area.name, id, area.owner,
area.open and ":open" or "")) area.open and ":open" or "")
end end
for i, area in pairs(areas:getExternalHudEntries(pos)) do
local str = "" local str = ""
if area.name then str = area.name .. " " end for _, area in pairs(areas:getExternalHudEntries(pos)) do
if area.id then str = str.."["..area.id.."] " end if area.name then str = area.name .. " " end
if area.owner then str = str.."("..area.owner..")" end if area.id then str = str .. "[" .. area.id .. "] " end
table.insert(areaStrings, str) if area.owner then str = str .. "(" .. area.owner .. ")" end
end areaStrings[#areaStrings+1] = str
end
local areaString = "" local areaString = ""
if #areaStrings > 0 then if #areaStrings > 0 then
areaString = S("Areas:") areaString = S("Areas:")
areaString = areaString.."\n".. areaString = areaString .. "\n" ..
table.concat(areaStrings, "\n") table.concat(areaStrings, "\n")
end end
if not areas.hud[name] then local phud = areas.hud[name] or {}
areas.hud[name] = {} if not phud.oldAreas or phud.oldAreas ~= areaString then
hud.change_item(player, "areas", {text = areaString}) hud.change_item(player, "areas", {text = areaString})
areas.hud[name].oldAreas = areaString phud.oldAreas = areaString
return end
elseif areas.hud[name].oldAreas ~= areaString then
hud.change_item(player, "areas", {text = areaString})
areas.hud[name].oldAreas = areaString
end end
end end
end, true) -- Force this callback to run every step for smoother animations end, true) -- Force this callback to run every step to display actual information
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
areas.hud[player:get_player_name()] = nil areas.hud[player:get_player_name()] = nil