SCED/src/accessories/HandHelper.ttslua

111 lines
3.4 KiB
Plaintext
Raw Normal View History

2022-12-22 07:45:47 -05:00
local playmatAPI = require("playermat/PlaymatApi")
local matColor
local handColor
local loopId
function onLoad()
local buttonParamaters = {}
buttonParamaters.function_owner = self
-- index 0: button as hand size label
buttonParamaters.hover_color = "White"
buttonParamaters.click_function = "none"
buttonParamaters.position = { 0, 0.11, -0.4 }
buttonParamaters.height = 0
buttonParamaters.width = 0
buttonParamaters.font_size = 500
buttonParamaters.font_color = "White"
self.createButton(buttonParamaters)
-- index 1: button to toggle "des"
buttonParamaters.label = "DES: ✗"
buttonParamaters.click_function = "none"
buttonParamaters.position = { 0, 0.11, 0.25 }
buttonParamaters.height = 0
buttonParamaters.width = 0
buttonParamaters.font_size = 120
self.createButton(buttonParamaters)
-- index 2: button to discard a card
buttonParamaters.label = "discard random card"
buttonParamaters.click_function = "discardRandom"
buttonParamaters.position = { 0, 0.11, 0.7 }
buttonParamaters.height = 175
buttonParamaters.width = 900
buttonParamaters.font_size = 90
buttonParamaters.font_color = "Black"
self.createButton(buttonParamaters)
updateColors()
-- start loop to update card count
loopId = Wait.time(updateValue, 1, -1)
2022-11-11 02:59:55 -05:00
end
-- updates colors when object is dropped somewhere
function onDrop() updateColors() end
-- toggles counting method briefly
2022-11-11 02:59:55 -05:00
function onObjectHover(hover_color, obj)
-- only continue if correct player hovers over "self"
if obj ~= self or hover_color ~= handColor then return end
-- stop loop, toggle "des" and displayed value briefly, then start new loop after 2s
Wait.stop(loopId)
updateValue(true)
Wait.time(function() loopId = Wait.time(updateValue, 1, -1) end, 2)
2022-11-11 02:59:55 -05:00
end
-- updates the matcolor and handcolor variable
function updateColors()
matColor = playmatAPI.getMatColorByPosition(self.getPosition())
handColor = playmatAPI.getHandColor(matColor)
self.setName(handColor .. " Hand Helper")
2022-11-11 02:59:55 -05:00
end
-- count cards in hand (by name for DES)
function updateValue(toggle)
-- update colors if handColor doesn't own a handzone
if Player[handColor].getHandCount() == 0 then
updateColors()
end
-- if there is still no handzone, then end here
if Player[handColor].getHandCount() == 0 then return end
-- get state of "Dream-Enhancing Serum" from playermat and update button label
local des = playmatAPI.isDES(matColor)
if toggle then des = not des end
self.editButton({ index = 1, label = "DES: " .. (des and "✓" or "✗") })
-- count cards in hand
local hand = Player[handColor].getHandObjects()
local size = 0
if des then
local cardHash = {}
for _, obj in pairs(hand) do
if obj.tag == "Card" then
local name = obj.getName()
local title = string.match(name, '(.+)(%s%(%d+%))') or name
cardHash[title] = true
end
2022-11-11 02:59:55 -05:00
end
for _, title in pairs(cardHash) do
size = size + 1
2022-11-11 02:59:55 -05:00
end
else
for _, obj in pairs(hand) do
if obj.tag == "Card" then size = size + 1 end
2022-11-11 02:59:55 -05:00
end
end
-- update button label and color
self.editButton({ index = 0, font_color = des and "Green" or "White", label = size })
2022-11-11 02:59:55 -05:00
end
-- discards a random non-hidden card from hand
function discardRandom()
playmatAPI.doDiscardOne(matColor)
2022-11-11 02:59:55 -05:00
end