Skip to content

Commit

Permalink
Fix breath_bar scaling; delay breath_bar hiding by one second
Browse files Browse the repository at this point in the history
PLAYER_MAX_BREATH_DEFAULT was earlier set to 11, so that 10 bubbles are shown before the breath bar disappears.

Now, PLAYER_MAX_BREATH_DEFAULT is set to 10, and the breath_bar scaling code in builtin has been tweaked to show all 10 bubbles before hiding the breath_bar
  • Loading branch information
ClobberXD committed Aug 2, 2019
1 parent 1a97d06 commit 28006bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion builtin/game/constants.lua
Expand Up @@ -24,7 +24,7 @@ core.MAP_BLOCKSIZE = 16
-- Default maximal HP of a player
core.PLAYER_MAX_HP_DEFAULT = 20
-- Default maximal breath of a player
core.PLAYER_MAX_BREATH_DEFAULT = 11
core.PLAYER_MAX_BREATH_DEFAULT = 10

-- light.h
-- Maximum value for node 'light_source' parameter
Expand Down
43 changes: 26 additions & 17 deletions builtin/game/statbars.lua
Expand Up @@ -4,34 +4,35 @@ local enable_damage = core.settings:get_bool("enable_damage")
local health_bar_definition =
{
hud_elem_type = "statbar",
position = { x=0.5, y=1 },
position = {x = 0.5, y = 1},
text = "heart.png",
number = core.PLAYER_MAX_HP_DEFAULT,
direction = 0,
size = { x=24, y=24 },
offset = { x=(-10*24)-25, y=-(48+24+16)},
size = {x = 24, y = 24},
offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)},
}

local breath_bar_definition =
{
hud_elem_type = "statbar",
position = { x=0.5, y=1 },
position = {x = 0.5, y = 1},
text = "bubble.png",
number = core.PLAYER_MAX_BREATH_DEFAULT,
direction = 0,
size = { x=24, y=24 },
offset = {x=25,y=-(48+24+16)},
size = {x = 24, y = 24},
offset = {x = 25, y= -(48 + 24 + 16)},
}

local hud_ids = {}

local function scaleToDefault(player, field)
-- Scale "hp" or "breath" to the default dimensions
local current = player["get_" .. field](player)
local nominal = core["PLAYER_MAX_".. field:upper() .. "_DEFAULT"]
local nominal = core["PLAYER_MAX_" .. field:upper() .. "_DEFAULT"]
local max_display = math.max(nominal,
math.max(player:get_properties()[field .. "_max"], current))
return current / max_display * nominal
math.max(player:get_properties()[field .. "_max"], current))

return current / max_display * nominal
end

local function update_builtin_statbars(player)
Expand All @@ -51,10 +52,11 @@ local function update_builtin_statbars(player)
local hud = hud_ids[name]

local immortal = player:get_armor_groups().immortal == 1

if flags.healthbar and enable_damage and not immortal then
local number = scaleToDefault(player, "hp")
if hud.id_healthbar == nil then
local hud_def = table.copy(health_bar_definition)
if hud.id_healthbar == nil then
local hud_def = table.copy(health_bar_definition)
hud_def.number = number
hud.id_healthbar = player:hud_add(hud_def)
else
Expand All @@ -65,19 +67,26 @@ local function update_builtin_statbars(player)
hud.id_healthbar = nil
end

local breath = player:get_breath()
local breath_max = player:get_properties().breath_max
if flags.breathbar and enable_damage and not immortal and
player:get_breath() < breath_max then
breath <= breath_max then
local number = 2 * scaleToDefault(player, "breath")
if hud.id_breathbar == nil then
local hud_def = table.copy(breath_bar_definition)
if not hud.id_breathbar and breath < breath_max then
local hud_def = table.copy(breath_bar_definition)
hud_def.number = number
hud.id_breathbar = player:hud_add(hud_def)
else
elseif hud.id_breathbar then
player:hud_change(hud.id_breathbar, "number", number)
end
elseif hud.id_breathbar then
player:hud_remove(hud.id_breathbar)
end

-- Hide the breath bar if breathbar HUD flag is set to false,
-- player is immortal, or breath == breath_max
if hud.id_breathbar and (not flags.breathbar or immortal or breath == breath_max) then
minetest.after(1, function(breath_bar)
player:hud_remove(breath_bar)
end, hud.id_breathbar)
hud.id_breathbar = nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/constants.h
Expand Up @@ -93,7 +93,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define PLAYER_MAX_HP_DEFAULT 20

// Default maximal breath of a player
#define PLAYER_MAX_BREATH_DEFAULT 11
#define PLAYER_MAX_BREATH_DEFAULT 10

// Number of different files to try to save a player to if the first fails
// (because of a case-insensitive filesystem)
Expand Down

0 comments on commit 28006bd

Please sign in to comment.