Improve health display lifecycle management
This commit is contained in:
parent
d6662e8094
commit
82772ce999
1 changed files with 29 additions and 9 deletions
|
|
@ -40,18 +40,17 @@ Player = {
|
|||
|
||||
e.eye_height = 1.6
|
||||
|
||||
|
||||
|
||||
e.health_display = minetest.add_entity(p:get_pos(), "display")
|
||||
e.health_display:set_attach(p, nil, vector.new(0, 17, 0))
|
||||
e:update_hp(p:get_hp())
|
||||
|
||||
return e
|
||||
end,
|
||||
update_hp = function(m, hp)
|
||||
if not (m.health_display and m.health_display:is_valid()) then
|
||||
m.health_display = minetest.add_entity(m.object:get_pos(), "display")
|
||||
local pos = m.object:get_pos()
|
||||
if not pos then return end
|
||||
m.health_display = minetest.add_entity(pos, "rgt_player:health_display")
|
||||
m.health_display:set_attach(m.object, nil, vector.new(0, 17, 0))
|
||||
m.health_display:get_luaentity().owner = m
|
||||
end
|
||||
local tx = "[combine:90x90"
|
||||
for i = math.floor(hp /2), math.floor(m.props.hp_max /2) -1 do
|
||||
|
|
@ -210,9 +209,10 @@ Player = {
|
|||
if hp < p:get_properties().hp_max then
|
||||
p:set_hp(hp +1)
|
||||
-- Make sure the health display is still loaded.
|
||||
elseif not (m.health_display and m.health_display:is_valid()) then
|
||||
m.health_display = minetest.add_entity(m.object:get_pos(), "display")
|
||||
m.health_display:set_attach(m.object, nil, vector.new(0, 17, 0))
|
||||
end
|
||||
|
||||
if not (m.health_display and m.health_display:is_valid()) then
|
||||
m:update_hp(hp)
|
||||
end
|
||||
|
||||
m.last_time = time
|
||||
|
|
@ -287,6 +287,10 @@ Player = {
|
|||
end
|
||||
p:hud_set_hotbar_image("[combine:"..(21 *slots +1).."x22"..list)
|
||||
p:hud_set_hotbar_selected_image("rgt_hotbar_selected.png")
|
||||
end,
|
||||
deinit = function(m)
|
||||
m.health_display:remove()
|
||||
rgt.players[m.name] = nil
|
||||
end
|
||||
}
|
||||
setmetatable(Player, {
|
||||
|
|
@ -320,7 +324,7 @@ minetest.register_on_joinplayer(function(p)
|
|||
end)
|
||||
|
||||
minetest.register_on_leaveplayer(function(p)
|
||||
ns.players[p:get_player_name()] = nil
|
||||
ns.players[p:get_player_name()]:deinit()
|
||||
end)
|
||||
|
||||
minetest.register_globalstep(function(time)
|
||||
|
|
@ -333,6 +337,22 @@ minetest.register_on_player_hpchange(function(p, hp_change)
|
|||
rgt.players[p:get_player_name()]:update_hp(p:get_hp() +hp_change)
|
||||
end)
|
||||
|
||||
minetest.register_entity("rgt_player:health_display", {
|
||||
initial_properties = {
|
||||
visual = "sprite",
|
||||
textures = {"blank.png"},
|
||||
static_save = false
|
||||
},
|
||||
on_detach = function(e)
|
||||
e.object:remove()
|
||||
end,
|
||||
on_deactivate = function(e)
|
||||
if e.owner then
|
||||
minetest.after(0, function() e.owner:update_hp(e.owner.object:get_hp()) end)
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
minetest.register_chatcommand("hungerbar", {
|
||||
func = function(name, args)
|
||||
local m = rgt.players[name]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue