Add priority to bossbars; display bossbars of closest bosses first

This commit is contained in:
Elias Fleckenstein 2021-04-09 13:35:58 +02:00
parent cbd2731e06
commit c6ffccfef5

View file

@ -36,12 +36,12 @@ end
local last_id = 0
function mcl_bossbars.add_bar(player, def)
function mcl_bossbars.add_bar(player, def, dynamic, priority)
local name = player:get_player_name()
local bars = mcl_bossbars.bars[name]
local bar = {text = def.text}
local bar = {text = def.text, priority = priority or 0}
bar.color, bar.image = get_color_info(def.color, def.percentage)
if def.dynamic then
if dynamic then
for _, other in pairs(bars) do
if not other.id and other.color == bar.color and (other.original_text or other.text) == bar.text and other.image == bar.image then
if not other.count then
@ -55,7 +55,7 @@ function mcl_bossbars.add_bar(player, def)
end
end
table.insert(bars, bar)
if not def.dynamic then
if not dynamic then
bar.raw_color = def.color
bar.id = last_id + 1
last_id = bar.id
@ -69,10 +69,11 @@ function mcl_bossbars.remove_bar(id)
mcl_bossbars.static[id] = nil
end
function mcl_bossbars.update_bar(id, def)
function mcl_bossbars.update_bar(id, def, priority)
local old = mcl_bossbars.static[id]
old.color = get_color_info(def.color or old.raw_color, def.percentage or old.percentage)
old.text = def.text or old.text
old.priority = priority or old.priority
end
function mcl_bossbars.update_boss(luaentity, name, color)
@ -81,15 +82,15 @@ function mcl_bossbars.update_boss(luaentity, name, color)
text = luaentity.nametag,
percentage = math.floor(luaentity.health / luaentity.hp_max * 100),
color = color,
dynamic = true,
}
if not bardef.text or bardef.text == "" then
bardef.text = name
end
local pos = object:get_pos()
for _, player in pairs(minetest.get_connected_players()) do
if vector.distance(pos, player:get_pos()) <= 80 then
mcl_bossbars.add_bar(player, bardef)
local d = vector.distance(pos, player:get_pos())
if d <= 80 then
mcl_bossbars.add_bar(player, bardef, true, d)
end
end
end
@ -116,6 +117,7 @@ minetest.register_globalstep(function()
local name = player:get_player_name()
local bars = mcl_bossbars.bars[name]
local huds = mcl_bossbars.huds[name]
table.sort(bars, function(a, b) return a.priority < b.priority end)
local huds_new = {}
local bars_new = {}
local i = 0