Hi, I have a boss bar at my place and it works fine, e.g. when changing floors so that the boss bar disappears/appears.
I have a problem though, being on the same floor. If I move away from the boss - the bar disappears. If I get closer to the boss - the boss bar does not appear.
BUT
If I move away from the boss on the left - the bar disappears. If I go towards the boss on the left - then the Boss bar appears.
How to solve this so that the boss bar appears when I get closer to the boss while maintaining the appropriate functions so that the Boss Bar does not appear when I am e.g. a level higher or lower?
I have a problem though, being on the same floor. If I move away from the boss - the bar disappears. If I get closer to the boss - the boss bar does not appear.
BUT
If I move away from the boss on the left - the bar disappears. If I go towards the boss on the left - then the Boss bar appears.
How to solve this so that the boss bar appears when I get closer to the boss while maintaining the appropriate functions so that the Boss Bar does not appear when I am e.g. a level higher or lower?

LUA:
local OPCODE = 111
local window, creatureBarHP, creatureHP, creatureName = nil
local focusedBoss = 0
local lastData = nil -- przechowuje dane bossa z serwera
function init()
connect(g_game, {
onGameStart = create,
onGameEnd = destroy
})
connect(Creature, {
onHealthPercentChange = onHealthPercentChange
})
ProtocolGame.registerExtendedOpcode(OPCODE, onExtendedOpcode)
if g_game.isOnline() then
create()
end
end
function terminate()
disconnect(g_game, {
onGameStart = create,
onGameEnd = destroy
})
disconnect(Creature, {
onHealthPercentChange = onHealthPercentChange
})
ProtocolGame.unregisterExtendedOpcode(OPCODE, onExtendedOpcode)
destroy()
end
function create()
if window then return end
window = g_ui.displayUI("bossbar")
window:hide()
creatureBarHP = window:recursiveGetChildById("creatureBarHP")
creatureName = window:recursiveGetChildById("creatureName")
creatureHP = window:recursiveGetChildById("creatureHP")
-- Listener zmiany pozycji gracza (tylko piętro)
local player = g_game.getLocalPlayer()
if player then
connect(player, { onPositionChange = onPlayerPositionChange })
end
-- Listener wejścia kreatury na widok (pułapka ponownego podejścia)
connect(g_map, { onCreatureEnter = onMapCreatureEnter })
end
function destroy()
if not window then return end
local player = g_game.getLocalPlayer()
if player then
disconnect(player, { onPositionChange = onPlayerPositionChange })
end
disconnect(g_map, { onCreatureEnter = onMapCreatureEnter })
window:destroy()
window, creatureBarHP, creatureHP, creatureName = nil, nil, nil, nil
focusedBoss, lastData = 0, nil
end
function onExtendedOpcode(protocol, code, buffer)
if not g_game.isOnline() then return end
local ok, payload = pcall(function() return json.decode(buffer) end)
if not ok or not payload then
g_logger.error("[Boss Bar] niepoprawny JSON: " .. tostring(buffer))
return
end
if payload.action == "show" then
local data = payload.data
local boss = g_map.getCreatureById(data.cid)
local player = g_game.getLocalPlayer()
if boss and player and boss:getPosition().z == player:getPosition().z then
lastData = data
show(data)
else
lastData = data
end
elseif payload.action == "hide" then
hide()
end
end
function show(data)
focusedBoss = data.cid
creatureName:setText(data.name)
creatureHP:setText(data.health .. "%")
creatureBarHP:setPercent(data.health)
window:setPosition({
x = modules.game_interface.getMapPanel():getWidth()*0.5 - 200,
y = modules.game_interface.getMapPanel():getHeight()*0.08
})
window:show()
end
function hide()
window:hide()
focusedBoss = 0
end
function onHealthPercentChange(creature, health)
if creature:getId() == focusedBoss then
creatureBarHP:setPercent(health)
creatureHP:setText(health .. "%")
end
end
-- chowamy pasek tylko przy zmianie piętra
function onPlayerPositionChange(player, newPos, oldPos)
if newPos.z ~= oldPos.z and focusedBoss ~= 0 then
hide()
end
end
-- ponowne podejście: gdy boss ponownie wejdzie w zasięg widzenia
function onMapCreatureEnter(creature)
if lastData and creature:getId() == lastData.cid then
local player = g_game.getLocalPlayer()
if player and creature:getPosition().z == player:getPosition().z then
show(lastData)
end
end
end