moved replenish to tokenmanager

This commit is contained in:
Chr1Z93 2023-06-22 10:50:08 +02:00
parent 1bfd025100
commit 458da5d2c1
2 changed files with 80 additions and 70 deletions

View File

@ -109,11 +109,11 @@ do
-- stateIDs for the multi-stated resource tokens
local stateTable = {
["Resource"] = 1,
["Ammo"] = 2,
["Charge"] = 3,
["Secret"] = 4,
["Supply"] = 5
["resource"] = 1,
["ammo"] = 2,
["charge"] = 3,
["secret"] = 4,
["supply"] = 5
}
-- Source for tokens
@ -232,7 +232,7 @@ do
-- this is used to load the correct state for additional resource tokens (e.g. "Ammo")
local callback = nil
local stateID = stateTable[subType]
local stateID = stateTable[string.lower(subType)]
if tokenType == "resource" and stateID ~= nil and stateID ~= 1 then
callback = function(spawned) spawned.setState(stateID) end
end
@ -281,6 +281,17 @@ do
})
end
-- Checks a card for metadata to maybe replenish it
---@param card Object Card object to be replenished
---@param uses Table The already decoded metadata.uses (to avoid decoding again)
---@param mat Object The playmat the card is placed on (for rotation and casting)
TokenManager.maybeReplenishCard = function(card, uses, mat)
-- TODO: support for cards with multiple uses AND replenish (as of yet, no official card needs that)
if uses[1].count and uses[1].replenish then
internal.replenishTokens(card, uses, mat)
end
end
-- Delegate function to the token spawn tracker. Exists to avoid circular dependencies in some
-- callers.
---@param card Object Card object to reset the tokens for
@ -462,5 +473,67 @@ do
return cluePositions
end
---@param card Object Card object to be replenished
---@param uses Table The already decoded metadata.uses (to avoid decoding again)
---@param mat Object The playmat the card is placed on (for rotation and casting)
internal.replenishTokens = function(card, uses, mat)
local cardPos = card.getPosition()
-- don't continue for cards on the deck (Norman) or in the discard pile
if mat.positionToLocal(cardPos).x < -1 then return end
-- get current amount of resource tokens on the card
local search = internal.searchOnCard(cardPos, card.getRotation())
local clickableResourceCounter = nil
local foundTokens = 0
for _, obj in ipairs(search) do
local obj = obj.hit_object
local memo = obj.getMemo()
if (stateTable[memo] or 0) > 0 then
foundTokens = foundTokens + math.abs(obj.getQuantity())
obj.destruct()
elseif memo == "resourceCounter" then
foundTokens = obj.getVar("val")
clickableResourceCounter = obj
break
end
end
-- this is the theoretical new amount of uses (to be checked below)
local newCount = foundTokens + uses[1].replenish
-- if there are already more uses than the replenish amount, keep them
if foundTokens > uses[1].count then
newCount = foundTokens
-- only replenish up until the replenish amount
elseif newCount > uses[1].count then
newCount = uses[1].count
end
-- update the clickable counter or spawn a group of tokens
if clickableResourceCounter then
clickableResourceCounter.call("updateVal", newCount)
else
TokenManager.spawnTokenGroup(card, uses[1].token, newCount, _, uses[1].type)
end
end
-- searches on a card (standard size) and returns the result
---@param position Table Position of the card
---@param rotation Table Rotation of the card
internal.searchOnCard = function(position, rotation)
return Physics.cast({
origin = position,
direction = {0, 1, 0},
orientation = rotation,
type = 3,
size = { 2.5, 0.5, 3.5 },
max_distance = 1,
debug = false
})
end
return TokenManager
end

View File

@ -288,13 +288,7 @@ function doUpkeep(_, clickedByColor, isRightClick)
forcedLearning = true
end
if cardMetadata.uses ~= nil then
-- check for cards with 'replenish' in their metadata
-- TODO: support for cards with multiple uses AND replenish (as of yet, no official card needs that)
local count = cardMetadata.uses[1].count
local replenish = cardMetadata.uses[1].replenish
if count and replenish then
replenishTokens(obj, count, replenish)
end
tokenManager.maybeReplenishCard(obj, cardMetadata.uses, self)
end
end
end
@ -539,63 +533,6 @@ end
-- playmat token spawning
---------------------------------------------------------
-- replenish Tokens for specific cards (like 'Physical Training (4)')
function replenishTokens(card, count, replenish)
local cardPos = card.getPosition()
-- don't continue for cards on your deck (Norman) or in your discard pile
if self.positionToLocal(cardPos).x < -1 then return end
-- resource token states
local stateTable = {
["resource"] = 1,
["ammo"] = 2,
["charge"] = 3,
["secret"] = 4,
["supply"] = 5
}
-- get current amount of resource tokens on the card
local search = searchArea(cardPos, { 2.5, 0.5, 3.5 })
local clickableResourceCounter = nil
local foundTokens = 0
for _, obj in ipairs(search) do
local obj = obj.hit_object
local memo = obj.getMemo()
if (stateTable[memo] or 0) > 0 then
foundTokens = foundTokens + math.abs(obj.getQuantity())
obj.destruct()
elseif memo == "resourceCounter" then
foundTokens = obj.getVar("val")
clickableResourceCounter = obj
break
end
end
-- this is the theoretical new amount of uses (to be checked below)
local newCount = foundTokens + replenish
-- if there are already more uses than the replenish amount, keep them
if foundTokens > count then
newCount = foundTokens
-- only replenish up until the replenish amount
elseif newCount > count then
newCount = count
end
-- update the clickable counter or spawn a group of tokens
if clickableResourceCounter then
clickableResourceCounter.call("updateVal", newCount)
else
-- get type of resource to spawn
local metadata = JSON.decode(card.getGMNotes()) or {}
local uses = metadata.uses or {}
tokenManager.spawnTokenGroup(card, "resource", newCount, _, uses[1].type)
end
end
-- Finds all customizable cards in this play area and updates their metadata based on the selections
-- on the matching upgrade sheet.
-- This method is theoretically O(n^2), and should be used sparingly. In practice it will only be