diff --git a/src/core/MythosArea.ttslua b/src/core/MythosArea.ttslua index 29db5e1d..e3520564 100644 --- a/src/core/MythosArea.ttslua +++ b/src/core/MythosArea.ttslua @@ -1,3 +1,4 @@ +local deckLib = require("util/DeckLib") local guidReferenceApi = require("core/GUIDReferenceApi") local playAreaApi = require("core/PlayAreaApi") local searchLib = require("util/SearchLib") @@ -171,8 +172,7 @@ function actualEncounterCardDraw(card, params) faceUpRotation = 180 end end - card.setPositionSmooth(params.pos, false, false) - card.setRotationSmooth({ 0, params.rotY, faceUpRotation }, false, false) + deckLib.placeOrMergeIntoDeck(card, params.pos, { 0, params.rotY, faceUpRotation }) end function reshuffleEncounterDeck(params) diff --git a/src/playermat/Playmat.ttslua b/src/playermat/Playmat.ttslua index 0d41779f..21939438 100644 --- a/src/playermat/Playmat.ttslua +++ b/src/playermat/Playmat.ttslua @@ -1,8 +1,9 @@ -local searchLib = require("util/SearchLib") local chaosBagApi = require("chaosbag/ChaosBagApi") +local deckLib = require("util/DeckLib") local guidReferenceApi = require("core/GUIDReferenceApi") local mythosAreaApi = require("core/MythosAreaApi") local navigationOverlayApi = require("core/NavigationOverlayApi") +local searchLib = require("util/SearchLib") local tokenChecker = require("core/token/TokenChecker") local tokenManager = require("core/token/TokenManager") @@ -210,9 +211,9 @@ function discardListOfObjects(objList) for _, obj in ipairs(objList) do if obj.type == "Card" or obj.type == "Deck" then if obj.hasTag("PlayerCard") then - placeOrMergeIntoDeck(obj, returnGlobalDiscardPosition(), self.getRotation()) + deckLib.placeOrMergeIntoDeck(obj, returnGlobalDiscardPosition(), self.getRotation()) else - placeOrMergeIntoDeck(obj, ENCOUNTER_DISCARD_POSITION, {x = 0, y = -90, z = 0}) + deckLib.placeOrMergeIntoDeck(obj, ENCOUNTER_DISCARD_POSITION, {x = 0, y = -90, z = 0}) end -- put chaos tokens back into bag (e.g. Unrelenting) elseif tokenChecker.isChaosToken(obj) then @@ -225,46 +226,6 @@ function discardListOfObjects(objList) end end --- places a card/deck at a position or merges into an existing deck --- rotation is optional -function placeOrMergeIntoDeck(obj, pos, rot) - if not pos then return end - - local offset = 0.5 - - -- search the new position for existing card/deck - local searchResult = searchArea(pos, { 1, 1, 1 }, "isCardOrDeck") - - -- get new position - local newPos - if #searchResult == 1 then - local bounds = searchResult[1].getBounds() - newPos = Vector(pos):setAt("y", bounds.center.y + bounds.size.y / 2 + offset) - else - newPos = Vector(pos) + Vector(0, offset, 0) - end - - -- allow moving the objects smoothly out of the hand - obj.use_hands = false - - if rot then - obj.setRotationSmooth(rot, false, true) - end - obj.setPositionSmooth(newPos, false, true) - - -- continue if the card stops smooth moving - Wait.condition( - function() - obj.use_hands = true - -- this avoids a TTS bug that merges unrelated cards that are not resting - if #searchResult == 1 and searchResult[1] ~= obj then - -- call this with avoiding errors (physics is sometimes too fast so the object doesn't exist for the put) - pcall(function() searchResult[1].putObject(obj) end) - end - end, - function() return not obj.isSmoothMoving() end, 3) -end - -- build a discard button to discard from searchPosition (number must be unique) function makeDiscardButton(xValue, number) local position = { xValue, 0.1, -0.94} @@ -530,7 +491,7 @@ function doDiscardOne() -- get a random non-hidden card (from the "choices" table) local num = math.random(1, #choices) - placeOrMergeIntoDeck(hand[choices[num]], returnGlobalDiscardPosition(), self.getRotation()) + deckLib.placeOrMergeIntoDeck(hand[choices[num]], returnGlobalDiscardPosition(), self.getRotation()) broadcastToAll(playerColor .. " randomly discarded card " .. choices[num] .. "/" .. #hand .. ".", "White") end end diff --git a/src/util/DeckLib.ttslua b/src/util/DeckLib.ttslua new file mode 100644 index 00000000..15c24ece --- /dev/null +++ b/src/util/DeckLib.ttslua @@ -0,0 +1,47 @@ +do + local DeckLib = {} + local searchLib = require("util/SearchLib") + + -- places a card/deck at a position or merges into an existing deck + ---@param obj TTSObject Object to move + ---@param pos Table New position for the object + ---@param rot Table New rotation for the object (optional) + DeckLib.placeOrMergeIntoDeck = function(obj, pos, rot) + if obj == nil or pos == nil then return end + + -- search the new position for existing card/deck + local searchResult = searchLib.atPosition(pos, "isCardOrDeck") + + -- get new position + local newPos + local offset = 0.5 + if #searchResult == 1 then + local bounds = searchResult[1].getBounds() + newPos = Vector(pos):setAt("y", bounds.center.y + bounds.size.y / 2 + offset) + else + newPos = Vector(pos) + Vector(0, offset, 0) + end + + -- allow moving the objects smoothly out of the hand + obj.use_hands = false + + if rot then + obj.setRotationSmooth(rot, false, true) + end + obj.setPositionSmooth(newPos, false, true) + + -- continue if the card stops smooth moving + Wait.condition( + function() + obj.use_hands = true + -- this avoids a TTS bug that merges unrelated cards that are not resting + if #searchResult == 1 and searchResult[1] ~= obj then + -- call this with avoiding errors (physics is sometimes too fast so the object doesn't exist for the put) + pcall(function() searchResult[1].putObject(obj) end) + end + end, + function() return not obj.isSmoothMoving() end, 3) + end + + return DeckLib +end