From 301603ed6800c796f4ac0b0526b729af06a800d5 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Tue, 7 Nov 2023 17:21:03 +0100 Subject: [PATCH] Reimplemented item handling --- .../client/Handlers/TOC_AmputationHandler.lua | 70 ++++++++++++++++++- .../lua/client/Handlers/TOC_PlayerHandler.lua | 3 - media/lua/client/TOC_Common.lua | 10 +++ media/lua/client/TOC_StaticData.lua | 9 +++ media/lua/client/UI/TOC_HealthPanel.lua | 4 +- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 media/lua/client/TOC_Common.lua diff --git a/media/lua/client/Handlers/TOC_AmputationHandler.lua b/media/lua/client/Handlers/TOC_AmputationHandler.lua index 1871543..5dcce41 100644 --- a/media/lua/client/Handlers/TOC_AmputationHandler.lua +++ b/media/lua/client/Handlers/TOC_AmputationHandler.lua @@ -1,5 +1,6 @@ local ModDataHandler = require("Handlers/TOC_ModDataHandler") local StaticData = require("TOC_StaticData") +local CommonMethods = require("TOC_Common") --------------------------- @@ -34,6 +35,9 @@ function AmputationHandler:new(limbName, surgeonPl) return o end + +--* Main methods *-- + ---Starts bleeding from the point where the saw is being used function AmputationHandler:damageDuringAmputation() print("TOC: Damage patient") @@ -68,7 +72,9 @@ function AmputationHandler:execute() patientStats:setStress(baseDamage - surgeonFactor) -- Set the data in modData - ModDataHandler.GetInstance():setCutLimb(self.limbName, false, false, false, self.surgeonFactor) + ModDataHandler.GetInstance():setCutLimb(self.limbName, false, false, false, surgeonFactor) + + -- Give the player the correct amputation item end ---Force the execution of the amputation for a trait @@ -81,5 +87,67 @@ function AmputationHandler:close() AmputationHandler.instance = nil end +--* Amputation Items *-- + +---Search and deletes an old amputation clothing item +---@private +function AmputationHandler:deleteOldAmputationItem() + local side = CommonMethods.GetSide(self.limbName) + + for partName, _ in pairs(StaticData.PARTS_STRINGS) do + local othLimbName = partName .. "_" .. side + local othClothingItemName = StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. othLimbName + local othClothingItem = self.patient:getInventory():FindAndReturn(othClothingItemName) + if othClothingItem then + self.patient:getInventory():Remove(othClothingItem) -- It accepts it as an Item, not a string + print("TOC: found and deleted " .. othClothingItemName) + return + end + end +end + +---Returns the correct index for the textures of the amputation +---@param isCicatrized boolean +---@return number +function AmputationHandler:getAmputationTexturesIndex(isCicatrized) + local textureString = self.patient:getHumanVisual():getSkinTexture() + local isHairy = string.find(textureString, "a$") + -- Hairy bodies + if isHairy then + textureString = textureString:sub(1, -2) -- Removes b at the end to make it compatible + end + + local matchedIndex = string.match(textureString, "%d$") + + -- TODO Rework this + if isHairy then + matchedIndex = matchedIndex + 5 + end + + if isCicatrized then + if isHairy then + matchedIndex = matchedIndex + 5 -- to use the cicatrized texture on hairy bodies + else + matchedIndex = matchedIndex + 10 -- cicatrized texture only, no hairs + end + end + + return matchedIndex - 1 +end + +---Spawns and equips the correct amputation item to the player. In case there was another amputation on the same side, it's gonna get deleted +---@private +function AmputationHandler:spawnAmputationItem() + -- TODO Check if there are previous amputation clothing items on that side and deletes them + + local clothingItem = self.patient:getInventory():AddItem(StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. self.limbName) + local texId = self:getAmputationTexturesIndex(false) + + ---@cast clothingItem InventoryItem + clothingItem:getVisual():setTextureChoice(texId) -- it counts from 0, so we have to subtract 1 + self.patient:setWornItem(clothingItem:getBodyLocation(), clothingItem) +end + + return AmputationHandler \ No newline at end of file diff --git a/media/lua/client/Handlers/TOC_PlayerHandler.lua b/media/lua/client/Handlers/TOC_PlayerHandler.lua index 55e3a71..48ba547 100644 --- a/media/lua/client/Handlers/TOC_PlayerHandler.lua +++ b/media/lua/client/Handlers/TOC_PlayerHandler.lua @@ -11,12 +11,9 @@ local StaticData = require("TOC_StaticData") -- Update current player status (infection checks) -- handle stats increase\decrease - ---@class PlayerHandler local PlayerHandler = {} --- TODO This should be instanceable for a player. Separate handlers not - ---Setup player modData ---@param _ nil ---@param playerObj IsoPlayer diff --git a/media/lua/client/TOC_Common.lua b/media/lua/client/TOC_Common.lua new file mode 100644 index 0000000..fcb92b3 --- /dev/null +++ b/media/lua/client/TOC_Common.lua @@ -0,0 +1,10 @@ +local CommonMethods = {} + +---Returns the side for a certain limb +---@param limbName string +---@return string "L" or "R" +function CommonMethods.GetSide(limbName) + if string.find(limbName, "_L") then return "L" else return "R" end +end + +return CommonMethods \ No newline at end of file diff --git a/media/lua/client/TOC_StaticData.lua b/media/lua/client/TOC_StaticData.lua index 7bcdde8..aaae948 100644 --- a/media/lua/client/TOC_StaticData.lua +++ b/media/lua/client/TOC_StaticData.lua @@ -70,4 +70,13 @@ StaticData.HEALTH_PANEL_TEXTURES = { } +----------------- +-- Visuals and clothing + + + + +StaticData.AMPUTATION_CLOTHING_ITEM_BASE = "TOC.Amputation_" + + return StaticData diff --git a/media/lua/client/UI/TOC_HealthPanel.lua b/media/lua/client/UI/TOC_HealthPanel.lua index 5ff7a78..154a074 100644 --- a/media/lua/client/UI/TOC_HealthPanel.lua +++ b/media/lua/client/UI/TOC_HealthPanel.lua @@ -1,5 +1,6 @@ local PlayerHandler = require("TOC_PlayerHandler") local StaticData = require("TOC_StaticData") +local CommonMethods = require("TOC_Common") ---@diagnostic disable: duplicate-set-field local CutLimbHandler = require("UI/TOC_CutLimbHandler") @@ -42,8 +43,7 @@ function ISHealthPanel.GetHighestAmputation() ISHealthPanel.highestAmputations = {} for i=1, #StaticData.LIMBS_STRINGS do local limbName = StaticData.LIMBS_STRINGS[i] - local index - if string.find(limbName, "_L") then index = "L" else index = "R" end + local index = CommonMethods.GetSide(limbName) if PlayerHandler.modDataHandler:getIsCut(limbName) and PlayerHandler.modDataHandler:getIsVisible(limbName) then ISHealthPanel.highestAmputations[index] = limbName end