2019-03-01 19:17:13 +01:00
|
|
|
-- gauges: Adds health/breath bars above players
|
|
|
|
--
|
|
|
|
-- Copyright © 2014-2019 4aiman, Hugo Locurcio and contributors - CC0 1.0 Universal
|
|
|
|
-- See `LICENSE.md` included in the source distribution for details.
|
2017-10-12 16:48:40 +02:00
|
|
|
|
|
|
|
local hp_bar = {
|
|
|
|
physical = false,
|
|
|
|
collisionbox = {x = 0, y = 0, z = 0},
|
|
|
|
visual = "sprite",
|
2019-03-01 19:17:13 +01:00
|
|
|
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
|
2017-10-12 16:48:40 +02:00
|
|
|
wielder = nil,
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:20:50 +02:00
|
|
|
function vector.sqdist(a, b)
|
|
|
|
local dx = a.x - b.x
|
|
|
|
local dy = a.y - b.y
|
|
|
|
local dz = a.z - b.z
|
2019-03-01 19:19:22 +01:00
|
|
|
return dx * dx + dy * dy + dz * dz
|
2017-10-12 17:20:50 +02:00
|
|
|
end
|
|
|
|
|
2017-10-12 16:48:40 +02:00
|
|
|
function hp_bar:on_step(dtime)
|
2017-10-12 16:57:00 +02:00
|
|
|
local wielder = self.wielder and minetest.get_player_by_name(self.wielder)
|
2019-03-01 19:19:22 +01:00
|
|
|
|
|
|
|
if
|
|
|
|
wielder == nil or
|
|
|
|
vector.sqdist(wielder:get_pos(), self.object:get_pos()) > 3
|
|
|
|
then
|
2017-10-12 16:48:40 +02:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2017-10-12 16:51:53 +02:00
|
|
|
|
|
|
|
local hp = wielder:get_hp()
|
|
|
|
local breath = wielder:get_breath()
|
2019-03-01 19:19:22 +01:00
|
|
|
|
2017-10-12 16:51:53 +02:00
|
|
|
self.object:set_properties({
|
|
|
|
textures = {
|
|
|
|
"health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png",
|
|
|
|
},
|
|
|
|
})
|
2017-10-12 16:48:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_entity("gauges:hp_bar", hp_bar)
|
|
|
|
|
2017-10-12 17:20:50 +02:00
|
|
|
local function add_HP_gauge(name)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
2017-10-12 16:51:53 +02:00
|
|
|
local pos = player:get_pos()
|
|
|
|
local ent = minetest.add_entity(pos, "gauges:hp_bar")
|
2019-03-01 19:19:22 +01:00
|
|
|
|
2017-10-12 16:51:53 +02:00
|
|
|
if ent ~= nil then
|
|
|
|
ent:set_attach(player, "", {x = 0, y = 10, z = 0}, {x = 0, y = 0, z = 0})
|
|
|
|
ent = ent:get_luaentity()
|
2017-10-12 16:57:00 +02:00
|
|
|
ent.wielder = player:get_player_name()
|
2017-10-12 16:51:53 +02:00
|
|
|
end
|
2017-10-12 16:48:40 +02:00
|
|
|
end
|
|
|
|
|
2019-03-01 19:19:22 +01:00
|
|
|
if
|
|
|
|
minetest.setting_getbool("health_bars") ~= false and
|
|
|
|
minetest.setting_getbool("enable_damage")
|
|
|
|
then
|
2017-10-12 17:20:50 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(1, add_HP_gauge, player:get_player_name())
|
|
|
|
end)
|
2017-10-12 16:48:40 +02:00
|
|
|
end
|