2019-03-01 19:17:13 +01:00
|
|
|
-- gauges: Adds health/breath bars above players
|
|
|
|
--
|
2020-01-01 04:11:02 +01:00
|
|
|
-- Copyright © 2014-2020 4aiman, Hugo Locurcio and contributors - MIT License
|
2019-03-01 19:17:13 +01:00
|
|
|
-- See `LICENSE.md` included in the source distribution for details.
|
2017-10-12 16:48:40 +02:00
|
|
|
|
2020-01-01 13:42:09 +01:00
|
|
|
if minetest.settings:get_bool("health_bars") == false or
|
|
|
|
not minetest.settings:get_bool("enable_damage")
|
|
|
|
then return end
|
2017-10-12 16:48:40 +02:00
|
|
|
|
2020-01-21 22:52:07 +01:00
|
|
|
-- Localize the vector distance function for better performance,
|
|
|
|
-- as it's called on every step
|
2020-01-01 18:06:03 +01:00
|
|
|
local vector_distance = vector.distance
|
2017-10-12 17:20:50 +02:00
|
|
|
|
2020-01-21 22:52:07 +01:00
|
|
|
local function add_gauge(player)
|
|
|
|
if player and player:is_player() then
|
|
|
|
local entity = minetest.add_entity(player:get_pos(), "gauges:hp_bar")
|
|
|
|
local height = 19
|
|
|
|
|
2020-02-15 16:19:22 +01:00
|
|
|
-- Check for Minetest 0.4.17 and adjust the entity height if needed
|
|
|
|
-- (The entity height offset was changed in Minetest 5.0.0.)
|
2020-01-21 22:52:07 +01:00
|
|
|
local version = tonumber(minetest.get_version().string:sub(1, 1))
|
|
|
|
if version and version < 5 then
|
|
|
|
height = 9
|
|
|
|
end
|
|
|
|
|
|
|
|
entity:set_attach(player, "", {x=0, y=height, z=0}, {x=0, y=0, z=0})
|
|
|
|
entity:get_luaentity().wielder = player
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:42:09 +01:00
|
|
|
minetest.register_entity("gauges:hp_bar", {
|
|
|
|
visual = "sprite",
|
2020-01-01 18:03:08 +01:00
|
|
|
visual_size = {x=1, y=1/16, z=1},
|
2020-01-01 13:42:09 +01:00
|
|
|
textures = {"blank.png"},
|
|
|
|
collisionbox = {0},
|
|
|
|
physical = false,
|
2019-03-01 19:19:22 +01:00
|
|
|
|
2020-01-01 13:42:09 +01:00
|
|
|
on_step = function(self)
|
|
|
|
local player = self.wielder
|
2020-01-16 10:53:37 +01:00
|
|
|
local gauge = self.object
|
2020-01-01 13:42:09 +01:00
|
|
|
|
2020-01-21 22:52:07 +01:00
|
|
|
if not player or not player:is_player() then
|
|
|
|
gauge:remove()
|
|
|
|
return
|
|
|
|
elseif vector_distance(player:get_pos(), gauge:get_pos()) > 3 then
|
2020-01-16 10:53:37 +01:00
|
|
|
gauge:remove()
|
2020-01-21 22:52:07 +01:00
|
|
|
add_gauge(player)
|
2020-01-01 13:42:09 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local hp = player:get_hp() <= 20 and player:get_hp() or 20
|
|
|
|
local breath = player:get_breath() <= 10 and player:get_breath() or 11
|
|
|
|
|
|
|
|
if self.hp ~= hp or self.breath ~= breath then
|
2020-01-16 10:53:37 +01:00
|
|
|
gauge:set_properties({
|
2020-01-01 13:42:09 +01:00
|
|
|
textures = {
|
2020-01-21 22:52:07 +01:00
|
|
|
"health_"..hp..".png^"..
|
|
|
|
"breath_"..breath..".png"
|
2020-01-01 13:42:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
self.hp = hp
|
|
|
|
self.breath = breath
|
|
|
|
end
|
2017-10-12 16:48:40 +02:00
|
|
|
end
|
2020-01-01 13:42:09 +01:00
|
|
|
})
|
2017-10-12 16:51:53 +02:00
|
|
|
|
2020-01-01 13:42:09 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(1, add_gauge, player)
|
|
|
|
end)
|
2019-05-01 18:37:42 +02:00
|
|
|
|
|
|
|
minetest.log("action", "[gauges] loaded.")
|