diff --git a/drowning.lua b/drowning.lua index ff2239c..8e7e7c5 100644 --- a/drowning.lua +++ b/drowning.lua @@ -4,18 +4,27 @@ local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime; - if timer >= 2 then + if timer >= 1 then + local t0 = minetest.get_us_time() for _,player in ipairs(minetest.get_connected_players()) do local name = player:get_player_name() local name, armor_inv = armor.get_valid_player(armor, player, "[spacesuit]") + local has_helmet = armor_inv:contains_item("armor", "spacesuit:helmet") local has_chestplate = armor_inv:contains_item("armor", "spacesuit:chestplate") local has_pants = armor_inv:contains_item("armor", "spacesuit:pants") local has_boots = armor_inv:contains_item("armor", "spacesuit:boots") - if has_helmet and has_chestplate and has_pants and has_boots and player:get_breath() < 10 then + local has_full_suit = has_helmet and has_chestplate and has_pants and has_boots + + local armor_list = armor_inv:get_list("armor") + + -- does the player wear a suit? + spacesuit.set_player_wearing(player, has_full_suit, has_helmet, armor_list) + + if has_full_suit and player:get_breath() < 10 then for i, stack in pairs(armor_inv:get_list("armor")) do if not stack:is_empty() then @@ -29,5 +38,11 @@ minetest.register_globalstep(function(dtime) end end timer = 0 + + local t1 = minetest.get_us_time() + local diff = t1 - t0 + if diff > 10000 then + minetest.log("warning", "[spacesuit] update took " .. diff .. " us") + end end end) diff --git a/hud.lua b/hud.lua new file mode 100644 index 0000000..e7cc94b --- /dev/null +++ b/hud.lua @@ -0,0 +1,171 @@ + +local HUD_POSITION = {x = 0.1, y = 0.2} +local O2_BAR_OFFSET = {x = 0, y = 0} +local SUITE_INCOMPLETE_OFFSET = {x = 0, y = 20} +local HUD_ALIGNMENT = {x = 1, y = 0} + +local hud = {} -- playername -> data + + +local setup_hud = function(player) + local playername = player:get_player_name() + local hud_data = {} + hud[playername] = hud_data + + hud_data.overlay = player:hud_add({ + hud_elem_type = "image", + position = {x = 0.5, y = 0.5}, + scale = { + x = -100, + y = -100 + }, + text = "spacesuit_overlay.png" + }) + + hud_data.o2_bg = player:hud_add({ + hud_elem_type = "image", + position = HUD_POSITION, + offset = O2_BAR_OFFSET, + text = "spacesuit_o2_levels_bg.png", + alignment = HUD_ALIGNMENT, + scale = {x = -80, y = 1} + }) + + hud_data.o2_fg = player:hud_add({ + hud_elem_type = "image", + position = HUD_POSITION, + offset = O2_BAR_OFFSET, + text = "spacesuit_o2_levels_fg_green.png", + alignment = HUD_ALIGNMENT, + scale = {x = 0, y = 1} + }) + + hud_data.o2_label = player:hud_add({ + hud_elem_type = "text", + position = HUD_POSITION, + offset = {x = O2_BAR_OFFSET.x - 80, y = O2_BAR_OFFSET.y}, + text = "O2-Level:", + alignment = HUD_ALIGNMENT, + scale = {x = 100, y = 100}, + number = 0x00FF00 + }) + + hud_data.o2_level = player:hud_add({ + hud_elem_type = "text", + position = {x = 1, y = HUD_POSITION.y }, + offset = {x = O2_BAR_OFFSET.x - 70, y = O2_BAR_OFFSET.y}, + text = "", + alignment = HUD_ALIGNMENT, + scale = {x = 100, y = 100}, + number = 0x00FF00 + }) + + hud_data.suit_incomplete = player:hud_add({ + hud_elem_type = "text", + position = HUD_POSITION, + offset = SUITE_INCOMPLETE_OFFSET, + text = "", + alignment = HUD_ALIGNMENT, + scale = {x = 100, y = 100}, + number = 0xFF0000 + }) + +end + +local remove_hud = function(player) + local playername = player:get_player_name() + local hud_data = hud[playername] + + player:hud_remove(hud_data.overlay) + player:hud_remove(hud_data.o2_bg) + player:hud_remove(hud_data.o2_fg) + player:hud_remove(hud_data.o2_label) + player:hud_remove(hud_data.o2_level) + player:hud_remove(hud_data.suit_incomplete) + + hud[playername] = nil +end + +local get_color = function(r,g,b) + return b + (g * 256) + (r * 256 * 256) +end + +local update_hud = function(player, has_full_suit, armor_list) + local playername = player:get_player_name() + local hud_data = hud[playername] + + if has_full_suit then + player:hud_change(hud_data.suit_incomplete, "text", "") + else + player:hud_change(hud_data.suit_incomplete, "text", "Spacesuit incomplete!") + end + + local max_wear = 0 + for _,item in pairs(armor_list) do + -- wear:0 == full, wear:65535 == empty + if item:get_name() == "spacesuit:helmet" then + max_wear = math.max(max_wear, item:get_wear()) + elseif item:get_name() == "spacesuit:chestplate" then + max_wear = math.max(max_wear, item:get_wear()) + elseif item:get_name() == "spacesuit:pants" then + max_wear = math.max(max_wear, item:get_wear()) + elseif item:get_name() == "spacesuit:boots" then + max_wear = math.max(max_wear, item:get_wear()) + end + end + + local factor_full = 1 - (max_wear / 65535) + + player:hud_change(hud_data.o2_level, "text", math.floor(factor_full * 100) .. "%") + player:hud_change(hud_data.o2_fg, "scale", { x=math.floor(factor_full * -80), y=1 }) + + local color + + if factor_full > 0.3 then + -- green + color = get_color(0,255,0) + player:hud_change(hud_data.o2_fg, "text", "spacesuit_o2_levels_fg_green.png") + + elseif factor_full > 0.1 then + -- yellow + color = get_color(255,255,0) + player:hud_change(hud_data.o2_fg, "text", "spacesuit_o2_levels_fg_yellow.png") + + else + -- red + color = get_color(255,0,0) + player:hud_change(hud_data.o2_fg, "text", "spacesuit_o2_levels_fg_red.png") + + end + + player:hud_change(hud_data.o2_label, "number", color) + player:hud_change(hud_data.o2_level, "number", color) + +end + +spacesuit.set_player_wearing = function(player, has_full_suit, has_helmet, armor_list) + local playername = player:get_player_name() + local hud_data = hud[playername] + + if hud_data and has_helmet then + -- player wears it + update_hud(player, has_full_suit, armor_list) + + elseif not hud_data and not has_helmet then + -- player does not wear it + + elseif hud_data and not has_helmet then + -- player stopped wearing + remove_hud(player) + + elseif not hud_data and has_helmet then + -- player started wearing + setup_hud(player) + minetest.after(0.1, function() + update_hud(player, has_full_suit, armor_list) + end) + + end +end + + diff --git a/init.lua b/init.lua index 0f67e8e..f86bd89 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,6 @@ spacesuit = { - armor_use = tonumber(minetest.settings:get("spacesuit.armor_use")) or 150, + armor_use = tonumber(minetest.settings:get("spacesuit.armor_use")) or 75, } @@ -8,8 +8,7 @@ local MP = minetest.get_modpath("spacesuit") dofile(MP.."/suit.lua") dofile(MP.."/crafts.lua") +dofile(MP.."/hud.lua") dofile(MP.."/drowning.lua") - - print("[OK] Spacesuit") diff --git a/readme.md b/readme.md index a9a84f5..5e99dcd 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,15 @@ +# Intro +Spacesuit for minetest -Attribution: Anonymous_moose (https://forum.minetest.net/viewtopic.php?p=185473#p185473) +# Dependencies +* 3d_armor +* vacuum? + +# Features +* Breathe in vacuum or water + +# Attributions +* Spacesuit textures: Anonymous_moose (https://forum.minetest.net/viewtopic.php?p=185473#p185473) +* textures/spacesuit_overlay.png (https://github.com/bas080/minetest_vignette) diff --git a/textures/spacesuit_o2_levels_bg.png b/textures/spacesuit_o2_levels_bg.png new file mode 100644 index 0000000..0059259 Binary files /dev/null and b/textures/spacesuit_o2_levels_bg.png differ diff --git a/textures/spacesuit_o2_levels_fg_green.png b/textures/spacesuit_o2_levels_fg_green.png new file mode 100644 index 0000000..abfa8a7 Binary files /dev/null and b/textures/spacesuit_o2_levels_fg_green.png differ diff --git a/textures/spacesuit_o2_levels_fg_red.png b/textures/spacesuit_o2_levels_fg_red.png new file mode 100644 index 0000000..556cc6c Binary files /dev/null and b/textures/spacesuit_o2_levels_fg_red.png differ diff --git a/textures/spacesuit_o2_levels_fg_yellow.png b/textures/spacesuit_o2_levels_fg_yellow.png new file mode 100644 index 0000000..b12c3be Binary files /dev/null and b/textures/spacesuit_o2_levels_fg_yellow.png differ diff --git a/textures/spacesuit_overlay.png b/textures/spacesuit_overlay.png new file mode 100644 index 0000000..9aea9dd Binary files /dev/null and b/textures/spacesuit_overlay.png differ