davvero un sacco di cambiamenti: per player, sottoHUD, funzione hide()

master
Marco 2020-04-21 17:57:09 +02:00
parent 2abfca12bd
commit 3e8ca771f2
4 changed files with 97 additions and 44 deletions

140
api.lua
View File

@ -1,10 +1,12 @@
scoreboard_lib.scoreboards = {}
local function clone_table() end
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 = {},
-- player to show the scoreboard to
player_name = "",
-- id of the hud of the player
hud_id = {bg_hud_id = nil, text_hud_id = nil},
-- because the scoreboard is composed by a background and a text we need to
-- define the two HUD to use later
background_def = {
@ -18,10 +20,13 @@ Scoreboard = {
hud_elem_type = "text",
position = {x = 1, y = 0.5},
alignment = {x = 1, y = 1},
offset = {x = -100, y = -70},
offset = {x = 0, y = 0},
number = 0xFFFFFF,
text = "Default"
}
},
sub_txt_elems = {},
sub_img_elems = {}
}
--[[
@ -30,7 +35,7 @@ position = { x = 1, y = 0.5 },
alignment = { x = -1, y = 0 },
text = "Default",
text_color = 0xFFFFFF,
players = {}
player = {}
}
--]]
function Scoreboard:new(def)
@ -38,52 +43,101 @@ function Scoreboard:new(def)
setmetatable(scoreboard, self)
self.__index = self
if (def.position) then
if def.position then
scoreboard.background_def.position = def.position
scoreboard.text_def.position = def.position
end
if (def.alignment) then
if def.bg_scale then
scoreboard.background_def.scale = def.bg_scale
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}
if def.text_alignment then
scoreboard.text_def.alignment = def.text_alignment
else
scoreboard.text_def.alignment = def.alignment
end
end
table.insert(scoreboard_lib.scoreboards, scoreboard)
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
scoreboard.hud_id = {bg_hud_id = nil, text_hud_id = nil}
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
scoreboard.hud_id[name] = nil
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
scoreboard.hud_id[name] = nil
end
end
scoreboard_lib.scoreboards[def.player] = 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
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
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
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
end
-- 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

View File

@ -3,4 +3,3 @@ scoreboard_lib = {}
scoreboard_lib.path = minetest.get_modpath(minetest.get_current_modname())
dofile(scoreboard_lib.path.."/api.lua")
dofile(scoreboard_lib.path.."/test.lua")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 99 B