hud.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- This is inspired by the landrush mod by Bremaweb
  2. areas.hud = {}
  3. minetest.register_globalstep(function(dtime)
  4. for _, player in pairs(minetest.get_connected_players()) do
  5. local name = player:get_player_name()
  6. local pos = vector.round(player:getpos())
  7. local areaStrings = {}
  8. for id, area in pairs(areas:getAreasAtPos(pos)) do
  9. table.insert(areaStrings, ("%s [%u] (%s%s)")
  10. :format(area.name, id, area.owner,
  11. area.open and ":open" or ""))
  12. end
  13. for i, area in pairs(areas:getExternalHudEntries(pos)) do
  14. local str = ""
  15. if area.name then str = area.name .. " " end
  16. if area.id then str = str.."["..area.id.."] " end
  17. if area.owner then str = str.."("..area.owner..")" end
  18. table.insert(areaStrings, str)
  19. end
  20. local areaString = "Areas:"
  21. if #areaStrings > 0 then
  22. areaString = areaString.."\n"..
  23. table.concat(areaStrings, "\n")
  24. end
  25. local hud = areas.hud[name]
  26. if not hud then
  27. hud = {}
  28. areas.hud[name] = hud
  29. hud.areasId = player:hud_add({
  30. hud_elem_type = "text",
  31. name = "Areas",
  32. number = 0xFFFFFF,
  33. position = {x=0, y=1},
  34. offset = {x=8, y=-8},
  35. text = areaString,
  36. scale = {x=200, y=60},
  37. alignment = {x=1, y=-1},
  38. })
  39. hud.oldAreas = areaString
  40. return
  41. elseif hud.oldAreas ~= areaString then
  42. player:hud_change(hud.areasId, "text", areaString)
  43. hud.oldAreas = areaString
  44. end
  45. end
  46. end)
  47. minetest.register_on_leaveplayer(function(player)
  48. areas.hud[player:get_player_name()] = nil
  49. end)