89 lines
2.8 KiB
Lua
89 lines
2.8 KiB
Lua
|
scoreboard_lib.scoreboards = {}
|
||
|
|
||
|
Scoreboard = {
|
||
|
-- players to show the scoreboard to - key index value player
|
||
|
players = {},
|
||
|
-- table containing ids of the huds of all the players - key player value {bg_hud_id = 1, text_hud_id = 1}
|
||
|
hud_ids = {},
|
||
|
-- because the scoreboard is composed by a background and a text we need to
|
||
|
-- define the two HUD to use later
|
||
|
background_def = {
|
||
|
hud_elem_type = "image",
|
||
|
position = { x = 1, y = 0.5 },
|
||
|
scale = { x = 1, y = 1 },
|
||
|
alignment = { x = -1, y = 0 },
|
||
|
text = "scoreboard_bg.png",
|
||
|
},
|
||
|
text_def = {
|
||
|
hud_elem_type = "text",
|
||
|
position = {x = 1, y = 0.5},
|
||
|
alignment = {x = 1, y = 1},
|
||
|
offset = {x = -100, y = -70},
|
||
|
number = 0xFFFFFF,
|
||
|
text = "Default"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
--[[
|
||
|
{
|
||
|
position = { x = 1, y = 0.5 },
|
||
|
alignment = { x = -1, y = 0 },
|
||
|
text = "Default",
|
||
|
text_color = 0xFFFFFF,
|
||
|
players = {}
|
||
|
}
|
||
|
--]]
|
||
|
function Scoreboard:new(def)
|
||
|
local scoreboard = {}
|
||
|
|
||
|
setmetatable(scoreboard, self)
|
||
|
self.__index = self
|
||
|
if (def.position) then
|
||
|
scoreboard.background_def.position = def.position
|
||
|
scoreboard.text_def.position = def.position
|
||
|
end
|
||
|
if (def.alignment) then
|
||
|
scoreboard.background_def.alignment = def.alignment
|
||
|
scoreboard.text_def.alignment = def.alignment
|
||
|
end
|
||
|
if (def.text) then
|
||
|
scoreboard.text_def.text = def.text
|
||
|
end
|
||
|
if (def.text_color) then
|
||
|
scoreboard.text_def.number = def.text_color
|
||
|
end
|
||
|
if (def.players) then
|
||
|
for i, player in pairs(def.players) do
|
||
|
local player_name = player:get_player_name()
|
||
|
scoreboard.players[player_name] = player
|
||
|
scoreboard.hud_ids[player_name] = {bg_hud_id = nil, text_hud_id = nil}
|
||
|
end
|
||
|
end
|
||
|
table.insert(scoreboard_lib.scoreboards, scoreboard)
|
||
|
return scoreboard
|
||
|
end
|
||
|
|
||
|
function Scoreboard:show()
|
||
|
for player_name, player_hud_ids in pairs(self.hud_ids) do
|
||
|
local player = minetest.get_player_by_name(player_name)
|
||
|
if (player_hud_ids) then
|
||
|
if (player_hud_ids.bg_hud_id ~= nil) then
|
||
|
player:hud_remove(player_hud_ids.bg_hud_id)
|
||
|
end
|
||
|
if (player_hud_ids.text_hud_id ~= nil) then
|
||
|
player:hud_remove(player_hud_ids.text_hud_id)
|
||
|
end
|
||
|
end
|
||
|
player_hud_ids.bg_hud_id = player:hud_add(self.background_def)
|
||
|
player_hud_ids.text_hud_id = player:hud_add(self.text_def)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function Scoreboard:update_players(players)
|
||
|
for i, player in pairs(players) do
|
||
|
if (self.players[player:get_player_name()] == nil) then
|
||
|
self.players[player:get_player_name()] = player
|
||
|
self.hud_ids[player:get_player_name()] = {bg_hud_id = nil, text_hud_id = nil }
|
||
|
end
|
||
|
end
|
||
|
end
|