SCED/src/chaosbag/ChaosBagManager.ttslua
2024-02-15 23:01:14 +01:00

86 lines
2.3 KiB
Plaintext

local chaosBagApi = require("chaosbag/ChaosBagApi")
local TOKEN_IDS = {
-- first row
"p1", "0", "m1", "m2", "m3", "m4",
-- second row
"m5", "m6", "m7", "m8", "frost",
-- third row
"blue", "skull", "cultist", "tablet", "elder", "red"
}
local BUTTON_TOOLTIP = {
-- first row
"+1", "0", "-1", "-2", "-3", "-4",
-- second row
"-5", "-6", "-7", "-8", "Frost",
-- third row
"Elder Sign", "Skull", "Cultist", "Tablet", "Elder Thing", "Auto-fail"
}
local BUTTON_POSITION_X = {
-- first row
-0.9, -0.54, -0.18, 0.18, 0.54, 0.9,
-- second row
-0.9, -0.54, -0.18, 0.18, 0.9,
-- third row
-0.9, -0.54, -0.18, 0.18, 0.54, 0.9
}
local BUTTON_POSITION_Z = { -0.298, 0.05, 0.399 }
-- common button parameters
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 160
buttonParameters.height = 160
function onLoad()
-- create buttons for tokens
for i = 1, #BUTTON_POSITION_X do
local funcName = "buttonClick" .. i
self.setVar(funcName, function(_, _, isRightClick) buttonClick(i, isRightClick) end)
buttonParameters.click_function = funcName
buttonParameters.tooltip = BUTTON_TOOLTIP[i]
buttonParameters.position = { x = BUTTON_POSITION_X[i], y = 0 }
if i < 7 then
buttonParameters.position.z = BUTTON_POSITION_Z[1]
elseif i < 12 then
buttonParameters.position.z = BUTTON_POSITION_Z[2]
else
buttonParameters.position.z = BUTTON_POSITION_Z[3]
end
self.createButton(buttonParameters)
end
end
-- click function for buttons
function buttonClick(index, isRightClick)
local tokenId = TOKEN_IDS[index]
if isRightClick then
chaosBagApi.removeChaosToken(tokenId)
else
local tokens = {}
local name = BUTTON_TOOLTIP[index]
local chaosbag = chaosBagApi.findChaosBag()
for _, v in ipairs(chaosbag.getObjects()) do
if v.name == name then table.insert(tokens, v.guid) end
end
-- spawn token (only 8 frost tokens allowed)
if tokenId == "frost" and #tokens == 8 then
printToAll("The maximum of 8 Frost tokens is already in the bag.", "Yellow")
return
end
chaosBagApi.spawnChaosToken(tokenId)
printToAll("Adding " .. name .. " token (in bag: " .. #tokens + 1 .. ")", "White")
end
end