From cb5686e4bf97cc881f59a1ea961c91185fef34de Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Mon, 5 Aug 2024 10:11:10 +0200 Subject: [PATCH] fixed call back functions --- src/core/Global.ttslua | 106 ++++++++++++-------- src/core/UniversalActionAbilityToken.ttslua | 8 +- src/core/token/TokenManagerApi.ttslua | 8 +- src/playercards/cards/KohakuNarukami.ttslua | 8 +- src/playermat/Playermat.ttslua | 63 ++++++------ src/util/TokenSpawnTool.ttslua | 21 ++-- 6 files changed, 111 insertions(+), 103 deletions(-) diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index 3ae1dce4..bbf5aa47 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -72,7 +72,7 @@ local RESOURCE_OPTIONS = { } -- tracks the visibility of each hand -local handVisibility = {} +local handVisibility = {} --------------------------------------------------------- -- data for tokens @@ -1263,7 +1263,8 @@ end -- updates the preview window function updatePreviewWindow() local item = library[contentToShow][currentListItem] - local tempImage = "https://steamusercontent-a.akamaihd.net/ugc/2115061845788345842/2CD6ABC551555CCF58F9D0DDB7620197BA398B06/" + local tempImage = + "https://steamusercontent-a.akamaihd.net/ugc/2115061845788345842/2CD6ABC551555CCF58F9D0DDB7620197BA398B06/" -- set default image if not defined if item.boxsize == nil or item.boxsize == "" or item.boxart == nil or item.boxart == "" then @@ -2037,12 +2038,12 @@ function TokenManager.spawnForCard(params) end -- Spawns a set of tokens on the given card. -function TokenManager.spawnTokenGroup(param) - local card = param.card - local tokenType = param.tokenType - local tokenCount = param.tokenCount - local shiftDown = param.shiftDown - local subType = param.subType +function TokenManager.spawnTokenGroup(params) + local card = params.card + local tokenType = params.tokenType + local tokenCount = params.tokenCount + local shiftDown = params.shiftDown + local subType = params.subType if tokenType == "damage" or tokenType == "horror" then TokenManager.spawnCounterToken(card, tokenType, tokenCount, shiftDown) @@ -2062,33 +2063,22 @@ end function TokenManager.spawnCounterToken(card, tokenType, tokenValue, shiftDown) if tokenValue < 1 or tokenValue > 50 then return end - local pos = card.positionToWorld(tokenOffsets[1][1] + Vector(0, 0, shiftDown)) - local rot = card.getRotation() - TokenManager.spawnToken({ - position = pos, + position = card.positionToWorld(tokenOffsets[1][1] + Vector(0, 0, shiftDown)), tokenType = tokenType, - rotation = rot, - callback = function(spawned) - -- token starts in state 1, so don't attempt to change it to avoid error - if tokenValue ~= 1 then - spawned.setState(tokenValue) - end - end + rotation = card.getRotation(), + callbackName = "updateStateToken", + callbackParams = tokenValue }) end -TokenManager.spawnResourceCounterToken = function(card, tokenCount) - local pos = card.positionToWorld(card.positionToLocal(card.getPosition()) + Vector(0, 0.2, -0.5)) - local rot = card.getRotation() - +function TokenManager.spawnResourceCounterToken(card, tokenCount) TokenManager.spawnToken({ - position = pos, + position = card.positionToWorld(card.positionToLocal(card.getPosition()) + Vector(0, 0.2, -0.5)), tokenType = "resourceCounter", - rotation = rot, - callback = function(spawned) - spawned.call("updateVal", tokenCount) - end + rotation = card.getRotation(), + callbackName = "updateTokenValue", + callbackParams = tokenCount }) end @@ -2132,19 +2122,17 @@ function TokenManager.spawnMultipleTokens(card, tokenType, tokenCount, shiftDown end -- this is used to load the correct state for additional resource tokens (e.g. "Ammo") - local callback = nil + local callbackName = nil + local callbackParams = nil local stateID = stateTable[string.lower(subType or "")] - if tokenType == "resource" and stateID ~= nil and stateID ~= 1 then - callback = function(spawned) spawned.setState(stateID) end + if tokenType == "resource" then + callbackName = "updateStateToken" + callbackParams = stateID elseif tokenType == "universalActionAbility" then - callback = function(spawned) - local matColor = playermatApi.getMatColorByPosition(card.getPosition()) - local activeInvestigatorData = playermatApi.getActiveInvestigatorData(matColor) - spawned.call("updateClassAndSymbol", { - class = activeInvestigatorData.class, - symbol = subType or activeInvestigatorData.class - }) - end + local matColor = playermatApi.getMatColorByPosition(card.getPosition()) + local activeInvestigatorData = playermatApi.getActiveInvestigatorData(matColor) + callbackName = "updateUniversalActionAbilityToken" + callbackParams = { class = activeInvestigatorData.class, symbol = subType or activeInvestigatorData.class } end for i = 1, tokenCount do @@ -2152,7 +2140,8 @@ function TokenManager.spawnMultipleTokens(card, tokenType, tokenCount, shiftDown position = offsets[i], tokenType = tokenType, rotation = card.getRotation(), - callback = callback + callbackName = callbackName, + callbackParams = callbackParams }) end end @@ -2162,7 +2151,13 @@ function TokenManager.spawnToken(params) local position = params.position local rotation = params.rotation local tokenType = params.tokenType - local callback = params.callback + local callbackName = params.callbackName + local callbackParams = params.callbackParams + + if callbackName and type(_G[callbackName]) ~= "function" then + error("Callback function " .. callbackName .. " does not exist") + return + end TokenManager.initTokenTemplates() @@ -2191,7 +2186,7 @@ function TokenManager.spawnToken(params) data = tokenTemplate, position = position, rotation = rot, - callback_function = callback + callback_function = function(obj) _G[callbackName](obj, callbackParams) end }) end @@ -2312,7 +2307,11 @@ end function TokenManager.spawnLocationTokensFromDataHelper(card, locationData) local clueCount = TokenManager.getClueCountFromData(card, locationData) if clueCount > 0 then - TokenManager.spawnTokenGroup({ card = card, tokenType = "clue", tokenCount = clueCount }) + TokenManager.spawnTokenGroup({ + card = card, + tokenType = "clue", + tokenCount = clueCount + }) tokenSpawnTrackerApi.markTokensSpawned(card.getGUID()) end end @@ -2461,6 +2460,27 @@ TokenManager.replenishTokens = function(card, useInfo) end end +--------------------------------------------------------- +-- Callback functions for token spawning +--------------------------------------------------------- + +function updateUniversalActionAbilityToken(obj, params) + obj.call("updateClassAndSymbol", params) + if params.addTag then + obj.addTag(params.addTag) + end +end + +function updateStateToken(obj, stateID) + if stateID ~= nil and stateID ~= 1 then + obj.setState(stateID) + end +end + +function updateTokenValue(obj, newVal) + obj.call("updateVal", newVal) +end + --------------------------------------------------------- -- Utility functions --------------------------------------------------------- diff --git a/src/core/UniversalActionAbilityToken.ttslua b/src/core/UniversalActionAbilityToken.ttslua index de55b40d..138575db 100644 --- a/src/core/UniversalActionAbilityToken.ttslua +++ b/src/core/UniversalActionAbilityToken.ttslua @@ -45,10 +45,10 @@ local listOfSymbols = { local colorsForClasses = { Guardian = Color.new(19 / 255, 84 / 255, 165 / 255), - Mystic = Color.new(82 / 255, 18 / 255, 97 / 255), - Neutral = Color.new(108 / 255, 110 / 255, 112 / 255), - Rogue = Color.new(17 / 255, 72 / 255, 54 / 255), - Seeker = Color.new(215 / 255, 115 / 255, 35 / 255), + Mystic = Color.new(82 / 255, 18 / 255, 97 / 255), + Neutral = Color.new(108 / 255, 110 / 255, 112 / 255), + Rogue = Color.new(17 / 255, 72 / 255, 54 / 255), + Seeker = Color.new(215 / 255, 115 / 255, 35 / 255), Survivor = Color.new(190 / 255, 30 / 255, 45 / 255) } diff --git a/src/core/token/TokenManagerApi.ttslua b/src/core/token/TokenManagerApi.ttslua index 1b9e42d6..eff3cc75 100644 --- a/src/core/token/TokenManagerApi.ttslua +++ b/src/core/token/TokenManagerApi.ttslua @@ -38,15 +38,17 @@ do ---@param tokenType string Type of token to spawn (template needs to be in source bag) ---@param rotation tts__Vector Rotation to be used for the new token. Only the y-value will be used, -- x and z will use the default rotation from the source bag - ---@param callback? function A callback function triggered after the new token is spawned - function TokenManagerApi.spawnToken(position, tokenType, rotation, callback) + ---@param callbackName? string Name of the callback function (in Global) + ---@param callbackParams? any Parameters for the callback function + function TokenManagerApi.spawnToken(position, tokenType, rotation, callbackName, callbackParams) Global.call("callTable", { { "TokenManager", "spawnToken" }, { position = position, tokenType = tokenType, rotation = rotation, - callback = callback + callbackName = callbackName, + callbackParams = callbackParams } }) end diff --git a/src/playercards/cards/KohakuNarukami.ttslua b/src/playercards/cards/KohakuNarukami.ttslua index 812f50e8..561dce0b 100644 --- a/src/playercards/cards/KohakuNarukami.ttslua +++ b/src/playercards/cards/KohakuNarukami.ttslua @@ -84,11 +84,9 @@ function removeAndExtraAction() end end - callback = function(spawned) - spawned.call("updateClassAndSymbol", { class = "Mystic", symbol = "Mystic" }) - spawned.addTag("Temporary") - end - tokenManagerApi.spawnToken(emptyPos + Vector(0, 0.7, 0), "universalActionAbility", rotation, callback) + local callbackName = "updateUniversalActionAbilityToken" + local callbackParams = { class = "Mystic", symbol = "Mystic", addTag = "Temporary"} + tokenManagerApi.spawnToken(emptyPos + Vector(0, 0.7, 0), "universalActionAbility", rotation, callbackName, callbackParams) end function elderSignAbility() diff --git a/src/playermat/Playermat.ttslua b/src/playermat/Playermat.ttslua index d2244c63..c78eed6a 100644 --- a/src/playermat/Playermat.ttslua +++ b/src/playermat/Playermat.ttslua @@ -74,6 +74,16 @@ local DECK_DISCARD_AREA = { local DRAW_DECK_POSITION = { x = -1.82, y = 0.1, z = 0 } local DISCARD_PILE_POSITION = { x = -1.82, y = 0.1, z = 0.61 } local DRAWN_ENCOUNTER_POSITION = { x = 1.365, y = 0.5, z = -0.625 } +local tokenSpawnPos = { + action = { + Vector(-0.86, 0, -0.28), -- left of the regular three actions + Vector(-1.54, 0, -0.28), -- right of the regular three actions + }, + ability = { + Vector(-1, 0, 0.118), -- bottom left corner of the investigator card + Vector(-1, 0, -0.118), -- top left corner of the investigator card + } +} -- global position of encounter discard pile local ENCOUNTER_DISCARD_POSITION = { x = -3.85, y = 1.5, z = 10.38 } @@ -150,14 +160,14 @@ end function onLoad(savedData) if savedData and savedData ~= "" then - local loadedData = JSON.decode(savedData) - activeInvestigatorData = loadedData.activeInvestigatorData - isClassTextureEnabled = loadedData.isClassTextureEnabled - isDrawButtonVisible = loadedData.isDrawButtonVisible - optionPanelData = loadedData.optionPanelData - optionPanelVisibility = loadedData.optionPanelVisibility - playerColor = loadedData.playerColor - slotData = loadedData.slotData + local loadedData = JSON.decode(savedData) + activeInvestigatorData = loadedData.activeInvestigatorData + isClassTextureEnabled = loadedData.isClassTextureEnabled + isDrawButtonVisible = loadedData.isDrawButtonVisible + optionPanelData = loadedData.optionPanelData + optionPanelVisibility = loadedData.optionPanelVisibility + playerColor = loadedData.playerColor + slotData = loadedData.slotData -- make sure that edit mode starts disabled optionPanelData.slotEditing = false @@ -975,7 +985,9 @@ end -- instruct Global to update this mat's hand visibility function onClick_visibilitySelect(player) if player.color == playerColor then - printToColor("This is meant to be clicked by other players to be able to see your hand (primarily for multi-handed gameplay). It won't do anything for you.", playerColor) + printToColor( + "This is meant to be clicked by other players to be able to see your hand (primarily for multi-handed gameplay). It won't do anything for you.", + playerColor) return end @@ -1257,32 +1269,17 @@ function maybeUpdateActiveInvestigator(card) end -- spawn three regular action tokens (investigator specific one in the bottom spot) + local rotation = self.getRotation() + local callbackName = "updateUniversalActionAbilityToken" + local callbackParams = { class = activeInvestigatorData.class, symbol = activeInvestigatorData.class } + for i = 1, 3 do local pos = self.positionToWorld(Vector(-1.54 + i * 0.17, 0, -0.28)):add(Vector(0, 0.2, 0)) - tokenManagerApi.spawnToken(pos, "universalActionAbility", self.getRotation(), - function(spawned) - spawned.call("updateClassAndSymbol", - { - class = activeInvestigatorData.class, - symbol = activeInvestigatorData.class - }) - end) + tokenManagerApi.spawnToken(pos, "universalActionAbility", rotation, callbackName, callbackParams) end -- spawn additional token (maybe specific for investigator) if extraToken and extraToken ~= "None" then - -- local positions - local tokenSpawnPos = { - action = { - Vector(-0.86, 0, -0.28), -- left of the regular three actions - Vector(-1.54, 0, -0.28), -- right of the regular three actions - }, - ability = { - Vector(-1, 0, 0.118), -- bottom left corner of the investigator card - Vector(-1, 0, -0.118), -- top left corner of the investigator card - } - } - -- spawn tokens (split string by "|") local count = { action = 0, ability = 0 } for str in string.gmatch(extraToken, "([^|]+)") do @@ -1297,11 +1294,8 @@ function maybeUpdateActiveInvestigator(card) else local localSpawnPos = tokenSpawnPos[type][count[type]] local globalSpawnPos = self.positionToWorld(localSpawnPos):add(Vector(0, 0.2, 0)) - - tokenManagerApi.spawnToken(globalSpawnPos, "universalActionAbility", self.getRotation(), - function(spawned) - spawned.call("updateClassAndSymbol", { class = activeInvestigatorData.class, symbol = str }) - end) + callbackParams.symbol = str + tokenManagerApi.spawnToken(globalSpawnPos, "universalActionAbility", rotation, callbackName, callbackParams) end end end @@ -1567,7 +1561,6 @@ function updatePlayerCards(args) tokenManagerApi.addPlayerCardData(playerCardData) end - function getActiveInvestigatorData() return activeInvestigatorData end function setActiveInvestigatorData(newData) activeInvestigatorData = newData end diff --git a/src/util/TokenSpawnTool.ttslua b/src/util/TokenSpawnTool.ttslua index e140aee2..d476599c 100644 --- a/src/util/TokenSpawnTool.ttslua +++ b/src/util/TokenSpawnTool.ttslua @@ -20,7 +20,8 @@ function onScriptingButtonDown(index, playerColor) local rotation = { x = 0, y = Player[playerColor].getPointerRotation(), z = 0 } local position = Player[playerColor].getPointerPosition() + Vector(0, 0.2, 0) - callback = nil + callbackName = nil + callbackParams = nil -- check for subtype of resource based on card below if tokenType == "resource" then @@ -66,20 +67,14 @@ function onScriptingButtonDown(index, playerColor) -- check for nearest investigator card and change action token state to its class elseif tokenType == "universalActionAbility" then - callback = function(spawned) - local matColor = playermatApi.getMatColorByPosition(position) - local matRotation = playermatApi.returnRotation(matColor) - local activeInvestigatorData = playermatApi.getActiveInvestigatorData(matColor) - - spawned.setRotation(matRotation) - spawned.call("updateClassAndSymbol", { - class = activeInvestigatorData.class, - symbol = activeInvestigatorData.class - }) - end + local matColor = playermatApi.getMatColorByPosition(position) + local activeInvestigatorData = playermatApi.getActiveInvestigatorData(matColor) + rotation = playermatApi.returnRotation(matColor) + callbackName = "updateUniversalActionAbilityToken" + callbackParams = { class = activeInvestigatorData.class, symbol = activeInvestigatorData.class } end - tokenManagerApi.spawnToken(position, tokenType, rotation, callback) + tokenManagerApi.spawnToken(position, tokenType, rotation, callbackName, callbackParams) end -- gets the target card for this operation