Put armor into it's own class

This commit is contained in:
oilboi 2020-06-10 13:56:00 -04:00
parent 7606210fbc
commit 146bc3eec6
4 changed files with 35 additions and 30 deletions

View File

@ -1,8 +1,11 @@
function recalculate_armor(player)
local minetest,math,skins = minetest,math,skins
armor_class = {} --the armor class
function armor_class.recalculate_armor(player)
if not player or (player and not player:is_player()) then return end
local inv = player:get_inventory()
local player_skin = minetest.get_skin(player)
local player_skin = skins.get_skin(player)
local armor_skin = "blank_skin.png"
local stack = inv:get_stack("armor_head",1):get_name()
@ -31,7 +34,7 @@ function recalculate_armor(player)
player:set_properties({textures = {player_skin,armor_skin}})
end
function calculate_armor_absorbtion(player)
function armor_class.calculate_armor_absorbtion(player)
if not player or (player and not player:is_player()) then return end
local inv = player:get_inventory()
@ -70,17 +73,17 @@ function calculate_armor_absorbtion(player)
return(armor_absorbtion)
end
function set_armor_gui(player)
function armor_class.set_armor_gui(player)
if not player or (player and not player:is_player()) then return end
local meta = player:get_meta()
local level = calculate_armor_absorbtion(player)
local level = armor_class.calculate_armor_absorbtion(player)
local hud = meta:get_int("armor_bar")
player:hud_change(hud, "number", level)
end
function damage_armor(player,damage)
function armor_class.damage_armor(player,damage)
if not player or (player and not player:is_player()) then return end
local inv = player:get_inventory()
@ -137,8 +140,8 @@ function damage_armor(player,damage)
if recalc == true then
minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
recalculate_armor(player)
set_armor_gui(player)
armor_class.recalculate_armor(player)
armor_class.set_armor_gui(player)
--do particles too
end
end
@ -159,7 +162,7 @@ minetest.register_on_joinplayer(function(player)
hud_elem_type = "statbar",
position = {x = 0.5, y = 1},
text = "armor_icon.png",
number = calculate_armor_absorbtion(player),--meta:get_int("hunger"),
number = armor_class.calculate_armor_absorbtion(player),--meta:get_int("hunger"),
--direction = 1,
size = {x = 24, y = 24},
offset = {x = (-10 * 24) - 25, y = -(48 + 50 + 39)},
@ -174,15 +177,15 @@ minetest.register_on_joinplayer(function(player)
end)
minetest.register_on_dieplayer(function(player)
set_armor_gui(player)
armor_class.set_armor_gui(player)
end)
minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
if inventory_info.from_list == "armor_head" or inventory_info.from_list == "armor_torso" or inventory_info.from_list == "armor_legs" or inventory_info.from_list == "armor_feet" or
inventory_info.to_list == "armor_head" or inventory_info.to_list == "armor_torso" or inventory_info.to_list == "armor_legs" or inventory_info.to_list == "armor_feet" then
minetest.after(0,function()
recalculate_armor(player)
set_armor_gui(player)
armor_class.recalculate_armor(player)
armor_class.set_armor_gui(player)
end)
end
end)

View File

@ -65,8 +65,8 @@ minetest.register_globalstep(function(dtime)
end
if inv:get_stack("armor_torso",1):get_name() == "" then
recalculate_armor(player)
set_armor_gui(player)
armor_class.recalculate_armor(player)
armor_class.set_armor_gui(player)
player:set_physics_override({gravity=1.25})
if sound_handling_loop[player_name] then
--minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})

View File

@ -25,8 +25,8 @@ minetest.register_on_player_hpchange(function(player, hp_change, reason)
if new_stack == "" then
minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
recalculate_armor(player)
set_armor_gui(player)
armor_class.recalculate_armor(player)
armor_class.set_armor_gui(player)
--do particles too
elseif minetest.get_item_group(new_stack,"boots") > 0 then
local pos = player:get_pos()
@ -134,7 +134,7 @@ minetest.register_on_dieplayer(function(player, reason)
end
recalculate_armor(player)
armor_class.recalculate_armor(player)
end)
@ -274,9 +274,9 @@ minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch,
dir.y = 7
end
local hp_modifier = math.ceil(calculate_armor_absorbtion(player)/3)
local hp_modifier = math.ceil(armor_class.calculate_armor_absorbtion(player)/3)
--print("hp_modifier:",hp_modifier)
damage_armor(player,math.abs(hurt))
armor_class.damage_armor(player,math.abs(hurt))
--print("hurt:",hurt,"|","hp_modifier:",hp_modifier)
local modify_output = (hurt == 0)

View File

@ -33,7 +33,8 @@ end
--use this to retrieve skin data
local player_skin_table = {}
minetest.get_skin = function(player)
skins = {}
skins.get_skin = function(player)
local name = player:get_player_name()
if player_skin_table[name] then
return(player_skin_table[name])
@ -43,7 +44,7 @@ minetest.get_skin = function(player)
end
--use this to set skin data
minetest.set_skin = function(player,skin)
skins.set_skin = function(player,skin)
local name = player:get_player_name()
player_skin_table[name] = skin
end
@ -155,10 +156,10 @@ fetch_function = function(name)
player:set_properties({textures = {stored_texture, "blank_skin.png"}})
minetest.set_skin(player,stored_texture)
skins.set_skin(player,stored_texture)
recalculate_armor(player) --redundancy
armor_class.recalculate_armor(player) --redundancy
--[[
player:hud_add(
@ -277,7 +278,8 @@ minetest.register_entity("skins:cape",cape)
--function for handling capes
local cape_table = {}
minetest.add_cape = function(player,cape)
capes = {} -- cape class
capes.add_cape = function(player,cape)
local obj = minetest.add_entity(player:get_pos(),"skins:cape")
obj:get_luaentity().owner = player
obj:set_attach(player, "Cape_bone", vector.new(0,0.25,0.5), vector.new(-90,180,0))
@ -286,21 +288,21 @@ minetest.add_cape = function(player,cape)
cape_table[name] = {cape_texture=cape,object=obj}
end
minetest.readd_capes = function()
capes.readd_capes = function()
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if cape_table[name] and not cape_table[name].object:get_luaentity() then
minetest.add_cape(player,cape_table[name].cape_texture)
capes.add_cape(player,cape_table[name].cape_texture)
end
end
minetest.after(3,function()
minetest.readd_capes()
capes.readd_capes()
end)
end
minetest.register_on_mods_loaded(function()
minetest.after(3,function()
minetest.readd_capes()
capes.readd_capes()
end)
end)
@ -327,11 +329,11 @@ minetest.register_on_joinplayer(function(player)
end
if cape then
minetest.add_cape(player,cape)
capes.add_cape(player,cape)
end
minetest.after(0,function()
fetch_function(player:get_player_name())
recalculate_armor(player)
armor_class.recalculate_armor(player)
end)
end)