From 50ebc24860aa9943e20a356c1b50d05275be46f6 Mon Sep 17 00:00:00 2001 From: oilboi <47129783+oilboi@users.noreply.github.com> Date: Thu, 11 Jun 2020 19:00:42 -0400 Subject: [PATCH] Swap hunger and drowning bar position and make drowning bar dissapear when full --- mods/drowning/init.lua | 70 +++++++++++++++++++++++---------------- mods/hud_manager/init.lua | 10 ++++++ mods/hunger/init.lua | 4 +-- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/mods/drowning/init.lua b/mods/drowning/init.lua index 7b4c790..d97c797 100644 --- a/mods/drowning/init.lua +++ b/mods/drowning/init.lua @@ -1,4 +1,4 @@ -local minetest,vector = minetest,vector +local minetest,vector,hud_manager = minetest,vector,hud_manager local mod_storage = minetest.get_mod_storage() @@ -26,12 +26,45 @@ drowning_class.set_data = function(player,data) end if data.breath then - hud_manager.change_hud({ - player = player , - hud_name = "breath", - element = "number", - data = data.breath - }) + + if data.breath > 20 then + if hud_manager.hud_exists(player,"breath_bg") then + hud_manager.remove_hud(player,"breath_bg") + end + if hud_manager.hud_exists(player,"breath") then + hud_manager.remove_hud(player,"breath") + end + else + if not hud_manager.hud_exists(player,"breath_bg") then + hud_manager.add_hud(player,"breath_bg",{ + hud_elem_type = "statbar", + position = {x = 0.5, y = 1}, + text = "bubble_bg.png", + number = 20, + direction = 1, + size = {x = 24, y = 24}, + offset = {x = 24*10, y= -(48 + 52 + 39)}, + }) + end + if not hud_manager.hud_exists(player,"breath") then + hud_manager.add_hud(player,"breath",{ + hud_elem_type = "statbar", + position = {x = 0.5, y = 1}, + text = "bubble.png", + number = data.breath, + direction = 1, + size = {x = 24, y = 24}, + offset = {x = 24*10, y= -(48 + 52 + 39)}, + }) + end + + hud_manager.change_hud({ + player = player , + hud_name = "breath", + element = "number", + data = data.breath + }) + end end end @@ -168,25 +201,6 @@ minetest.register_on_joinplayer(function(player) drowning_class.set_data(player,data) player:hud_set_flags({breathbar=false}) - - hud_manager.add_hud(player,"breath_bg",{ - hud_elem_type = "statbar", - position = {x = 0.5, y = 1}, - text = "bubble_bg.png", - number = 20, - direction = 1, - size = {x = 24, y = 24}, - offset = {x = 24*10, y= -(48 + 24 + 39)}, - }) - hud_manager.add_hud(player,"breath",{ - hud_elem_type = "statbar", - position = {x = 0.5, y = 1}, - text = "bubble.png", - number = data.breath, - direction = 1, - size = {x = 24, y = 24}, - offset = {x = 24*10, y= -(48 + 24 + 39)}, - }) end) -- saves specific users data for when they relog @@ -279,7 +293,7 @@ drowning_class.handle_breath = function(dtime) drowning_class.ticker = drowning_class.ticker + dtime - if drowning_class.breath < 20 and drowning_class.ticker >= 0.25 then + if drowning_class.breath < 21 and drowning_class.ticker >= 0.25 then drowning_class.breath = drowning_class.breath + 2 @@ -288,7 +302,7 @@ drowning_class.handle_breath = function(dtime) drowning = 0, breath_ticker = 0, }) - elseif drowning_class.breath < 20 then + elseif drowning_class.breath < 21 then drowning_class.set_data(player,{breath_ticker = drowning_class.ticker}) else drowning_class.set_data(player,{breath_ticker = 0}) diff --git a/mods/hud_manager/init.lua b/mods/hud_manager/init.lua index dc28db7..e338f36 100644 --- a/mods/hud_manager/init.lua +++ b/mods/hud_manager/init.lua @@ -46,4 +46,14 @@ hud_manager.change_hud = function(data) if player_huds[name] and player_huds[name][data.hud_name] then data.player:hud_change(player_huds[name][data.hud_name], data.element, data.data) end +end + +-- gets if hud exists +hud_manager.hud_exists = function(player,hud_name) + local name = player:get_player_name() + if player_huds[name] and player_huds[name][hud_name] then + return(true) + else + return(false) + end end \ No newline at end of file diff --git a/mods/hunger/init.lua b/mods/hunger/init.lua index 54b3054..9b42e94 100644 --- a/mods/hunger/init.lua +++ b/mods/hunger/init.lua @@ -194,7 +194,7 @@ minetest.register_on_joinplayer(function(player) number = 20, direction = 1, size = {x = 24, y = 24}, - offset = {x = 24*10, y= -(48 + 50 + 39)}, + offset = {x = 24*10, y= -(48 + 24 + 39)}, }) hud_manager.add_hud(player,"hunger",{ hud_elem_type = "statbar", @@ -203,7 +203,7 @@ minetest.register_on_joinplayer(function(player) number = data.hunger, direction = 1, size = {x = 24, y = 24}, - offset = {x = 24*10, y= -(48 + 50 + 39)}, + offset = {x = 24*10, y= -(48 + 24 + 39)}, }) end)