Statbars: Reduce initial update interval
* Centralize HUD updating in update_builtin_statbars.
* Fixes race condition in 'on joinplayer' that causes stat bar bugs.
* Correctly scales stat bars to custom max values set by mods.
* backport eea1fdaf25
* Related to https://codeberg.org/minenux/minetest-engine-minetest/issues/33
This commit is contained in:
parent
a42c93ac8b
commit
1efec3cbf3
@ -55,12 +55,15 @@ local function initialize_builtin_statbars(player)
|
||||
local hud = hud_ids[name]
|
||||
|
||||
if player:hud_get_flags().healthbar and enable_damage then
|
||||
local number = scaleToDefault(player, "hp")
|
||||
if hud.id_healthbar == nil then
|
||||
local hud_def = table.copy(health_bar_definition)
|
||||
hud_def.number = scaleToDefault(player, "hp")
|
||||
hud_def.number = number
|
||||
hud.id_healthbar = player:hud_add(hud_def)
|
||||
else
|
||||
player:hud_change(hud.id_healthbar, "number", number)
|
||||
end
|
||||
elseif hud.id_healthbar ~= nil then
|
||||
elseif hud.id_healthbar then
|
||||
player:hud_remove(hud.id_healthbar)
|
||||
hud.id_healthbar = nil
|
||||
end
|
||||
@ -68,12 +71,15 @@ local function initialize_builtin_statbars(player)
|
||||
local breath_max = player:get_properties().breath_max
|
||||
if player:hud_get_flags().breathbar and enable_damage and
|
||||
player:get_breath() < breath_max then
|
||||
local number = 2 * scaleToDefault(player, "breath")
|
||||
if hud.id_breathbar == nil then
|
||||
local hud_def = table.copy(breath_bar_definition)
|
||||
hud_def.number = 2 * scaleToDefault(player, "breath")
|
||||
hud_def.number = number
|
||||
hud.id_breathbar = player:hud_add(hud_def)
|
||||
else
|
||||
player:hud_change(hud.id_breathbar, "number", number)
|
||||
end
|
||||
elseif hud.id_breathbar ~= nil then
|
||||
elseif hud.id_breathbar then
|
||||
player:hud_remove(hud.id_breathbar)
|
||||
hud.id_breathbar = nil
|
||||
end
|
||||
@ -87,7 +93,7 @@ local function cleanup_builtin_statbars(player)
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if name == "" then
|
||||
if name == "" or not hud_ids[name] then
|
||||
return
|
||||
end
|
||||
|
||||
@ -106,9 +112,7 @@ local function player_event_handler(player,eventname)
|
||||
if eventname == "health_changed" then
|
||||
initialize_builtin_statbars(player)
|
||||
|
||||
if hud_ids[name].id_healthbar ~= nil then
|
||||
player:hud_change(hud_ids[name].id_healthbar,
|
||||
"number", scaleToDefault(player, "hp"))
|
||||
if hud_ids[name].id_healthbar then
|
||||
return true
|
||||
end
|
||||
end
|
||||
@ -116,9 +120,7 @@ local function player_event_handler(player,eventname)
|
||||
if eventname == "breath_changed" then
|
||||
initialize_builtin_statbars(player)
|
||||
|
||||
if hud_ids[name].id_breathbar ~= nil then
|
||||
player:hud_change(hud_ids[name].id_breathbar,
|
||||
"number", 2 * scaleToDefault(player, "breath"))
|
||||
if hud_ids[name].id_breathbar then
|
||||
return true
|
||||
end
|
||||
end
|
||||
@ -144,8 +146,9 @@ function core.hud_replace_builtin(name, definition)
|
||||
|
||||
for name,ids in pairs(hud_ids) do
|
||||
local player = core.get_player_by_name(name)
|
||||
if player and hud_ids[name].id_healthbar then
|
||||
player:hud_remove(hud_ids[name].id_healthbar)
|
||||
if player and ids.id_healthbar then
|
||||
player:hud_remove(ids.id_healthbar)
|
||||
ids.id_healthbar = nil
|
||||
initialize_builtin_statbars(player)
|
||||
end
|
||||
end
|
||||
@ -157,8 +160,9 @@ function core.hud_replace_builtin(name, definition)
|
||||
|
||||
for name,ids in pairs(hud_ids) do
|
||||
local player = core.get_player_by_name(name)
|
||||
if player and hud_ids[name].id_breathbar then
|
||||
player:hud_remove(hud_ids[name].id_breathbar)
|
||||
if player and ids.id_breathbar then
|
||||
player:hud_remove(ids.id_breathbar)
|
||||
ids.id_breathbar = nil
|
||||
initialize_builtin_statbars(player)
|
||||
end
|
||||
end
|
||||
@ -168,6 +172,8 @@ function core.hud_replace_builtin(name, definition)
|
||||
return false
|
||||
end
|
||||
|
||||
core.register_on_joinplayer(initialize_builtin_statbars)
|
||||
core.after(0.1,function()
|
||||
core.register_on_joinplayer(initialize_builtin_statbars)
|
||||
end)
|
||||
core.register_on_leaveplayer(cleanup_builtin_statbars)
|
||||
core.register_playerevent(player_event_handler)
|
||||
|
Loading…
x
Reference in New Issue
Block a user