Moved some functions to ItemsHandler

This commit is contained in:
ZioPao
2023-11-09 10:09:46 +01:00
parent 066f4f8049
commit 60a7f1b996
3 changed files with 107 additions and 74 deletions

View File

@@ -1,6 +1,7 @@
local ModDataHandler = require("Handlers/TOC_ModDataHandler")
local StaticData = require("TOC_StaticData")
local CommonMethods = require("TOC_Common")
local ItemsHandler = require("Handlers/TOC_ItemsHandler")
---------------------------
@@ -75,8 +76,8 @@ function AmputationHandler:execute()
ModDataHandler.GetInstance():setCutLimb(self.limbName, false, false, false, surgeonFactor)
-- Give the player the correct amputation item
AmputationHandler.DeleteOldAmputationItem(self.patient, self.limbName)
self:spawnAmputationItem()
ItemsHandler.DeleteOldAmputationItem(self.patient, self.limbName)
ItemsHandler.SpawnAmputationItem(self.patient, self.limbName)
end
---Force the execution of the amputation for a trait
@@ -89,73 +90,5 @@ function AmputationHandler:close()
AmputationHandler.instance = nil
end
--* Amputation Items *--
---Search and deletes an old amputation clothing item
---@param player IsoPlayer
---@param limbName string
function AmputationHandler.DeleteOldAmputationItem(player, limbName)
local side = CommonMethods.GetSide(limbName)
for partName, _ in pairs(StaticData.PARTS_STRINGS) do
local othLimbName = partName .. "_" .. side
local othClothingItemName = StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. othLimbName
-- TODO FindAndReturn could return an ArrayList. We need to check for that
local othClothingItem = player:getInventory():FindAndReturn(othClothingItemName)
---@cast othClothingItem InventoryItem
if othClothingItem and instanceof(othClothingItem, "InventoryItem") then
player:removeWornItem(othClothingItem)
player:getInventory():Remove(othClothingItem) -- Can be a InventoryItem too.. I guess? todo check it
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
---@private
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.
---@private
function AmputationHandler:spawnAmputationItem()
print("Clothing name " .. StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. self.limbName)
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

View File

@@ -1 +1,103 @@
local StaticData = require("TOC_StaticData")
local CommonMethods = require("TOC_Common")
--- Submodule to handle spawning the correct items after certain actions (ie: cutting a hand)
---@class ItemsHandler
local ItemsHandler = {}
---Returns the correct index for the textures of the amputation
---@param isCicatrized boolean
---@return number
---@private
function ItemsHandler.GetAmputationTexturesIndex(playerObj, isCicatrized)
local textureString = playerObj: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
---Main function to delete a clothing item
---@param playerObj IsoPlayer
---@param clothingItem InventoryItem?
---@return boolean
---@private
function ItemsHandler.RemoveClothingItem(playerObj, clothingItem)
if clothingItem and instanceof(clothingItem, "InventoryItem") then
playerObj:removeWornItem(clothingItem)
playerObj:getInventory():Remove(clothingItem) -- Can be a InventoryItem too.. I guess? todo check it
print("TOC: found and deleted " .. tostring(clothingItem))
return true
end
return false
end
---Search and deletes an old amputation clothing item on the same side
---@param playerObj IsoPlayer
---@param limbName string
function ItemsHandler.DeleteOldAmputationItem(playerObj, limbName)
local side = CommonMethods.GetSide(limbName)
for partName, _ in pairs(StaticData.PARTS_STRINGS) do
local othLimbName = partName .. "_" .. side
local othClothingItemName = StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. othLimbName
-- TODO FindAndReturn could return an ArrayList. We need to check for that
local othClothingItem = playerObj:getInventory():FindAndReturn(othClothingItemName)
-- If we manage to find and remove an item, then we should stop this function.
---@cast othClothingItem InventoryItem
if ItemsHandler.RemoveClothingItem(playerObj, othClothingItem) then return end
end
end
---Deletes all the old amputation items, used for resets
---@param playerObj IsoPlayer
function ItemsHandler.DeleteAllOldAmputationItems(playerObj)
for i=1, #StaticData.LIMBS_STRINGS do
local limbName = StaticData.LIMBS_STRINGS[i]
local clothItemName = StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. limbName
local clothItem = playerObj:getInventory():FindAndReturn(clothItemName)
---@cast clothItem InventoryItem
ItemsHandler.RemoveClothingItem(playerObj, clothItem)
end
end
---Spawns and equips the correct amputation item to the player.
function ItemsHandler.SpawnAmputationItem(playerObj, limbName)
print("Clothing name " .. StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. limbName)
local clothingItem = playerObj:getInventory():AddItem(StaticData.AMPUTATION_CLOTHING_ITEM_BASE .. limbName)
local texId = ItemsHandler.GetAmputationTexturesIndex(playerObj, false)
---@cast clothingItem InventoryItem
clothingItem:getVisual():setTextureChoice(texId) -- it counts from 0, so we have to subtract 1
playerObj:setWornItem(clothingItem:getBodyLocation(), clothingItem)
end
return ItemsHandler

View File

@@ -1,5 +1,6 @@
local ModDataHandler = require("Handlers/TOC_ModDataHandler")
local AmputationHandler = require("Handlers/TOC_AmputationHandler")
local ItemsHandler = require("Handlers/TOC_ItemsHandler")
local StaticData = require("TOC_StaticData")
-----------
@@ -25,10 +26,7 @@ function PlayerHandler.InitializePlayer(_, playerObj, isForced)
-- Since isForced is used to reset an existing player data, we're gonna clean their ISHealthPanel table too
if isForced then
ISHealthPanel.highestAmputations = {}
-- TODO Hacky way to check both limbs
AmputationHandler.DeleteOldAmputationItem(playerObj, "Hand_L")
AmputationHandler.DeleteOldAmputationItem(playerObj, "Hand_R")
ItemsHandler.DeleteAllOldAmputationItems(playerObj)
end
end