scoreboard_lib.scoreboards = {} local function clone_table() end Scoreboard = { -- player to show the scoreboard to player_name = "", -- ids of the hud of the player hud_id = {}, -- 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 = 0, y = 0}, number = 0xFFFFFF, text = "Default" }, sub_img_elems = {}, sub_txt_elems = {} } --[[ { position = { x = 1, y = 0.5 }, alignment = { x = -1, y = 0 }, text = "Default", text_color = 0xFFFFFF, player = {} } --]] 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.bg_scale then scoreboard.background_def.scale = def.bg_scale end if def.alignment then scoreboard.background_def.alignment = def.alignment if def.text_alignment then scoreboard.text_def.alignment = def.text_alignment else scoreboard.text_def.alignment = def.alignment end 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.player then scoreboard.player_name = def.player end if def.sub_img_elems then local i = 1 for name, elem in pairs(def.sub_img_elems) do scoreboard.sub_img_elems[i] = name scoreboard[name] = clone_table(scoreboard.background_def) scoreboard[name].position = elem.position scoreboard[name].text = elem.text i = i +1 end end if def.sub_txt_elems then local i = 1 for name, elem in pairs(def.sub_txt_elems) do scoreboard.sub_txt_elems[i] = name scoreboard[name] = clone_table(scoreboard.text_def) scoreboard[name].position = elem.position scoreboard[name].text = elem.text i = i +1 end end scoreboard_lib.scoreboards[def.player] = scoreboard return scoreboard end function Scoreboard:show() 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_img_elems) do self.hud_id[name] = player:hud_add(self[name]) end for _, name in pairs(self.sub_txt_elems) do self.hud_id[name] = player:hud_add(self[name]) end end 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]) end end end 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---------------- ---------------------------------------------- -- 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