SCED/src/accessories/SearchAssistant.ttslua
2023-12-11 18:11:04 +01:00

189 lines
6.1 KiB
Plaintext

local searchLib = require("util/SearchLib")
local playmatApi = require("playermat/PlaymatApi")
-- forward declaration of variables that are used across functions
local matColor, handColor, setAsidePosition, setAsideRotation, drawDeckPosition, topCardDetected
local quickParameters = {}
quickParameters.function_owner = self
quickParameters.font_size = 165
quickParameters.width = 275
quickParameters.height = 275
quickParameters.color = "White"
-- common parameters
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.font_size = 125
buttonParameters.width = 650
buttonParameters.height = 225
buttonParameters.color = "White"
local inputParameters = {}
inputParameters.function_owner = self
inputParameters.input_function = "updateSearchNumber"
inputParameters.tooltip = "custom search amount"
inputParameters.label = "#"
inputParameters.font_size = 175
inputParameters.width = 400
inputParameters.height = inputParameters.font_size + 23
inputParameters.position = { 0, 0.11, 0 }
inputParameters.alignment = 3
inputParameters.validation = 2
function onLoad()
normalView()
end
-- regular view with search box
function normalView()
self.clearButtons()
self.clearInputs()
self.createInput(inputParameters)
-- create custom search button
buttonParameters.click_function = "searchCustom"
buttonParameters.tooltip = "Search the entered number of cards"
buttonParameters.position = { 0, 0.11, 0.65 }
buttonParameters.label = "Search"
self.createButton(buttonParameters)
-- create buttons to search 3, 6 or 9 cards
quickParameters.click_function = "search3"
quickParameters.label = "3"
quickParameters.position = { -0.65, 0.11, -0.65 }
self.createButton(quickParameters)
quickParameters.click_function = "search6"
quickParameters.label = "6"
quickParameters.position = { 0, 0.11, -0.65 }
self.createButton(quickParameters)
quickParameters.click_function = "search9"
quickParameters.label = "9"
quickParameters.position = { 0.65, 0.11, -0.65 }
self.createButton(quickParameters)
end
-- click functions
function search3(_, playerColor) startSearch(playerColor, 3) end
function search6(_, playerColor) startSearch(playerColor, 6) end
function search9(_, playerColor) startSearch(playerColor, 9) end
-- view during a search with "done" buttons
function searchView()
self.clearButtons()
self.clearInputs()
-- create the "End Search" button
buttonParameters.click_function = "endSearch"
buttonParameters.tooltip = "Left-click: Return cards and shuffle\nRight-click: Return cards without shuffling"
buttonParameters.position = { 0, 0.11, 0 }
buttonParameters.label = "End Search"
self.createButton(buttonParameters)
end
-- input_function to get number of cards to search
function updateSearchNumber(_, _, input)
inputParameters.value = tonumber(input)
end
-- starts the search with the number from the input field
function searchCustom(_, messageColor)
local number = inputParameters.value
if number ~= nil then
startSearch(messageColor, number)
else
printToColor("Enter the number of cards to search in the textbox.", messageColor, "Orange")
end
end
-- start the search (change UI, set handCards aside, draw cards)
function startSearch(messageColor, number)
matColor = playmatApi.getMatColorByPosition(self.getPosition())
handColor = playmatApi.getPlayerColor(matColor)
topCardDetected = false
-- get draw deck
local deckAreaObjects = playmatApi.getDeckAreaObjects(matColor)
if deckAreaObjects.draw == nil then
printToColor(matColor .. " draw deck could not be found!", messageColor, "Red")
return
end
-- get bounds to know the height of the deck
local bounds = deckAreaObjects.draw.getBounds()
drawDeckPosition = bounds.center + Vector(0, bounds.size.y / 2 + 0.2, 0)
printToColor("Place target(s) of search on set aside hand.", messageColor, "Green")
-- get playmat orientation
local offset = -15
if matColor == "Orange" or matColor == "Red" then
offset = 15
end
-- get position and rotation for set aside cards
local handData = Player[handColor].getHandTransform()
local handCards = Player[handColor].getHandObjects()
setAsidePosition = handData.position + offset * handData.right
setAsideRotation = { handData.rotation.x, handData.rotation.y + 180, 180 }
-- set y-value
setAsidePosition.y = 1.5
for i = #handCards, 1, -1 do
handCards[i].setPosition(setAsidePosition + Vector(0, (#handCards - i) * 0.1, 0))
handCards[i].setRotation(setAsideRotation)
end
-- handling for Norman Withers
if deckAreaObjects.topCard then
deckAreaObjects.topCard.flip()
topCardDetected = true
end
searchView()
Wait.time(function()
deckAreaObjects = playmatApi.getDeckAreaObjects(matColor)
deckAreaObjects.draw.deal(number, handColor)
end, 1)
end
-- place handCards back into deck and optionally shuffle
function endSearch(_, _, isRightClick)
local handCards = Player[handColor].getHandObjects()
for i = #handCards, 1, -1 do
handCards[i].setPosition(drawDeckPosition + Vector(0, (#handCards - i) * 0.1, 0))
handCards[i].setRotation(setAsideRotation)
end
-- draw set aside cards (from the ground!)
for _, obj in ipairs(searchLib.atPosition(setAsidePosition), "isCardOrDeck") do
if obj.type == "Deck" then
Wait.time(function() obj.deal(#obj.getObjects(), handColor) end, 1)
elseif obj.type == "Card" then
obj.setPosition(Player[handColor].getHandTransform().position)
obj.flip()
end
end
normalView()
-- delay is to wait for cards to enter deck
if not isRightClick then
Wait.time(function()
local deckAreaObjects = playmatApi.getDeckAreaObjects(matColor)
if deckAreaObjects.draw then
deckAreaObjects.draw.shuffle()
end
end, #handCards * 0.1)
end
-- Norman Withers handling
if topCardDetected then
Wait.time(function() playmatApi.flipTopCardFromDeck(matColor) end, #handCards * 0.1)
end
end