Fix attempt for gauges disappearing

This commit is contained in:
rubenwardy 2017-10-12 16:39:15 +01:00
parent a7d2b5961f
commit d1f21b62db

View file

@ -10,34 +10,75 @@ local hp_bar = {
wielder = nil, wielder = nil,
} }
function vector.sqdist(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return dx*dx + dy*dy + dz*dz
end
function hp_bar:on_step(dtime) function hp_bar:on_step(dtime)
local wielder = self.wielder local wielder = self.wielder and minetest.get_player_by_name(self.wielder)
if wielder == nil then if wielder == nil then
self.object:remove() print("[gauges] Gauge removed as null wielder! " .. dump(self.wielder))
return
elseif minetest.env:get_player_by_name(wielder:get_player_name()) == nil then
self.object:remove() self.object:remove()
return return
end end
hp = wielder:get_hp()
breath = wielder:get_breath() if vector.sqdist(wielder:get_pos(), self.object:get_pos()) > 3 then
self.object:set_properties({textures = {"health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png",},} print("[gauges] Gauge removed as not attached! " .. dump(self.wielder))
) self.object:remove()
return
end
local hp = wielder:get_hp()
local breath = wielder:get_breath()
self.object:set_properties({
textures = {
"health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png",
},
})
end end
minetest.register_entity("gauges:hp_bar", hp_bar) minetest.register_entity("gauges:hp_bar", hp_bar)
function add_HP_gauge(pl) local function add_HP_gauge(name)
local pos = pl:getpos() local player = minetest.get_player_by_name(name)
local ent = minetest.env:add_entity(pos, "gauges:hp_bar") local pos = player:get_pos()
if ent ~= nil then local ent = minetest.add_entity(pos, "gauges:hp_bar")
ent:set_attach(pl, "", {x = 0, y = 10, z = 0}, {x = 0, y = 0, z = 0}) if ent ~= nil then
ent = ent:get_luaentity() ent:set_attach(player, "", {x = 0, y = 10, z = 0}, {x = 0, y = 0, z = 0})
ent.wielder = pl ent = ent:get_luaentity()
end ent.wielder = player:get_player_name()
end
end end
if minetest.setting_getbool("health_bars") ~= false -- “If not defined or set to true then” -- If health_bars not defined or set to true
and minetest.setting_getbool("enable_damage") then -- Health bars only display when damage is enabled. if minetest.setting_getbool("health_bars") ~= false and
minetest.register_on_joinplayer(add_HP_gauge) minetest.setting_getbool("enable_damage") then
minetest.register_on_joinplayer(function(player)
minetest.after(1, add_HP_gauge, player:get_player_name())
end)
end end
local function check_gauges()
for _, player in pairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
local found = false
local objects = minetest.get_objects_inside_radius(player:get_pos(), 1)
for _, object in pairs(objects) do
local le = object:get_luaentity()
if le and le.wielder == pname then
found = true
break
end
end
if not found then
print("Gauge not found!")
add_HP_gauge(pname)
end
end
minetest.after(9.3, check_gauges)
end
minetest.after(2, check_gauges)