2015-10-04 22:40:36 +02:00
|
|
|
--
|
|
|
|
-- Headbars mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
|
|
|
headbars = {}
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
local enable_damage = minetest.setting_getbool("enable_damage")
|
2015-10-21 22:49:40 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
local enable_headbars = minetest.setting_getbool("headbars_enable")
|
2015-10-04 22:40:36 +02:00
|
|
|
if enable_headbars == nil then enable_headbars = true end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
local headbars_scale = tonumber(minetest.setting_get("headbars_scale")) or 1.0
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
function headbars.get_sprite(icon, background, max, amt)
|
|
|
|
local img = "[combine:" .. (max * 8) .. "x16:0,0=ui_null.png:0,0=ui_null.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
|
|
|
|
img = img .. "^[combine:16x16:0,0=ui_null.png:" .. (i * 16) .. ",0=" .. background
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
img = img .. "^([combine:" .. (max * 8) .. "x16:0,0=ui_null.png:0,0=ui_null.png"
|
|
|
|
|
|
|
|
for i = 0, max / 2 do
|
|
|
|
if i < (amt - 1) / 2 then
|
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=ui_null.png:" .. (i * 16) .. ",0=" .. icon
|
|
|
|
elseif i < amt / 2 then
|
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=ui_null.png:" .. (i * 16) .. ",0=" .. icon
|
|
|
|
img = img .. "^[combine:" .. (max * 8) .. "x16:0,0=ui_null.png:" .. (i * 16) .. ",0=headbars_half.png"
|
|
|
|
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},
|
|
|
|
textures = {headbars.get_sprite("heart.png", "ui_null.png", 20, 20)},
|
|
|
|
|
|
|
|
physical = false,
|
|
|
|
collisionbox = {0, 0, 0, 0, 0, 0},
|
|
|
|
|
|
|
|
on_step = function(self, dtime)
|
2017-05-12 01:05:36 +02:00
|
|
|
local ent = self.wielder
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
if ent == nil or (minetest.get_player_by_name(ent:get_player_name(0)) == nil) then
|
2017-05-12 01:05:36 +02:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2017-05-12 01:05:36 +02:00
|
|
|
local hp = ent:get_hp()
|
2015-10-04 22:40:36 +02:00
|
|
|
|
2017-05-12 01:05:36 +02:00
|
|
|
self.object:set_properties({textures = {headbars.get_sprite("heart.png", "headbars_heart_bg.png", 20, hp)}})
|
|
|
|
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
|
|
|
|
|
|
|
|
local pos = to:getpos()
|
2017-05-12 04:29:55 +02:00
|
|
|
local bar = minetest.add_entity(pos, "headbars:hpbar")
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
if bar == nil then return end
|
|
|
|
|
2017-05-12 01:05:36 +02:00
|
|
|
-- local attach_pos = {x = 0, y = 0, z = 0}
|
2015-10-20 20:44:38 +02:00
|
|
|
local attach_pos = {x = 0, y = 9, z = 0}
|
2015-10-04 22:40:36 +02:00
|
|
|
|
|
|
|
bar:set_attach(to, "", attach_pos, {x = 0, y = 0, z = 0})
|
|
|
|
bar = bar:get_luaentity()
|
|
|
|
bar.wielder = to
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_joinplayer(headbars.attach_hpbar)
|
2017-05-12 01:05:36 +02:00
|
|
|
default.log("mod:headbars", "loaded")
|