SCED/src/accessories/SearchAssistant.ttslua

189 lines
6.1 KiB
Plaintext
Raw Normal View History

2023-12-11 12:11:04 -05:00
local searchLib = require("util/SearchLib")
local playmatApi = require("playermat/PlaymatApi")
2022-11-11 02:59:55 -05:00
2023-01-05 20:02:24 -05:00
-- forward declaration of variables that are used across functions
2023-10-21 06:47:29 -04:00
local matColor, handColor, setAsidePosition, setAsideRotation, drawDeckPosition, topCardDetected
2023-01-05 20:02:24 -05:00
local quickParameters = {}
quickParameters.function_owner = self
2023-01-06 04:36:57 -05:00
quickParameters.font_size = 165
2023-01-05 20:02:24 -05:00
quickParameters.width = 275
quickParameters.height = 275
quickParameters.color = "White"
2022-11-11 02:59:55 -05:00
-- common parameters
2022-11-12 09:36:48 -05:00
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.font_size = 125
buttonParameters.width = 650
buttonParameters.height = 225
2023-01-05 20:02:24 -05:00
buttonParameters.color = "White"
2022-11-12 09:36:48 -05:00
local inputParameters = {}
inputParameters.function_owner = self
inputParameters.input_function = "updateSearchNumber"
2023-01-05 20:02:24 -05:00
inputParameters.tooltip = "custom search amount"
2022-11-12 09:36:48 -05:00
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
2023-01-05 20:02:24 -05:00
function onLoad()
normalView()
2022-11-11 02:59:55 -05:00
end
2023-01-05 20:02:24 -05:00
-- regular view with search box
2022-11-11 02:59:55 -05:00
function normalView()
2023-01-05 20:02:24 -05:00
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
2023-01-06 04:36:57 -05:00
quickParameters.click_function = "search3"
quickParameters.label = "3"
quickParameters.position = { -0.65, 0.11, -0.65 }
self.createButton(quickParameters)
2022-11-11 02:59:55 -05:00
2023-01-06 04:36:57 -05:00
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)
2022-11-11 02:59:55 -05:00
end
2023-01-06 04:36:57 -05:00
-- click functions
function search3(_, playerColor) startSearch(playerColor, 3) end
function search6(_, playerColor) startSearch(playerColor, 6) end
function search9(_, playerColor) startSearch(playerColor, 9) end
2022-11-11 02:59:55 -05:00
-- view during a search with "done" buttons
function searchView()
2023-01-05 20:02:24 -05:00
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)
2022-11-11 02:59:55 -05:00
end
-- input_function to get number of cards to search
function updateSearchNumber(_, _, input)
2023-01-05 20:02:24 -05:00
inputParameters.value = tonumber(input)
2022-11-11 02:59:55 -05:00
end
2023-01-05 20:02:24 -05:00
-- 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
2022-11-11 02:59:55 -05:00
end
2023-01-05 20:02:24 -05:00
-- start the search (change UI, set handCards aside, draw cards)
function startSearch(messageColor, number)
matColor = playmatApi.getMatColorByPosition(self.getPosition())
handColor = playmatApi.getPlayerColor(matColor)
2023-10-21 06:47:29 -04:00
topCardDetected = false
2023-01-05 20:02:24 -05:00
-- get draw deck
2023-10-21 17:17:57 -04:00
local deckAreaObjects = playmatApi.getDeckAreaObjects(matColor)
if deckAreaObjects.draw == nil then
2023-01-05 20:02:24 -05:00
printToColor(matColor .. " draw deck could not be found!", messageColor, "Red")
return
end
2023-10-21 17:17:57 -04:00
-- 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)
2023-01-05 20:02:24 -05:00
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()
2023-01-05 20:02:24 -05:00
setAsidePosition = handData.position + offset * handData.right
setAsideRotation = { handData.rotation.x, handData.rotation.y + 180, 180 }
2023-10-21 17:17:57 -04:00
-- set y-value
setAsidePosition.y = 1.5
2023-01-05 20:02:24 -05:00
for i = #handCards, 1, -1 do
2023-10-21 17:17:57 -04:00
handCards[i].setPosition(setAsidePosition + Vector(0, (#handCards - i) * 0.1, 0))
2023-01-05 20:02:24 -05:00
handCards[i].setRotation(setAsideRotation)
end
-- handling for Norman Withers
2023-10-21 17:17:57 -04:00
if deckAreaObjects.topCard then
deckAreaObjects.topCard.flip()
topCardDetected = true
2023-01-05 20:02:24 -05:00
end
2022-11-11 02:59:55 -05:00
2023-01-05 20:02:24 -05:00
searchView()
2023-10-21 17:17:57 -04:00
Wait.time(function()
deckAreaObjects = playmatApi.getDeckAreaObjects(matColor)
deckAreaObjects.draw.deal(number, handColor)
end, 1)
2023-01-05 20:02:24 -05:00
end
2022-11-11 02:59:55 -05:00
2023-01-05 20:02:24 -05:00
-- place handCards back into deck and optionally shuffle
function endSearch(_, _, isRightClick)
local handCards = Player[handColor].getHandObjects()
2023-01-05 20:02:24 -05:00
for i = #handCards, 1, -1 do
2023-10-21 17:17:57 -04:00
handCards[i].setPosition(drawDeckPosition + Vector(0, (#handCards - i) * 0.1, 0))
2023-01-05 20:02:24 -05:00
handCards[i].setRotation(setAsideRotation)
end
-- draw set aside cards (from the ground!)
2023-12-11 12:11:04 -05:00
for _, obj in ipairs(searchLib.atPosition(setAsidePosition), "isCardOrDeck") do
2023-10-21 17:17:57 -04:00
if obj.type == "Deck" then
2023-12-11 12:11:04 -05:00
Wait.time(function() obj.deal(#obj.getObjects(), handColor) end, 1)
2023-10-21 17:17:57 -04:00
elseif obj.type == "Card" then
obj.setPosition(Player[handColor].getHandTransform().position)
2023-01-05 20:02:24 -05:00
obj.flip()
2022-11-11 02:59:55 -05:00
end
2023-01-05 20:02:24 -05:00
end
2022-11-11 02:59:55 -05:00
2023-01-05 20:02:24 -05:00
normalView()
2023-10-21 06:47:29 -04:00
2023-10-21 17:17:57 -04:00
-- 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
2023-10-21 06:47:29 -04:00
-- Norman Withers handling
if topCardDetected then
2023-10-21 17:17:57 -04:00
Wait.time(function() playmatApi.flipTopCardFromDeck(matColor) end, #handCards * 0.1)
2023-10-21 06:47:29 -04:00
end
2022-11-11 02:59:55 -05:00
end