Fix healthbar HUD (#981)

* Fix healthbar HUD

* Add texture for "gone" heart

* Right-align HP bar
This commit is contained in:
Lars Müller 2022-03-16 21:36:38 +01:00 committed by GitHub
parent 430ba899e4
commit 2be697e3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 0 deletions

@ -0,0 +1 @@
Subproject commit fa7fa3e8f80dd82d6e98766581269817325e546b

View File

@ -0,0 +1,40 @@
local ids = {}
local texture_res = 24 -- heart texture resolution
local function calculate_offset(hearts)
return {x = (-hearts * texture_res) - 25, y = -(48 + texture_res + 16)}
end
minetest.register_on_joinplayer(function(player)
player:hud_set_flags({healthbar = false}) -- Hide the builtin HP bar
-- Add own HP bar with the same visuals as the builtin one
ids[player:get_player_name()] = player:hud_add({
hud_elem_type = "statbar",
position = {x = 0.5, y = 1},
text = "heart.png",
text2 = "heart_gone.png",
number = minetest.PLAYER_MAX_HP_DEFAULT,
item = minetest.PLAYER_MAX_HP_DEFAULT,
direction = 0,
size = {x = texture_res, y = texture_res},
offset = calculate_offset(10),
})
end)
minetest.register_on_leaveplayer(function(player)
ids[player:get_player_name()] = nil
end)
-- HACK `register_playerevent` is not documented, but used to implement statbars by MT internally
minetest.register_playerevent(function(player, eventname)
if eventname == "health_changed" then
player:hud_change(ids[player:get_player_name()], "number", player:get_hp())
elseif eventname == "properties_changed" then
-- HP max has probably changed, update HP bar background size ("item") accordingly
local hp_max = player:get_properties().hp_max
local id = ids[player:get_player_name()]
player:hud_change(id, "item", hp_max)
player:hud_change(id, "offset", calculate_offset(hp_max / 2))
end
end)

View File

@ -0,0 +1 @@
name = hpbar_hud

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B