Add HUD to show worn armor

This commit is contained in:
Zughy 2024-06-19 01:41:27 +00:00 committed by Giov4
parent 8bb21de099
commit 003ff26daa
5 changed files with 104 additions and 9 deletions

View File

@ -23,7 +23,7 @@ arena_lib.register_minigame("skywars", {
},
sounds = {
eliminate = false,
eliminate = false,
},
temp_properties = {
@ -68,4 +68,4 @@ dofile(minetest.get_modpath("skywars") .. "/src/_arena_lib/arena_callbacks.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_kits/formspec.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_kits/kit_items.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_player/auto_equip_items.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_player/damage_tracker.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_player/damage_tracker.lua")

View File

@ -9,6 +9,7 @@ local add_node = minetest.add_node
local get_node = minetest.get_node
local remove_node = minetest.remove_node
local S = skywars.T
local is_3d_armor_enabled = minetest.get_modpath("3d_armor")
minetest.register_on_joinplayer(function(player)
@ -29,6 +30,10 @@ arena_lib.on_load("skywars", function(arena)
create_barrier_cage(player)
player:get_inventory():add_item("main", "skywars:kit_selector")
if is_3d_armor_enabled then
skywars.HUD_armor_create(player)
end
end
arena_lib.HUD_send_msg_all(

View File

@ -0,0 +1,77 @@
local saved_huds = {} -- id = hud
function skywars.HUD_armor_create(player)
local pl_name = player:get_player_name()
if saved_huds[pl_name] then return end
local x_pos = 0.5
local y_pos = 1
local scale = 1.6
local x_off = -32 * player:hud_get_hotbar_itemcount()
local y_off = -20
local head = player:hud_add({
hud_elem_type = "image",
position = {x = x_pos, y = y_pos},
offset = {x = x_off -25, y = y_off - 25},
text = "3d_armor_inv_helmet_bronze.png^[multiply:#000000",
alignment = {x = 1},
scale = {x = scale, y = scale},
})
local torso = player:hud_add({
hud_elem_type = "image",
position = {x = x_pos, y = y_pos},
offset = {x = x_off, y = y_off - 25},
text = "3d_armor_inv_chestplate_bronze.png^[multiply:#000000",
alignment = {x = 1},
scale = {x = scale, y = scale},
})
local legs = player:hud_add({
hud_elem_type = "image",
position = {x = x_pos, y = y_pos},
offset = {x = x_off -25, y = y_off},
text = "3d_armor_inv_leggings_bronze.png^[multiply:#000000",
alignment = {x = 1},
scale = {x = scale, y = scale},
})
local feet = player:hud_add({
hud_elem_type = "image",
position = {x = x_pos, y = y_pos},
offset = {x = x_off, y = y_off},
text = "3d_armor_inv_boots_bronze.png^[multiply:#000000",
alignment = {x = 1},
scale = {x = scale, y = scale},
})
saved_huds[pl_name] = { head = head, torso = torso, legs = legs, feet = feet}
end
function skywars.HUD_armor_remove(player)
local pl_name = player:get_player_name()
if not saved_huds[pl_name] then return end
for _, id in pairs(saved_huds[pl_name]) do
player:hud_remove(id)
end
saved_huds[pl_name] = nil
end
function skywars.HUD_armor_update(player, stack)
local type = armor:get_element(stack:get_name())
local txtr = stack:get_definition().inventory_image
-- there's no way they can simply unequip a piece at the moment, so I don't run further checks
player:hud_change(saved_huds[player:get_player_name()][type], "text", txtr)
end

View File

@ -1,5 +1,5 @@
local function get_armor_importance() end
local on_punch = minetest.registered_entities["__builtin:item"].on_punch
local on_punch = minetest.registered_entities["__builtin:item"].on_punch
function skywars.auto_equip_armor(player, armor_itemstack)
@ -10,21 +10,24 @@ function skywars.auto_equip_armor(player, armor_itemstack)
-- A table containing pairs of [armor_element : string] = armor_name : string.
local player_armor_elements = armor:get_weared_armor_elements(player)
local equipped_armor = player_armor_elements[armor_element]
local inventory = player:get_inventory()
if equipped_armor then
local armor_importance = get_armor_importance(armor_name)
local weared_armor_importance = get_armor_importance(equipped_armor)
if not armor_importance or not weared_armor_importance then return false end
-- Returning if the just taken armor is worse or as good as the equipped one.
if armor_importance <= weared_armor_importance then return end
end
if arena_lib.is_player_in_arena(player:get_player_name()) then
skywars.HUD_armor_update(player, armor_itemstack)
end
armor:equip(player, armor_itemstack)
return true
return true
end
@ -40,8 +43,8 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
if not armor_itemstack then return end
if skywars.auto_equip_armor(player, armor_itemstack) then
minetest.after(0, function()
inventory:remove_item("main", ItemStack(armor_name))
minetest.after(0, function()
inventory:remove_item("main", ItemStack(armor_name))
end)
end
end

View File

@ -9,9 +9,19 @@ end)
function skywars.remove_armor(player)
if minetest.get_modpath("3d_armor") then
armor:remove_all(player)
skywars.HUD_armor_remove(player)
end
end
dofile(minetest.get_modpath("skywars") .. "/src/_compatible_mods/3d_armor/auto_equip_armors.lua")
armor:register_on_equip(function(player, index, stack)
if arena_lib.is_player_in_arena(player:get_player_name(), "skywars") then
skywars.HUD_armor_update(player, stack)
end
end)
dofile(minetest.get_modpath("skywars") .. "/src/_compatible_mods/3d_armor/auto_equip_armors.lua")
dofile(minetest.get_modpath("skywars") .. "/src/_compatible_mods/3d_armor/armor_hud.lua")