2015-11-22 00:41:38 +00:00
|
|
|
-- Adds health bars above players.
|
|
|
|
-- Code by 4aiman, textures by Calinou. Licensed under CC0.
|
|
|
|
|
2018-11-26 16:28:37 +00:00
|
|
|
gauges = {}
|
|
|
|
|
2015-11-22 00:41:38 +00:00
|
|
|
local hp_bar = {
|
|
|
|
physical = false,
|
|
|
|
collisionbox = {x = 0, y = 0, z = 0},
|
|
|
|
visual = "sprite",
|
|
|
|
textures = {"20.png"}, -- The texture is changed later in the code.
|
|
|
|
visual_size = {x = 1.5, y = 0.09375, z = 1.5}, -- Y value is (1 / 16) * 1.5.
|
|
|
|
wielder = nil,
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:39:15 +00:00
|
|
|
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
|
|
|
|
|
2015-11-22 00:41:38 +00:00
|
|
|
function hp_bar:on_step(dtime)
|
2017-10-12 15:39:15 +00:00
|
|
|
local wielder = self.wielder and minetest.get_player_by_name(self.wielder)
|
2015-11-22 00:41:38 +00:00
|
|
|
if wielder == nil then
|
2019-10-18 05:52:18 +00:00
|
|
|
minetest.log("warning", "[gauges] Gauge removed as null wielder! " .. dump(self.wielder))
|
2015-11-22 00:41:38 +00:00
|
|
|
self.object:remove()
|
2017-10-12 15:39:15 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if vector.sqdist(wielder:get_pos(), self.object:get_pos()) > 3 then
|
2019-10-18 05:52:18 +00:00
|
|
|
minetest.log("warning", "[gauges] Gauge removed as not attached! " .. dump(self.wielder))
|
2015-11-22 00:41:38 +00:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2017-10-12 15:39:15 +00:00
|
|
|
|
|
|
|
local hp = wielder:get_hp()
|
|
|
|
local breath = wielder:get_breath()
|
|
|
|
self.object:set_properties({
|
|
|
|
textures = {
|
|
|
|
"health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png",
|
|
|
|
},
|
|
|
|
})
|
2015-11-22 00:41:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_entity("gauges:hp_bar", hp_bar)
|
|
|
|
|
2018-11-26 16:28:37 +00:00
|
|
|
function gauges.add_HP_gauge(name)
|
2017-10-12 15:39:15 +00:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2017-10-12 18:20:43 +00:00
|
|
|
if player then
|
|
|
|
local pos = player:get_pos()
|
|
|
|
local ent = minetest.add_entity(pos, "gauges:hp_bar")
|
2019-02-03 04:26:02 +00:00
|
|
|
assert(ent)
|
2017-10-12 18:20:43 +00:00
|
|
|
if ent ~= nil then
|
2019-02-03 04:26:02 +00:00
|
|
|
ent:set_attach(player, "", {x = 0, y = 19, z = 0}, {x = 0, y = 0, z = 0})
|
2017-10-12 18:20:43 +00:00
|
|
|
ent = ent:get_luaentity()
|
|
|
|
ent.wielder = player:get_player_name()
|
|
|
|
end
|
2017-10-12 15:39:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If health_bars not defined or set to true
|
2018-12-30 23:08:25 +00:00
|
|
|
if minetest.settings:get_bool("health_bars") ~= false and
|
|
|
|
minetest.settings:get_bool("enable_damage") then
|
2017-10-12 15:39:15 +00:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2018-11-29 16:09:57 +00:00
|
|
|
minetest.after(2, gauges.add_HP_gauge, player:get_player_name())
|
2017-10-12 15:39:15 +00:00
|
|
|
end)
|
2015-11-22 00:41:38 +00:00
|
|
|
end
|
|
|
|
|
2018-11-26 16:28:37 +00:00
|
|
|
function gauges.check_gauges()
|
2017-10-12 15:39:15 +00:00
|
|
|
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
|
2019-10-18 05:52:18 +00:00
|
|
|
minetest.log("warning", "[gauges] Gauge not found for player " .. pname)
|
2018-11-26 16:28:37 +00:00
|
|
|
gauges.add_HP_gauge(pname)
|
2017-10-12 15:39:15 +00:00
|
|
|
end
|
|
|
|
end
|
2018-11-26 16:28:37 +00:00
|
|
|
minetest.after(9.3, gauges.check_gauges)
|
2015-11-22 00:41:38 +00:00
|
|
|
end
|
2018-11-26 16:28:37 +00:00
|
|
|
minetest.after(2, gauges.check_gauges)
|