Merge pull request #989 from YumiWhellie/main

added check for dropping cards with tokens on them
This commit is contained in:
Chr1Z 2024-11-18 16:31:33 +01:00 committed by GitHub
commit 7bc0856135
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 103 additions and 2 deletions

View File

@ -73,6 +73,9 @@ local RESOURCE_OPTIONS = {
local handVisibility = {}
local blurseVisibility = {}
-- track cards' settings
local cardSetting = {}
---------------------------------------------------------
-- data for tokens
---------------------------------------------------------
@ -205,6 +208,8 @@ function tryObjectEnterContainer(container, object)
-- stop mini cards from forming decks
if object.hasTag("Minicard") and container.hasTag("Minicard") then
return false
elseif object.getName() ~= "Atlach-Nacha" and next(object.getAttachments()) ~= nil then
handleTokenDetaching({ card = object })
end
playAreaApi.tryObjectEnterContainer(container, object)
@ -297,6 +302,45 @@ function onPlayerAction(player, action, targets)
trash.putObject(target)
end
return false
elseif action == Player.Action.PickUp then
local pickedCards = {}
for _, target in ipairs(targets) do
if target.type == "Card" then
table.insert(pickedCards, target)
end
end
if #pickedCards > 5 then return end
for _, pickedCard in ipairs(pickedCards) do
local searchResult = searchLib.onObject(pickedCard, "isTileOrToken", 0.95)
if pickedCard.is_face_down and next(searchResult) ~= nil then
cardSetting[pickedCard] = {
hideFacedown = pickedCard.hide_when_face_down,
tooltip = pickedCard.tooltip
}
pickedCard.hide_when_face_down = false
pickedCard.tooltip = false
end
for _, token in ipairs(searchResult) do
if not token.locked then
pickedCard.addAttachment(token)
end
end
Wait.condition(
function()
if pickedCard ~= nil then
handleTokenDetaching({ card = pickedCard })
end
end,
function()
if pickedCard ~= nil and player ~= nil and player.seated then
return pickedCard.resting and not tableContains(player.getHoldingObjects(), pickedCard)
else
return true
end
end
)
end
end
return true
end
@ -2821,3 +2865,38 @@ end
function moveAndRotatePlayermat(params)
playermatApi.moveAndRotate(params.matColor, params.position, params.rotationY)
end
-- check if an element is in a table
function tableContains(thisTable, thisElement)
for _, element in pairs(thisTable) do
if element == thisElement then
return true
end
end
return false
end
function handleTokenDetaching(params)
local pickedCard = params["card"]
if cardSetting[pickedCard] ~= nil then
local pickedCardSetting = cardSetting[pickedCard]
pickedCard.hide_when_face_down = pickedCardSetting["hideFacedown"]
pickedCard.tooltip = pickedCardSetting["tooltip"]
cardSetting[pickedCard] = nil
end
local removedTokens = pickedCard.removeAttachments()
for _, token in ipairs(removedTokens) do
if token.getPosition().y < pickedCard.getPosition().y then
local posY = pickedCard.getPosition().y + 0.05
token.setPosition(token.getPosition():setAt("y", posY))
end
end
if pickedCard.hasTag("CardThatSeals") then
local func = pickedCard.getVar("updateStackSize") -- make sure function exists
if func ~= nil then
pickedCard.call("updateStackSize")
end
end
end

View File

@ -26,6 +26,9 @@ local isReshuffling = false
local collisionEnabled = false
local currentScenario, useFrontData, tokenData, scenarioCard
-- for stopping multiple collisions of the same object
local collisionTable = {}
function updateSave()
local data = {
currentScenario = currentScenario,
@ -92,11 +95,19 @@ function onCollisionEnter(collisionInfo)
-- early exit for better performance
if object.type ~= "Card" then return end
-- only continue if card didn't already collide
if collisionTable[object] ~= nil then return end
collisionTable[object] = true
Wait.frames(function() collisionTable[object] = nil end, 1)
local localPos = self.positionToLocal(object.getPosition())
if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then
-- reset spawned tokens and remove tokens from cards in encounter deck / discard area
Wait.frames(function() tokenSpawnTrackerApi.resetTokensSpawned(object) end, 1)
if next(object.getAttachments()) ~= nil then
Global.call("handleTokenDetaching", { card = object })
end
removeTokensFromObject(object)
elseif inArea(localPos, SCENARIO_REFERENCE_AREA) then

View File

@ -45,6 +45,9 @@ optionPanelData.slotEditing = false
local collisionEnabled = false
local currentlyEditingSlots = false
-- for stopping multiple collisions of the same object
local collisionTable = {}
-- x-Values for discard buttons
local DISCARD_BUTTON_X_START = -1.365
local DISCARD_BUTTON_X_OFFSET = 0.455
@ -1277,6 +1280,11 @@ function onCollisionEnter(collisionInfo)
-- only continue for cards
if object.type ~= "Card" then return end
-- only continue if card didn't already collide
if collisionTable[object] ~= nil then return end
collisionTable[object] = true
Wait.frames(function() collisionTable[object] = nil end, 1)
local md = JSON.decode(object.getGMNotes()) or {}
syncCustomizableMetadata(object, md)
@ -1290,6 +1298,9 @@ function onCollisionEnter(collisionInfo)
end
elseif inArea(localCardPos, DECK_DISCARD_AREA) then
if next(object.getAttachments()) ~= nil then
Global.call("handleTokenDetaching", { card = object })
end
tokenSpawnTrackerApi.resetTokensSpawned(object)
removeTokensFromObject(object)

View File

@ -46,8 +46,8 @@ do
-- searches the area on an object
function SearchLib.onObject(obj, filter, scale)
scale = scale or 1
local pos = obj.getPosition()
local size = obj.getBounds().size:scale(scale):setAt("y", 1)
local pos = obj.getPosition() + Vector(0, 1, 0) -- offset by half the cast's height
local size = obj.getBounds().size:scale(scale):setAt("y", 2)
return returnSearchResult(pos, _, size, filter)
end