Camas/mods/headbars/init.lua

85 lines
2.3 KiB
Lua
Raw Normal View History

--
-- Headbars mod
-- By Kaadmy, for Pixture
--
headbars = {}
local enable_damage = minetest.settings:get_bool("enable_damage")
local enable_headbars = minetest.settings:get_bool("headbars_enable")
if enable_headbars == nil then enable_headbars = true end
local headbars_scale = tonumber(minetest.settings:get("headbars_scale")) or 1.0
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
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
end
end
2019-08-29 05:46:44 +02:00
img = img .. "^([combine:" .. (max * 8) .. "x16:0,0=blank.png:0,0=blank.png"
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
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"
end
end
img = img .. "^[makealpha:255,0,255)"
return img
end
minetest.register_entity(
"headbars:hpbar",
{
visual = "sprite",
visual_size = {x = 1 * headbars_scale, y = 0.1 * headbars_scale, z = 1},
2019-08-29 05:46:44 +02:00
textures = {headbars.get_sprite("heart.png", "blank.png", 20, 20)},
2017-10-23 18:51:56 +02:00
glow = 5,
2017-10-23 18:51:03 +02:00
physical = false,
2019-08-31 15:23:39 +02:00
pointable = false,
on_step = function(self, dtime)
2017-05-12 01:05:36 +02:00
local ent = self.wielder
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
2017-05-12 01:05:36 +02:00
local hp = ent:get_hp()
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,
})
function headbars.attach_hpbar(to)
2017-05-12 01:05:36 +02:00
if not enable_damage then return end
if not enable_headbars then return end
local pos = to:get_pos()
local bar = minetest.add_entity(pos, "headbars:hpbar")
if bar == nil then return end
2017-05-12 01:05:36 +02:00
-- local attach_pos = {x = 0, y = 0, z = 0}
local attach_pos = {x = 0, y = 9, z = 0}
bar:set_attach(to, "", attach_pos, {x = 0, y = 0, z = 0})
bar = bar:get_luaentity()
bar.wielder = to
end
minetest.register_on_joinplayer(headbars.attach_hpbar)
2017-05-12 01:05:36 +02:00
default.log("mod:headbars", "loaded")