Fix healthbar HUD (#981)
* Fix healthbar HUD * Add texture for "gone" heart * Right-align HP bar
This commit is contained in:
parent
430ba899e4
commit
2be697e3f6
1
mods/ctf/ctf_map/ctf_map_core/maps
Submodule
1
mods/ctf/ctf_map/ctf_map_core/maps
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit fa7fa3e8f80dd82d6e98766581269817325e546b
|
40
mods/other/hpbar_hud/init.lua
Normal file
40
mods/other/hpbar_hud/init.lua
Normal 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)
|
1
mods/other/hpbar_hud/mod.conf
Normal file
1
mods/other/hpbar_hud/mod.conf
Normal file
@ -0,0 +1 @@
|
|||||||
|
name = hpbar_hud
|
BIN
mods/other/hpbar_hud/textures/heart_gone.png
Normal file
BIN
mods/other/hpbar_hud/textures/heart_gone.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 147 B |
Loading…
x
Reference in New Issue
Block a user