HUD score improvements (#656)

* Move hud_score mod to 'other' modpack

* hud_score: Reuse element when adding a new element with the same name
This commit is contained in:
ANAND 2020-09-14 07:47:19 +05:30 committed by GitHub
parent 81d7553f7c
commit 58db9e7dd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View file

@ -28,15 +28,15 @@ Example definition:
## `players` table
This is a table of tables, indexed by player names. This table holds the HUD
data of all online players. Each sub-table is a list of score tables, which
are added by `hud_score.new`.
data of all online players. Each sub-table is a list of HUD score elements,
which are added by `hud_score.new`.
```lua
local players = {
["name"] = {
[1] = <score_def>,
[2] = <score_def>,
[3] = <score_def>
[1] = <hud_score_element>,
[2] = <hud_score_element>,
[3] = <hud_score_element>
...
},
["name2"] = {

View file

@ -50,24 +50,38 @@ local function update(name)
end
function hud_score.new(name, def)
local player = minetest.get_player_by_name(name)
if not player then
return
end
-- Verify HUD score element def
if not name or not def or type(def) ~= "table" or
not def.name or not def.value or not def.color then
error("hud_score: Invalid HUD score element definition", 2)
end
local player = minetest.get_player_by_name(name)
if not player then
return
end
-- Store element expiration time in def.time
-- and append score element def to players[name]
def.time = os.time() + duration
if next_check > duration then
next_check = duration
end
table.insert(players[name], def)
-- If a HUD score element with the same name exists already,
-- reuse it instead of creating a new element
local is_new = true
for i, hud_score_spec in ipairs(players[name]) do
if hud_score_spec.name == def.name then
is_new = false
players[name][i] = def
break
end
end
if is_new then
table.insert(players[name], def)
end
-- If more than `max` active elements, mark oldest element for deletion
if #players[name] > max then