Skip to content

Commit

Permalink
Extract Player/appearance from armor & clothing. Relates to lord-se…
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed May 18, 2023
1 parent d4ac6f1 commit a7cd461
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
46 changes: 43 additions & 3 deletions mods/lord/Player/appearance/src/multiskin.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MULTISKIN_DEFAULT_SKIN = "character.png"
MULTISKIN_DEFAULT_PREVIEW = "character_preview.png"
MULTISKIN_TRANS = "lottarmor_trans.png"
local MULTISKIN_DEFAULT_SKIN = "character.png"
local MULTISKIN_DEFAULT_PREVIEW = "character_preview.png"
local MULTISKIN_TRANS = "lottarmor_trans.png"

multiskin = {}

Expand Down Expand Up @@ -62,3 +62,43 @@ player_api.register_model("lottarmor_character.b3d", {
sit = {x=81, y=160},
},
})

--- @param kind string kind(type) of equipment. For ex. "armor"|"clothing"|<your_one>
--- @param player Player the `Player` MT object
--- @return table|string[]
local function get_equip_textures(kind, player)
local textures = {}
for _, item in equipment.for_player(player):items(kind) do
if (not item:is_empty()) then
table.insert(textures, item:get_name():gsub("%:", "_") .. ".png")
end
end

return textures
end

--- @param kind string kind(type) of equipment. For ex. "armor"|"clothing"|<your_one>
--- @param player Player the `Player` MT object
--- @return string overlaid (via "^") textures names of `kind`-equipment or transparent texture name
local function overlay_equip_textures(kind, player)
local textures = get_equip_textures(kind, player)
if #textures == 0 then
return MULTISKIN_TRANS
end

return table.concat(textures, "^")
end

-- moved from lottarmor/armor.lua & lottclothing/clothing.lua
-- TODO: remove using `races.register_init_callback`, instead use `equipment.on_load` & `character.get_texture`
races.register_init_callback(function(name, race, gender, skin, texture, face)
local joined_player = minetest.get_player_by_name(name)
multiskin:init(joined_player, texture)
end)

-- When *any* equipment changed (armor or clothing),
-- we need to update player model appearance to show clothes and/or armor.
equipment.on_change(function(player, kind, event, slot, item)
multiskin[player:get_player_name()][kind] = overlay_equip_textures(kind, player)
multiskin:update_player_visuals(player)
end)
15 changes: 3 additions & 12 deletions mods/lord/lottarmor/armor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ function armor:set_player_armor(player)
if not name then
return
end
local armor_texture = "lottarmor_trans.png"
local armor_level = 0
local armor_heal = 0
local armor_fire = 0
local state = 0
local items = 0
local elements = {}
local textures = {}
local physics_o = {speed=1,gravity=1,jump=1}
local material = {type=nil, count=1}
local preview = multiskin:get_preview(name)
Expand All @@ -49,7 +47,6 @@ function armor:set_player_armor(player)
local level = def.groups["armor_"..k]
if level and not def.groups["clothes"] then
local texture = item:gsub("%:", "_")
table.insert(textures, texture..".png")
preview = preview.."^"..texture.."_preview.png"
armor_level = armor_level + level
state = state + stack:get_wear()
Expand Down Expand Up @@ -84,9 +81,6 @@ function armor:set_player_armor(player)
if material.type and material.count == #self.elements then
armor_level = armor_level * 1.1
end
if #textures > 0 then
armor_texture = table.concat(textures, "^")
end
local armor_groups = {fleshy=100}
local immortal = player:get_armor_groups().immortal
if immortal and immortal ~= 0 then
Expand All @@ -102,8 +96,9 @@ function armor:set_player_armor(player)
player:set_armor_groups(armor_groups)
end
player:set_physics_override(physics_o)
self.textures[name].armor = armor_texture

self.textures[name].preview = preview

self.def[name].state = state
self.def[name].count = items
self.def[name].level = armor_level
Expand All @@ -112,8 +107,6 @@ function armor:set_player_armor(player)
self.def[name].speed = physics_o.speed
self.def[name].gravity = physics_o.gravity
self.def[name].fire = armor_fire
multiskin[name].armor = armor_texture
multiskin:update_player_visuals(player)
end

local handle_armor_wear = function(player)
Expand Down Expand Up @@ -193,9 +186,6 @@ end
-- Register Callbacks

races.register_init_callback(function(name, race, gender, skin, texture, face)
local joined_player = minetest.get_player_by_name(name)
multiskin:init(joined_player, texture)

armor.player_hp[name] = 0
armor.def[name] = {
state = 0,
Expand All @@ -212,6 +202,7 @@ races.register_init_callback(function(name, race, gender, skin, texture, face)
preview = "character_preview.png"
}

local joined_player = minetest.get_player_by_name(name)
minetest.after(ARMOR_INIT_DELAY, function(player)
armor:set_player_armor(player)
inventory.update(player)
Expand Down
9 changes: 0 additions & 9 deletions mods/lord/lottarmor/clothing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,25 @@ clothing.set_player_clothing = function(self, player)
if not name or not player_inv then
return
end
local clothing_texture = "lottarmor_trans.png"
local textures = {}
local preview = multiskin:get_skin_name(name) or "clothing_preview"
preview = preview..".png"
for _, stack in equipment.for_player(player):items(equipment.Kind.CLOTHING) do
if stack:get_count() == 1 then
local def = stack:get_definition()
if def.groups["clothes"] == 1 then
local texture = stack:get_name():gsub("%:", "_")
table.insert(textures, texture..".png")
if not def.groups["no_preview"] then
preview = preview.."^"..texture.."_preview.png"
end
end
end
end
if #textures > 0 then
clothing_texture = table.concat(textures, "^")
end
multiskin[name].clothing = clothing_texture
multiskin:update_player_visuals(player)
end



races.register_init_callback(function(name, race, gender, skin, texture, face)
local joined_player = minetest.get_player_by_name(name)
multiskin:init(joined_player, texture)

minetest.after(ARMOR_INIT_DELAY, function(player)
clothing:set_player_clothing(player)
Expand Down

0 comments on commit a7cd461

Please sign in to comment.