Skip to content

Commit

Permalink
Replaced Minetest's built-in health and breath bar for our own (#48)
Browse files Browse the repository at this point in the history
* Replaced Minetest's built-in health and breath bar for our own
  • Loading branch information
PureTryOut authored Feb 4, 2017
1 parent fb54e5b commit fc480fe
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
2 changes: 2 additions & 0 deletions mods/default/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
local function active_formspec(fuel_percent, item_percent)
local formspec =
"size[9,8.5]"..
"bgcolor[#080808BB;true]" ..
"background[0,0;1,1;gui_formbg.png;true]" ..
"label[4,0;Furnace]" ..
"list[current_name;src;3.5,0.5;1,1;]"..
Expand All @@ -27,6 +28,7 @@ end

local inactive_formspec =
"size[9,8.5]"..
"bgcolor[#080808BB;true]" ..
"background[0,0;1,1;gui_formbg.png;true]" ..
"label[4,0;Furnace]" ..
"list[current_name;src;3.5,0.5;1,1;]"..
Expand Down
6 changes: 6 additions & 0 deletions mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ default.LIGHT_MAX = 14
-- Load files
local default_path = minetest.get_modpath("default")

default.players = {}

minetest.register_on_joinplayer(function(player)
default.players[player:get_player_name()] = player
end)

dofile(default_path.."/functions.lua")
dofile(default_path.."/trees.lua")
dofile(default_path.."/nodes.lua")
Expand Down
1 change: 1 addition & 0 deletions mods/default/mapgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ minetest.register_alias("mapgen_dirt", "default:dirt")
minetest.register_alias("mapgen_dirt_with_grass", "default:dirt_with_grass")
minetest.register_alias("mapgen_sand", "default:sand")
minetest.register_alias("mapgen_water_source", "default:water_source")
minetest.register_alias("mapgen_river_water_source", "default:water_source")
minetest.register_alias("mapgen_lava_source", "default:lava_source")
minetest.register_alias("mapgen_gravel", "default:gravel")
minetest.register_alias("mapgen_dirt_with_snow", "default:dirt_with_snow")
Expand Down
3 changes: 3 additions & 0 deletions mods/default/nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@ minetest.register_node("default:crafting_table", {
on_rightclick = function(pos, node, clicker, itemstack)
minetest.show_formspec(clicker:get_player_name(), "default:crafting_table",
"size[9,8.5;]" ..
"bgcolor[#080808BB;true]" ..
"background[0,0;9,8.5;gui_formbg.png;true]" ..
"label[4,0;Crafting]" ..
"list[current_player;craft;2,0.5;3,3;]" ..
Expand All @@ -1602,6 +1603,7 @@ minetest.register_node("default:crafting_table", {
-- Chest
local chest_formspec =
"size[9,8.5]" ..
"bgcolor[#080808BB;true]" ..
"background[5,5;1,1;gui_formbg.png;true]" ..
"label[0,-0.1;Chest]" ..
"list[current_name;main;0,0.3;9,3;]" ..
Expand All @@ -1613,6 +1615,7 @@ local function get_locked_chest_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local formspec =
"size[9,8.5]" ..
"bgcolor[#080808BB;true]" ..
"background[5,5;1,1;gui_formbg.png;true]" ..
"label[0,-0.1;Chest]" ..
"list[nodemeta:" .. spos .. ";main;0,0.3;9,3;]" ..
Expand Down
45 changes: 45 additions & 0 deletions mods/interface/hud.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interface.createHud = function(player)
-- Hide built-in hud bars, so we can customize them to our likings instead
local hud_flags = player:hud_get_flags()
hud_flags.healthbar = false
hud_flags.breathbar = false
player:hud_set_flags(hud_flags)

local health_hud = player:hud_add({
hud_elem_type = "statbar",
position = {x = 0.5, y = 1},
size = {x = 24, y = 24},
text = "heart.png",
number = player:get_hp(),
offset = {x = -260, y = -90}
})

local breath_hud = player:hud_add({
hud_elem_type = "statbar",
direction = 1,
position = {x = 0.5, y = 1},
size = {x = 24, y = 24},
text = "bubble.png",
number = 0, --We'll set it to the proper value in the coming tick
alignment = {x = 0, y = 1},
offset = {x = 230, y = -90}
})

local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime

if timer > 0.5 then --Only update hud every half a second
timer = 0
for name, player in pairs(default.players) do
player:hud_change(health_hud, text, player:get_hp())

if player:get_breath() == 11 then
player:hud_change(breath_hud, text, 0)
else
player:hud_change(breath_hud, text, player:get_breath() * 2) --Minetest only uses 10 units for breath instead of 20
end
end
end
end)
end
6 changes: 5 additions & 1 deletion mods/interface/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ local startIndex = 0

dofile(minetest.get_modpath("interface") .. "/survival.lua")
dofile(minetest.get_modpath("interface") .. "/creative.lua")
dofile(minetest.get_modpath("interface") .. "/hud.lua")

local creative_mode = minetest.setting_getbool("creative_mode")
local damage_enabled = minetest.setting_getbool("enable_damage")

minetest.register_on_joinplayer(function(player)
player.hud_set_hotbar_itemcount(player, 9)
Expand All @@ -69,11 +71,13 @@ minetest.register_on_joinplayer(function(player)
if not creative_mode then
interface.createSurvivalInventory(player)
else
player:get_inventory():set_size("main", 9*4)
player:get_inventory():set_size("main", 9 * 4)

interface.initializeCreativeInventory(player)
interface.fillCreativeInventory(player, "building")
interface.createCreativeInventory(player, "building", 0, 0)

interface.createHud(player)
end
end)

Expand Down
15 changes: 8 additions & 7 deletions mods/interface/survival.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ interface.createSurvivalInventory = function(player)
local inventory = minetest.get_inventory({type="detached", name="survival"})

local formspec = "size[9,8.5]"..
"background[0,0;9,8.5;gui_formbg.png;true]" ..
"label[5,0.1;Crafting]" ..
"list[current_player;craft;5,0.5;2,2;]"..
"image[7,1;1,1;gui_furnace_arrow_bg.png^[transformR270]" ..
"list[current_player;craftpreview;8,1;1,1;]"..
"list[current_player;main;0,4.25;9,3;9]" ..
"list[current_player;main;0,7.50;9,1;]"
"bgcolor[#080808BB;true]" ..
"background[0,0;9,8.5;gui_formbg.png;true]" ..
"label[5,0.1;Crafting]" ..
"list[current_player;craft;5,0.5;2,2;]"..
"image[7,1;1,1;gui_furnace_arrow_bg.png^[transformR270]" ..
"list[current_player;craftpreview;8,1;1,1;]"..
"list[current_player;main;0,4.25;9,3;9]" ..
"list[current_player;main;0,7.50;9,1;]"

player:set_inventory_formspec(formspec)
end

0 comments on commit fc480fe

Please sign in to comment.