context menu functions

This commit is contained in:
Chr1Z93 2023-02-03 15:06:01 +01:00
parent 8adc197a5c
commit 28ad9da9ab
4 changed files with 95 additions and 6 deletions

View File

@ -33,8 +33,8 @@
"IgnoreFoW": false,
"LayoutGroupSortIndex": 0,
"Locked": false,
"LuaScript": "",
"LuaScriptState": "",
"LuaScript_path": "AllPlayerCards.15bb07/FamilyInheritance.394603.ttslua",
"MeasureMovement": false,
"Name": "Card",
"Nickname": "Family Inheritance",

View File

@ -0,0 +1,76 @@
local tokenManager = require("core/token/TokenManager")
local playmatApi = require("playermat/PlaymatApi")
local clickableResourceCounter = nil
local foundTokens = 0
function onLoad()
self.addContextMenuItem("Add 4 resources", function(playerColor) add4(playerColor) end)
self.addContextMenuItem("Take all resources", function(playerColor) takeAll(playerColor) end)
self.addContextMenuItem("Discard all resources", function(playerColor) loseAll(playerColor) end)
end
function searchSelf()
clickableResourceCounter = nil
foundTokens = 0
for _, obj in ipairs(searchArea(self.getPosition(), { 2.5, 0.5, 3.5 })) do
local obj = obj.hit_object
if obj.getCustomObject().image ==
"http://cloud-3.steamusercontent.com/ugc/1758068501357192910/11DDDC7EF621320962FDCF3AE3211D5EDC3D1573/" then
foundTokens = foundTokens + math.abs(obj.getQuantity())
obj.destruct()
elseif obj.getMemo() == "resourceCounter" then
foundTokens = obj.getVar("val")
clickableResourceCounter = obj
return
end
end
end
function add4(playerColor)
searchSelf()
local newCount = foundTokens + 4
if clickableResourceCounter then
clickableResourceCounter.call("updateVal", newCount)
else
if newCount > 12 then
printToColor("Count increased to " .. newCount .. " resources. Spawning clickable counter instead.", playerColor)
tokenManager.spawnResourceCounterToken(self, newCount)
else
tokenManager.spawnTokenGroup(self, "resource", newCount)
end
end
end
function takeAll(playerColor)
searchSelf()
local matColor = playmatApi.getMatColorByPosition(self.getPosition())
playmatApi.gainResources(foundTokens, matColor)
if clickableResourceCounter then
clickableResourceCounter.call("updateVal", 0)
end
printToColor("Moved " .. foundTokens .. " resource(s) to " .. matColor .. "'s resource pool.", playerColor)
end
function loseAll(playerColor)
searchSelf()
if clickableResourceCounter then
clickableResourceCounter.call("updateVal", 0)
end
printToColor("Discarded " .. foundTokens .. " resource(s).", playerColor)
end
function searchArea(origin, size)
return Physics.cast({
origin = origin,
direction = { 0, 1, 0 },
orientation = PLAY_ZONE_ROTATION,
type = 3,
size = size,
max_distance = 1
})
end

View File

@ -287,13 +287,12 @@ function doUpkeep(_, color, alt_click)
end
end
-- gain a resource
RESOURCE_COUNTER.call("addOrSubtract")
-- gain an additional resource for Jenny Barnes
-- gain a resource (or two if playing Jenny Barnes)
if string.match(activeInvestigatorId, "%d%d%d%d%d") == "02003" then
RESOURCE_COUNTER.call("addOrSubtract")
gainResources(2)
printToColor("Gaining 2 resources (Jenny)", messageColor)
else
gainResources(1)
end
-- draw a card (with handling for Patrice and Forced Learning)
@ -316,6 +315,13 @@ function doUpkeep(_, color, alt_click)
end
end
-- adds the specified amount of resources to the resource counter
function gainResources(amount)
local count = RESOURCE_COUNTER.getVar("val")
local add = tonumber(amount) or 0
RESOURCE_COUNTER.call("updateVal", count + add)
end
-- function for "draw 1 button" (that can be added via option panel)
function doDrawOne(_, color)
setMessageColor(color)

View File

@ -109,6 +109,13 @@ do
return count
end
-- Adds the specified amount of resources to the requested playermat's resource counter
PlaymatApi.gainResources = function(amount, matColor)
for _, mat in ipairs(internal.getMatForColor(matColor)) do
mat.call("gainResources", amount)
end
end
-- Convenience function to look up a mat's object by color, or get all mats.
---@param matColor String for one of the active player colors - White, Orange, Green, Red. Also
-- accepts "All" as a special value which will return all four mats.