ctf_pvp_engine/ctf/hud.lua

122 lines
2.5 KiB
Lua

local function hudkit()
return {
players = {},
add = function(self, player, id, def)
local name = player:get_player_name()
local elements = self.players[name]
if not elements then
self.players[name] = {}
elements = self.players[name]
end
elements[id] = player:hud_add(def)
return true
end,
exists = function(self, player, id)
if not player then
return false
end
local name = player:get_player_name()
local elements = self.players[name]
if not elements or not elements[id] then
return false
end
return true
end,
change = function(self, player, id, stat, value)
if not player then
return false
end
local name = player:get_player_name()
local elements = self.players[name]
if not elements or not elements[id] then
return false
end
player:hud_change(elements[id], stat, value)
return true
end,
remove = function(self, player, id)
local name = player:get_player_name()
local elements = self.players[name]
if not elements or not elements[id] then
return false
end
player:hud_remove(elements[id])
elements[id] = nil
return true
end
}
end
ctf.hud = hudkit()
minetest.register_on_leaveplayer(function(player)
ctf.hud.players[player:get_player_name()] = nil
end)
function ctf.hud.update(player)
if not player then
return
end
local name = player:get_player_name()
local tplayer = ctf.player(name)
if not tplayer or not tplayer.team or not ctf.team(tplayer.team) then
return
end
-- Team Identifier
local color = ctf.flag_colors[ctf.team(tplayer.team).data.color]
if not color then
color = "0x000000"
end
player:set_nametag_attributes({ color = "0xFF" .. string.sub(color, 3) })
if not ctf.hud:exists(player, "ctf:hud_team") then
return ctf.hud:add(player, "ctf:hud_team", {
hud_elem_type = "text",
position = {x = 1, y = 0},
scale = {x = 100, y = 100},
text = tplayer.team,
number = color,
offset = {x=-100, y = 20}
})
else
ctf.hud:change(player, "ctf:hud_team", "text", tplayer.team)
ctf.hud:change(player, "ctf:hud_team", "number", color)
end
end
local count = 0
function ctf.hud.updateAll()
count = 0
if not ctf.setting("hud") then
return
end
local players = minetest.get_connected_players()
for i = 1, #players do
ctf.hud.update(players[i])
end
end
minetest.register_globalstep(function(delta)
count = count + delta
if count > 10 then
ctf.hud.updateAll()
end
end)