2020-04-14 13:26:33 +02:00
|
|
|
scoreboard_lib.scoreboards = {}
|
|
|
|
|
2020-04-21 17:57:09 +02:00
|
|
|
local function clone_table() end
|
|
|
|
|
2020-04-14 13:26:33 +02:00
|
|
|
Scoreboard = {
|
2020-04-21 17:57:09 +02:00
|
|
|
-- player to show the scoreboard to
|
|
|
|
player_name = "",
|
2020-04-21 23:01:14 +02:00
|
|
|
-- ids of the hud of the player
|
|
|
|
hud_id = {},
|
2020-04-14 13:26:33 +02:00
|
|
|
-- 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},
|
2020-04-21 17:57:09 +02:00
|
|
|
offset = {x = 0, y = 0},
|
2020-04-14 13:26:33 +02:00
|
|
|
number = 0xFFFFFF,
|
|
|
|
text = "Default"
|
2020-04-21 17:57:09 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sub_txt_elems = {},
|
|
|
|
sub_img_elems = {}
|
2020-04-14 13:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--[[
|
|
|
|
{
|
|
|
|
position = { x = 1, y = 0.5 },
|
|
|
|
alignment = { x = -1, y = 0 },
|
|
|
|
text = "Default",
|
|
|
|
text_color = 0xFFFFFF,
|
2020-04-21 17:57:09 +02:00
|
|
|
player = {}
|
2020-04-14 13:26:33 +02:00
|
|
|
}
|
|
|
|
--]]
|
|
|
|
function Scoreboard:new(def)
|
|
|
|
local scoreboard = {}
|
|
|
|
|
|
|
|
setmetatable(scoreboard, self)
|
|
|
|
self.__index = self
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
if def.position then
|
2020-04-14 13:26:33 +02:00
|
|
|
scoreboard.background_def.position = def.position
|
|
|
|
scoreboard.text_def.position = def.position
|
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
if def.bg_scale then
|
|
|
|
scoreboard.background_def.scale = def.bg_scale
|
|
|
|
end
|
|
|
|
|
|
|
|
if def.alignment then
|
2020-04-14 13:26:33 +02:00
|
|
|
scoreboard.background_def.alignment = def.alignment
|
2020-04-21 17:57:09 +02:00
|
|
|
if def.text_alignment then
|
|
|
|
scoreboard.text_def.alignment = def.text_alignment
|
|
|
|
else
|
|
|
|
scoreboard.text_def.alignment = def.alignment
|
|
|
|
end
|
2020-04-14 13:26:33 +02:00
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
if def.text then
|
2020-04-14 13:26:33 +02:00
|
|
|
scoreboard.text_def.text = def.text
|
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
if def.text_color then
|
2020-04-14 13:26:33 +02:00
|
|
|
scoreboard.text_def.number = def.text_color
|
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
if def.player then
|
|
|
|
scoreboard.player_name = def.player
|
|
|
|
end
|
|
|
|
|
|
|
|
if def.sub_txt_elems ~= nil then
|
|
|
|
for name, elem in pairs(def.sub_txt_elems) do
|
|
|
|
table.insert(scoreboard.sub_txt_elems, name)
|
|
|
|
scoreboard[name] = clone_table(scoreboard.text_def)
|
|
|
|
scoreboard[name].position = elem.position
|
|
|
|
scoreboard[name].text = elem.text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if def.sub_img_elems ~= nil then
|
|
|
|
for name, elem in pairs(def.sub_img_elems) do
|
|
|
|
table.insert(scoreboard.sub_img_elems, name)
|
|
|
|
scoreboard[name] = clone_table(scoreboard.background_def)
|
|
|
|
scoreboard[name].position = elem.position
|
|
|
|
scoreboard[name].text = elem.text
|
|
|
|
end
|
2020-04-14 13:26:33 +02:00
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
scoreboard_lib.scoreboards[def.player] = scoreboard
|
2020-04-14 13:26:33 +02:00
|
|
|
return scoreboard
|
|
|
|
end
|
|
|
|
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
|
2020-04-14 13:26:33 +02:00
|
|
|
function Scoreboard:show()
|
2020-04-21 17:57:09 +02:00
|
|
|
local player = minetest.get_player_by_name(self.player_name)
|
|
|
|
self.hud_id.bg_hud_id = player:hud_add(self.background_def)
|
|
|
|
self.hud_id.text_hud_id = player:hud_add(self.text_def)
|
|
|
|
|
|
|
|
--check for custom elements
|
|
|
|
for _, name in pairs(self.sub_txt_elems) do
|
|
|
|
self.hud_id[name] = player:hud_add(self[name])
|
|
|
|
end
|
2020-04-21 23:01:14 +02:00
|
|
|
-- check per testare
|
|
|
|
minetest.chat_send_player(self.player_name, minetest.serialize(self.hud_id))
|
2020-04-14 13:26:33 +02:00
|
|
|
end
|
|
|
|
|
2020-04-21 17:57:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
function Scoreboard:hide()
|
|
|
|
local player = minetest.get_player_by_name(self.player_name)
|
|
|
|
|
|
|
|
if (self.hud_id) then
|
|
|
|
for k, v in pairs(self.hud_id) do
|
|
|
|
player:hud_remove(self.hud_id[k])
|
2020-04-14 13:26:33 +02:00
|
|
|
end
|
2020-04-21 17:57:09 +02:00
|
|
|
end
|
2020-04-21 23:01:14 +02:00
|
|
|
-- check per testare
|
|
|
|
minetest.chat_send_player(self.player_name, minetest.serialize(self.hud_id))
|
2020-04-21 17:57:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-21 23:01:14 +02:00
|
|
|
function Scoreboard:update(def, txt_elems, img_elems)
|
|
|
|
|
|
|
|
if def ~= nil then
|
|
|
|
for k, v in pairs(def) do
|
|
|
|
self[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if txt_elems ~= nil then
|
|
|
|
|
|
|
|
for elem, _ in pairs(txt_elems) do
|
|
|
|
for k, v in pairs(txt_elems[elem]) do
|
|
|
|
self[elem][k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------
|
|
|
|
-----------------GETTERS----------------------
|
|
|
|
----------------------------------------------
|
|
|
|
|
|
|
|
function scoreboard_lib.get_scoreboard(p_name)
|
|
|
|
return scoreboard_lib.scoreboards[p_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------
|
|
|
|
---------------FUNZIONI LOCALI----------------
|
|
|
|
----------------------------------------------
|
|
|
|
|
2020-04-21 17:57:09 +02:00
|
|
|
-- code from => http://lua-users.org/wiki/CopyTable
|
|
|
|
function clone_table(orig)
|
|
|
|
local orig_type = type(orig)
|
|
|
|
local copy
|
|
|
|
if orig_type == 'table' then
|
|
|
|
copy = {}
|
|
|
|
for orig_key, orig_value in next, orig, nil do
|
|
|
|
copy[clone_table(orig_key)] = clone_table(orig_value)
|
|
|
|
end
|
|
|
|
setmetatable(copy, clone_table(getmetatable(orig)))
|
|
|
|
else -- number, string, boolean, etc
|
|
|
|
copy = orig
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|