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

@ -1,47 +0,0 @@
# `hud_score`
`hud_score` provides an API to display HUD score elements which can be used to
display kill scores, bounty scores, etc.
## Methods
- `hud_score.new(name, score_def)`: Adds a new HUD score element to player `name`.
- `name` [string]: Player name
- `score_def` [table]: HUD score element definition. See below.
## HUD score element definition
HUD score element definition table, passed to `hud_score.new`.
Example definition:
```lua
{
name = "ctf_stats:kill_score", -- Can be any arbitrary string
color = "0x00FF00", -- Should be compatible with Minetest's HUD def
value = 50, -- The actual number to be displayed
-- Field `time` is automatically added by `hud_score.new`
-- to keep track of element expiry
}
```
## `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`.
```lua
local players = {
["name"] = {
[1] = <score_def>,
[2] = <score_def>,
[3] = <score_def>
...
},
["name2"] = {
...
},
...
}
```

View file

@ -1,115 +0,0 @@
hud_score = {}
local hud = hudkit()
local players = {}
local duration = 5
local max = 6
local next_check = 10000000
local function update(name)
local player = minetest.get_player_by_name(name)
if not player then
return
end
-- Handle all elements marked for deletion
-- and rebuild table
local temp = {}
for _, def in ipairs(players[name]) do
if def.delete then
if hud:exists(player, def.name) then
hud:remove(player, def.name)
end
else
table.insert(temp, def)
end
end
for i, def in ipairs(temp) do
-- If not the top-most element, prefix with "+ "
local text = tostring(def.value)
if i > 1 then
text = "+ " .. text
end
if hud:exists(player, def.name) then
hud:change(player, def.name, "text", text)
hud:change(player, def.name, "offset", {x = 0, y = i * 20})
else
hud:add(player, def.name, {
hud_elem_type = "text",
alignment = {x = 0, y = 0},
position = {x = 0.5, y = 0.6},
offset = {x = 0, y = i * 20},
number = tonumber(def.color),
text = text
})
end
end
players[name] = temp
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
-- 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 more than `max` active elements, mark oldest element for deletion
if #players[name] > max then
players[name][1].delete = true
end
update(name)
end
minetest.register_globalstep(function(dtime)
next_check = next_check - dtime
if next_check > 0 then
return
end
next_check = 10000000
-- Loop through HUD score elements of all players
-- and remove them if they've expired
for name, hudset in pairs(players) do
local modified = false
for i, def in pairs(hudset) do
local rem = def.time - os.time()
if rem <= 0 then
def.delete = true
modified = true
elseif rem < next_check then
next_check = rem
end
end
-- If a player's hudset was modified, update player's HUD
if modified then
update(name)
end
end
end)
minetest.register_on_joinplayer(function(player)
players[player:get_player_name()] = {}
end)
minetest.register_on_leaveplayer(function(player)
players[player:get_player_name()] = nil
end)

View file

@ -1,3 +0,0 @@
name = hud_score
description = API for displaying scores on HUD
depends = hudkit