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 17:20:50 +02:00
|
|
|
|
2020-01-01 18:06:03 +01:00
|
|
|
-- Localize the vector distance function for better performance, as it's called
|
|
|
|
-- on every step
|
|
|
|
local vector_distance = vector.distance
|
|
|
|
|
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
|
|
|
-- The texture is changed later in the code
|
|
|
|
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
|
|
|
|
|
|
|
if not player or
|
|
|
|
not minetest.is_player(player) or
|
2020-01-16 10:53:37 +01:00
|
|
|
vector_distance(player:get_pos(), gauge:get_pos()) > 3
|
2020-01-01 13:42:09 +01:00
|
|
|
then
|
2020-01-16 10:53:37 +01:00
|
|
|
gauge:remove()
|
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 = {
|
|
|
|
"health_"..tostring(hp)..".png^"..
|
|
|
|
"breath_"..tostring(breath)..".png"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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
|
|
|
local function add_gauge(player)
|
2020-01-16 10:53:37 +01:00
|
|
|
if player and minetest.is_player(player) then
|
|
|
|
local entity = minetest.add_entity(player:get_pos(), "gauges:hp_bar")
|
|
|
|
-- check for minetest_game 0.4.*
|
|
|
|
local height = minetest.get_modpath("player_api") and 19 or 9
|
2019-03-01 19:19:22 +01:00
|
|
|
|
2020-01-16 10:53:37 +01:00
|
|
|
entity:set_attach(player, "", {x=0, y=height, z=0}, {x=0, y=0, z=0})
|
|
|
|
entity:get_luaentity().wielder = player
|
|
|
|
end
|
2017-10-12 16:48:40 +02:00
|
|
|
end
|
|
|
|
|
2020-01-01 13:42:09 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(1, add_gauge, player)
|
|
|
|
end)
|