File: hud.lua

package info (click to toggle)
minetest-mod-protector 20181016-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 292 kB
  • sloc: makefile: 10
file content (61 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

local S = protector.intllib
local radius = (tonumber(minetest.setting_get("protector_radius")) or 5)
local hud = {}
local hud_timer = 0

minetest.register_globalstep(function(dtime)

	-- every 5 seconds
	hud_timer = hud_timer + dtime
	if hud_timer < 5 then
		return
	end
	hud_timer = 0

	for _, player in pairs(minetest.get_connected_players()) do

		local name = player:get_player_name()
		local pos = vector.round(player:get_pos())
		local hud_text = ""

		local protectors = minetest.find_nodes_in_area(
			{x = pos.x - radius , y = pos.y - radius , z = pos.z - radius},
			{x = pos.x + radius , y = pos.y + radius , z = pos.z + radius},
			{"protector:protect","protector:protect2"})

		if #protectors > 0 then
			local npos = protectors[1]
			local meta = minetest.get_meta(npos)
			local nodeowner = meta:get_string("owner")

			hud_text = S("Owner: @1", nodeowner)
		end

		if not hud[name] then

			hud[name] = {}

			hud[name].id = player:hud_add({
				hud_elem_type = "text",
				name = "Protector Area",
				number = 0xFFFF22,
				position = {x=0, y=0.95},
				offset = {x=8, y=-8},
				text = hud_text,
				scale = {x=200, y=60},
				alignment = {x=1, y=-1},
			})

			return

		else

			player:hud_change(hud[name].id, "text", hud_text)
		end
	end
end)

minetest.register_on_leaveplayer(function(player)
	hud[player:get_player_name()] = nil
end)