SCED/src/core/PlayArea.ttslua

140 lines
4.2 KiB
Plaintext
Raw Normal View History

2022-11-12 11:46:05 +01:00
---------------------------------------------------------
-- general setup
---------------------------------------------------------
-- set true to enable debug logging
DEBUG = false
2022-11-12 11:46:05 +01:00
-- we use this to turn off collision handling until onLoad() is complete
COLLISION_ENABLED = false
2023-01-01 17:29:24 +01:00
-- used for recreating the link to a custom data helper after image change
customDataHelper = nil
local SHIFT_OFFSETS = {
left = { x = 0.00, y = 0, z = 7.67 },
right = { x = 0.00, y = 0, z = -7.67 },
up = { x = 6.54, y = 0, z = 0.00 },
down = { x = -6.54, y = 0, z = 0.00 }
}
local SHIFT_EXCLUSION = {
["b7b45b"] = true,
["f182ee"] = true,
["721ba2"] = true
}
2023-01-01 23:35:16 +01:00
local tokenManager = require("core/token/TokenManager")
local INVESTIGATOR_COUNTER_GUID = "f182ee"
local PLAY_AREA_ZONE_GUID = "a2f932"
2023-01-01 23:35:16 +01:00
local lastCollisionObject
local currentScenario
2022-11-13 01:12:01 +01:00
2022-11-12 11:46:05 +01:00
---------------------------------------------------------
-- general code
---------------------------------------------------------
function onSave()
return JSON.encode({
currentScenario = currentScenario
})
end
function onLoad(saveState)
2022-11-12 11:46:05 +01:00
-- records locations we have spawned clues for
local saveData = JSON.decode(saveState) or {}
currentScenario = saveData.currentScenario
2022-11-12 11:46:05 +01:00
self.interactable = DEBUG
Wait.time(function() COLLISION_ENABLED = true end, 1)
end
2022-11-12 11:46:05 +01:00
function log(message)
if DEBUG then print(message) end
end
-- Called by Custom Data Helpers to push their location data into the Data Helper. This adds the
-- data to the local token manager instance.
---@param args Table Single-value array holding the GUID of the Custom Data Helper making the call
function updateLocations(args)
2023-01-01 17:29:24 +01:00
customDataHelper = getObjectFromGUID(args[1])
if customDataHelper ~= nil then
tokenManager.addLocationData(customDataHelper.getTable("LOCATIONS_DATA"))
2022-11-12 11:46:05 +01:00
end
end
function onCollisionEnter(collision_info)
2023-01-01 23:35:16 +01:00
local obj = collision_info.collision_object
-- only trigger the following once every 0.1s for each object
-- (otherwise TTS fires this event 20 times and that causes 20 JSON decodes)
if not COLLISION_ENABLED or lastCollisionObject == obj then return end
lastCollisionObject = obj
Wait.time(function() lastCollisionObject = nil end, 0.1)
2022-11-12 11:46:05 +01:00
-- check if we should spawn clues here and do so according to playercount
2023-01-01 23:35:16 +01:00
if shouldSpawnTokens(obj) then
tokenManager.spawnForCard(obj)
end
end
function shouldSpawnTokens(card)
local metadata = JSON.decode(card.getGMNotes())
if metadata == nil then
2023-01-01 23:35:16 +01:00
return tokenManager.hasLocationData(card)
end
return metadata.type == "Location"
or metadata.type == "Enemy"
or metadata.type == "Treachery"
or metadata.weakness
end
2022-12-13 02:15:16 -08:00
-- Move all contents on the play area (cards, tokens, etc) one slot in the given direction. Certain
2023-01-01 23:35:16 +01:00
-- fixed objects will be ignored, as will anything the player has tagged with 'displacement_excluded'
---@param playerColor String Color of the player requesting the shift. Used solely to send an error
--- message in the unlikely case that the scripting zone has been deleted
function shiftContentsUp(playerColor)
shiftContents(playerColor, "up")
end
function shiftContentsDown(playerColor)
shiftContents(playerColor, "down")
end
function shiftContentsLeft(playerColor)
shiftContents(playerColor, "left")
end
function shiftContentsRight(playerColor)
shiftContents(playerColor, "right")
end
function shiftContents(playerColor, direction)
local zone = getObjectFromGUID(PLAY_AREA_ZONE_GUID)
if not zone then
broadcastToColor("Scripting zone couldn't be found.", playerColor, "Red")
return
end
for _, object in ipairs(zone.getObjects()) do
if not (SHIFT_EXCLUSION[object.getGUID()] or object.hasTag("displacement_excluded")) then
object.translate(SHIFT_OFFSETS[direction])
end
end
end
-- Returns the current value of the investigator counter from the playmat
---@return Integer. Number of investigators currently set on the counter
function getInvestigatorCount()
local investigatorCounter = getObjectFromGUID("f182ee")
return investigatorCounter.getVar("val")
end
-- Reset the play area's tracking of which cards have had tokens spawned.
function resetSpawnedCards()
spawnedLocationGUIDs = {}
end
function onScenarioChanged(scenarioName)
currentScenario = scenarioName
end