2015-10-04 22:40:36 +02:00
|
|
|
--
|
|
|
|
-- Headbars mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
|
|
|
headbars = {}
|
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
local enable_damage = minetest.settings:get_bool("enable_damage")
|
2015-10-21 22:49:40 +02:00
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
local enable_headbars = minetest.settings:get_bool("headbars_enable")
|
2015-10-04 22:40:36 +02:00
|
|
|
if enable_headbars == nil then enable_headbars = true end
|
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
local headbars_scale = tonumber(minetest.settings:get("headbars_scale")) or 1.0
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
function headbars.get_sprite(icon, background, max, amt)
|
2019-08-29 05:46:44 +02:00
|
|
|
local img = "[combine:" .. (max * 8) .. "x16:0,0=blank.png:0,0=blank.png"
|
2017-05-12 01:05:36 +02:00
|
|
|
|
2015-10-04 22:40:36 +02:00
|
|
|
if amt < max then
|
|
|
|
for i = 0, max / 2 do
|
2019-08-29 05:46:44 +02:00
|
|
|
img = img .. "^[combine:16x16:0,0=blank.png:" .. (i * 16) .. ",0=" .. background
|
2015-10-04 22:40:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-29 05:46:44 +02:00
|
|
|
img = img .. "^([combine:" .. (max * 8) .. "x16:0,0=blank.png:0,0=blank.png"
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
for i = 0, max / 2 do
|
|
|
|
if i < (amt - 1) / 2 then
|
2019-08-29 05:46:44 +02:00
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=blank.png:" .. (i * 16) .. ",0=" .. icon
|
2015-10-04 22:40:36 +02:00
|
|
|
elseif i < amt / 2 then
|
2019-08-29 05:46:44 +02:00
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=blank.png:" .. (i * 16) .. ",0=" .. icon
|
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=blank.png:" .. (i * 16) .. ",0=headbars_half.png"
|
2015-10-04 22:40:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
img = img .. "^[makealpha:255,0,255)"
|
|
|
|
|
|
|
|
return img
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_entity(
|
2015-10-04 22:40:36 +02:00
|
|
|
"headbars:hpbar",
|
|
|
|
{
|
|
|
|
visual = "sprite",
|
|
|
|
visual_size = {x = 1 * headbars_scale, y = 0.1 * headbars_scale, z = 1},
|
2020-01-26 16:00:16 +01:00
|
|
|
textures = {headbars.get_sprite("headbars_heart.png", "blank.png", 20, 20)},
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2017-10-23 18:51:56 +02:00
|
|
|
glow = 5,
|
2017-10-23 18:51:03 +02:00
|
|
|
|
2015-10-04 22:40:36 +02:00
|
|
|
physical = false,
|
2019-08-31 15:23:39 +02:00
|
|
|
pointable = false,
|
2019-09-15 12:46:36 +02:00
|
|
|
static_save = false,
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2019-09-15 12:46:36 +02:00
|
|
|
_wielder = nil,
|
|
|
|
|
|
|
|
on_activate = function(self, staticdata)
|
2019-09-15 12:55:39 +02:00
|
|
|
self.object:set_armor_groups({immortal=1})
|
2019-09-15 12:46:36 +02:00
|
|
|
local name = staticdata
|
|
|
|
local wielder = minetest.get_player_by_name(name)
|
|
|
|
if wielder and wielder:is_player() then
|
|
|
|
self._wielder = wielder
|
2019-09-15 13:12:13 +02:00
|
|
|
else
|
|
|
|
minetest.log("info", "[headbars] Attempted to spawn orphan HP bar entity!")
|
|
|
|
self.object:remove()
|
|
|
|
return
|
2019-09-15 12:46:36 +02:00
|
|
|
end
|
|
|
|
end,
|
2015-10-04 22:40:36 +02:00
|
|
|
on_step = function(self, dtime)
|
2019-09-15 12:46:36 +02:00
|
|
|
local ent = self._wielder
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2019-09-15 13:12:13 +02:00
|
|
|
-- Remove orphan HP bar
|
2019-09-15 12:46:36 +02:00
|
|
|
if ent == nil or (minetest.get_player_by_name(ent:get_player_name()) == nil) then
|
2017-05-12 01:05:36 +02:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2019-09-15 12:46:36 +02:00
|
|
|
if self.object:get_attach() == nil then
|
|
|
|
self.object:set_attach(ent, "", {x = 0, y = 19, z = 0}, {x = 0, y = 0, z = 0})
|
|
|
|
end
|
|
|
|
|
2017-05-12 01:05:36 +02:00
|
|
|
local hp = ent:get_hp()
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2019-09-15 13:12:13 +02:00
|
|
|
-- Update displayed hearts
|
2020-01-26 16:00:16 +01:00
|
|
|
self.object:set_properties({textures = {headbars.get_sprite("headbars_heart.png", "headbars_heart_bg.png", 20, hp)}})
|
2017-05-12 01:05:36 +02:00
|
|
|
end,
|
|
|
|
})
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
function headbars.attach_hpbar(to)
|
2017-05-12 01:05:36 +02:00
|
|
|
if not enable_damage then return end
|
2015-10-04 22:40:36 +02:00
|
|
|
if not enable_headbars then return end
|
|
|
|
|
2019-03-20 15:04:20 +01:00
|
|
|
local pos = to:get_pos()
|
2019-09-15 12:46:36 +02:00
|
|
|
local name = to:get_player_name()
|
|
|
|
local bar = minetest.add_entity(pos, "headbars:hpbar", name)
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2019-09-15 12:46:36 +02:00
|
|
|
if bar == nil then
|
|
|
|
minetest.log("error", "[headbars] HP bar failed to spawn!")
|
|
|
|
return
|
|
|
|
end
|
2015-10-04 22:40:36 +02:00
|
|
|
end
|
|
|
|
|
2019-09-15 12:46:36 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
minetest.after(1, function(player)
|
|
|
|
if player and player:is_player() then
|
|
|
|
headbars.attach_hpbar(player)
|
|
|
|
end
|
|
|
|
end, player)
|
|
|
|
end)
|
2017-05-12 01:05:36 +02:00
|
|
|
default.log("mod:headbars", "loaded")
|