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:
parent
81d7553f7c
commit
58db9e7dd0
3 changed files with 25 additions and 11 deletions
|
@ -28,15 +28,15 @@ Example definition:
|
||||||
## `players` table
|
## `players` table
|
||||||
|
|
||||||
This is a table of tables, indexed by player names. This table holds the HUD
|
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
|
data of all online players. Each sub-table is a list of HUD score elements,
|
||||||
are added by `hud_score.new`.
|
which are added by `hud_score.new`.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local players = {
|
local players = {
|
||||||
["name"] = {
|
["name"] = {
|
||||||
[1] = <score_def>,
|
[1] = <hud_score_element>,
|
||||||
[2] = <score_def>,
|
[2] = <hud_score_element>,
|
||||||
[3] = <score_def>
|
[3] = <hud_score_element>
|
||||||
...
|
...
|
||||||
},
|
},
|
||||||
["name2"] = {
|
["name2"] = {
|
|
@ -50,24 +50,38 @@ local function update(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
function hud_score.new(name, def)
|
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
|
-- Verify HUD score element def
|
||||||
if not name or not def or type(def) ~= "table" or
|
if not name or not def or type(def) ~= "table" or
|
||||||
not def.name or not def.value or not def.color then
|
not def.name or not def.value or not def.color then
|
||||||
error("hud_score: Invalid HUD score element definition", 2)
|
error("hud_score: Invalid HUD score element definition", 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local player = minetest.get_player_by_name(name)
|
||||||
|
if not player then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- Store element expiration time in def.time
|
-- Store element expiration time in def.time
|
||||||
-- and append score element def to players[name]
|
-- and append score element def to players[name]
|
||||||
def.time = os.time() + duration
|
def.time = os.time() + duration
|
||||||
if next_check > duration then
|
if next_check > duration then
|
||||||
next_check = duration
|
next_check = duration
|
||||||
end
|
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 more than `max` active elements, mark oldest element for deletion
|
||||||
if #players[name] > max then
|
if #players[name] > max then
|
Loading…
Reference in a new issue