Merge pull request #91 from argonui/require-refactor

Refactor some required files to function as modules
This commit is contained in:
Buhallin 2022-12-11 13:34:35 -08:00 committed by GitHub
commit b0ba445767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 293 additions and 283 deletions

View File

@ -1,9 +1,10 @@
require("playermat/Zones")
require("arkhamdb/LoaderUi")
require("playercards/PlayerCardSpawner")
local bondedList = {}
local customizationRowsWithFields = {}
local zones = require("playermat/Zones")
local bondedList = { }
local customizationRowsWithFields = { }
-- inputMap maps from (our 1-indexes) customization row index to inputValue table index
-- The Raven Quill
customizationRowsWithFields["09042"] = {}
@ -384,7 +385,7 @@ function loadCards(slots, investigatorId, customizations, playerColor, commandMa
local zoneDecks = buildZoneLists(cardsToSpawn)
-- Spawn the list for each zone
for zone, zoneCards in pairs(zoneDecks) do
local deckPos = Zones.getZonePosition(playerColor, zone)
local deckPos = zones.getZonePosition(playerColor, zone)
deckPos.y = 3
local callback = nil
@ -406,7 +407,7 @@ function loadCards(slots, investigatorId, customizations, playerColor, commandMa
Spawner.spawnCards(
zoneCards,
deckPos,
Zones.getDefaultCardRotation(playerColor, zone),
zones.getDefaultCardRotation(playerColor, zone),
true, -- Sort deck
callback)

View File

@ -1,4 +1,4 @@
require("playercards/spawnbag/SpawnBag")
local spawnBag = require("playercards/spawnbag/SpawnBag")
SPAWN_SPEC = {
name = "BondedCards",
@ -27,7 +27,7 @@ function onLoad(savedData)
if (savedData ~= nil) then
local saveState = JSON.decode(savedData)
if (saveState.spawnBagState ~= nil) then
SpawnBag.loadFromSave(saveState.spawnBagState)
spawnBag.loadFromSave(saveState.spawnBagState)
end
end
createActionButtons()
@ -35,7 +35,7 @@ end
function onSave()
local saveState = {
spawnBagState = SpawnBag.getStateForSave(),
spawnBagState = spawnBag.getStateForSave(),
}
return JSON.encode(saveState)
end
@ -68,10 +68,10 @@ function createActionButtons()
end
function buttonClick_place()
SpawnBag.spawn(SPAWN_SPEC)
spawnBag.spawn(SPAWN_SPEC)
end
-- Recalls objects to bag from table
function buttonClick_recall()
SpawnBag.recall()
spawnBag.recall()
end

View File

@ -19,22 +19,22 @@ require("playercards/PlayerCardSpawner")
-- will be moved a predefined distance
-- }
-- See BondedBag.ttslua for an example
do
local SpawnBag = { }
SpawnBag = { }
-- To assist debugging, will draw a box around the recall zone when it's set up
local SHOW_RECALL_ZONE = false
-- To assist debugging, will draw a box around the recall zone when it's set up
local SHOW_RECALL_ZONE = false
local ALL_CARDS_GUID = "15bb07"
local ALL_CARDS_GUID = "15bb07"
-- Distance to expand the recall zone around any added object.
local RECALL_BUFFER_X = 0.9
local RECALL_BUFFER_Z = 0.5
-- Distance to expand the recall zone around any added object.
local RECALL_BUFFER_X = 0.9
local RECALL_BUFFER_Z = 0.5
-- In order to mimic the behavior of the previous memory buttons we use a temporary bag when
-- recalling objects. This bag is tiny and transparent, and will be placed at the same location as
-- this object. Once all placed cards are recalled bag to this bag, it will be destroyed
local RECALL_BAG = {
-- In order to mimic the behavior of the previous memory buttons we use a temporary bag when
-- recalling objects. This bag is tiny and transparent, and will be placed at the same location as
-- this object. Once all placed cards are recalled bag to this bag, it will be destroyed
local RECALL_BAG = {
Name = "Bag",
Transform = {
scaleX = 0.01,
@ -51,32 +51,32 @@ local RECALL_BAG = {
Grid = true,
Snap = false,
Tooltip = false,
}
}
-- Tracks what has been placed by this "bag" so they can be recalled
local placedSpecs = { }
local placedObjectGuids = { }
local recallZone = nil
-- Tracks what has been placed by this "bag" so they can be recalled
local placedSpecs = { }
local placedObjectGuids = { }
local recallZone = nil
-- Loads a table of saved state, extracted during the parent object's onLoad
SpawnBag.loadFromSave = function(saveTable)
-- Loads a table of saved state, extracted during the parent object's onLoad
SpawnBag.loadFromSave = function(saveTable)
placedSpecs = saveTable.placed
placedObjectGuids = saveTable.placedObjects
recallZone = saveTable.recall
end
end
-- Generates a table of save state that can be included in the parent object's onSave
SpawnBag.getStateForSave = function()
-- Generates a table of save state that can be included in the parent object's onSave
SpawnBag.getStateForSave = function()
return {
placed = placedSpecs,
placedObjects = placedObjectGuids,
recall = recallZone,
}
end
end
-- Places the given spawnSpec on the table. See SpawnBag.ttslua header for spawnSpec table data and
-- examples
SpawnBag.spawn = function(spawnSpec)
-- Places the given spawnSpec on the table. See SpawnBag.ttslua header for spawnSpec table data and
-- examples
SpawnBag.spawn = function(spawnSpec)
-- Limit to one placement at a time
if (placedSpecs[spawnSpec.name]) then
return
@ -101,10 +101,10 @@ SpawnBag.spawn = function(spawnSpec)
Spawner.spawnCards(cardsToSpawn, spawnSpec.globalPos, spawnSpec.rotation, false, recordPlacedObject)
end
placedSpecs[spawnSpec.name] = true
end
end
-- Recalls all spawned objects to the bag, and clears the placedObjectGuids list
SpawnBag.recall = function()
-- Recalls all spawned objects to the bag, and clears the placedObjectGuids list
SpawnBag.recall = function()
local trash = spawnObjectData({data = RECALL_BAG, position = self.getPosition()})
for guid, _ in pairs(placedObjectGuids) do
local obj = getObjectFromGUID(guid)
@ -122,18 +122,18 @@ SpawnBag.recall = function()
placedSpecs = { }
placedObjectGuids = { }
recallZone = nil
end
end
-- Callback for when an object has been spawned. Tracks the object for later recall and updates the
-- recall zone.
function recordPlacedObject(spawned)
-- Callback for when an object has been spawned. Tracks the object for later recall and updates the
-- recall zone.
function recordPlacedObject(spawned)
placedObjectGuids[spawned.getGUID()] = true
expandRecallZone(spawned)
end
end
-- Expands the current recall zone based on the position of the given object. The recall zone will
-- be maintained as the bounding box of the extreme object positions, plus a small amount of buffer
function expandRecallZone(spawnedCard)
-- Expands the current recall zone based on the position of the given object. The recall zone will
-- be maintained as the bounding box of the extreme object positions, plus a small amount of buffer
function expandRecallZone(spawnedCard)
local pos = spawnedCard.getPosition()
if (recallZone == nil) then
-- First card out of the bag, initialize surrounding that
@ -185,15 +185,18 @@ function expandRecallZone(spawnedCard)
},
})
end
end
end
-- Checks to see if the given object is in the current recall zone. If there isn't a recall zone,
-- will return true so that everything can be easily cleaned up.
function isInRecallZone(obj)
-- Checks to see if the given object is in the current recall zone. If there isn't a recall zone,
-- will return true so that everything can be easily cleaned up.
function isInRecallZone(obj)
if (recallZone == nil) then
return true
end
local pos = obj.getPosition()
return (pos.x < recallZone.upperLeft.x and pos.x > recallZone.lowerRight.x
and pos.z < recallZone.upperLeft.z and pos.z > recallZone.lowerRight.z)
end
return SpawnBag
end

View File

@ -1,4 +1,4 @@
require("playercards/spawnbag/SpawnBag")
local spawnBag = require("playercards/spawnbag/SpawnBag")
local UPGRADES_SPEC = {
name = "UpgradeSheets",
@ -37,7 +37,7 @@ function onLoad(savedData)
if (savedData ~= nil) then
local saveState = JSON.decode(savedData)
if (saveState.spawnBagState ~= nil) then
SpawnBag.loadFromSave(saveState.spawnBagState)
spawnBag.loadFromSave(saveState.spawnBagState)
end
end
createActionButtons()
@ -45,7 +45,7 @@ end
function onSave()
local saveState = {
spawnBagState = SpawnBag.getStateForSave(),
spawnBagState = spawnBag.getStateForSave(),
}
return JSON.encode(saveState)
end
@ -78,11 +78,11 @@ function createActionButtons()
end
function buttonClick_place()
SpawnBag.spawn(UPGRADES_SPEC)
SpawnBag.spawn(SERVITOR_SPEC)
spawnBag.spawn(UPGRADES_SPEC)
spawnBag.spawn(SERVITOR_SPEC)
end
-- Recalls objects to bag from table
function buttonClick_recall()
SpawnBag.recall()
spawnBag.recall()
end

View File

@ -19,114 +19,117 @@
-- SetAside4: Upgrade sheets for customizable cards
-- SetAside5: Hunch Deck for Joe Diamond
-- SetAside6: currently unused
local playerMatGuids = {}
playerMatGuids["Red"] = "0840d5"
playerMatGuids["Orange"] = "bd0ff4"
playerMatGuids["White"] = "8b081b"
playerMatGuids["Green"] = "383d8b"
do
local Zones = { }
commonZones = {}
commonZones["Investigator"] = { -1.17702, 0, 0.00209 }
commonZones["Minicard"] = { -0.16, 0, -1.222326 }
commonZones["Deck"] = { -1.822724, 0, -0.02940192 }
commonZones["Discard"] = { -1.822451, 0, 0.6092291 }
commonZones["Ally"] = { -0.6157398, 0, 0.02435675 }
commonZones["Body"] = { -0.6306521, 0, 0.553170 }
commonZones["Hand1"] = { 0.2155387, 0, 0.04257287 }
commonZones["Hand2"] = { -0.1803701, 0, 0.03745948 }
commonZones["Arcane1"] = { 0.2124223, 0, 0.5596902 }
commonZones["Arcane2"] = { -0.1711275, 0, 0.5567944 }
commonZones["Tarot"] = { 0.6016169, 0, 0.03273106 }
commonZones["Accessory"] = { 0.6049907, 0, 0.5546234 }
commonZones["BlankTop"] = { 1.758446, 0, 0.03965336 }
commonZones["BlankBottom"] = { 1.754469, 0, 0.5634764 }
commonZones["Threat1"] = { -0.9116555, 0, -0.6446251 }
commonZones["Threat2"] = { -0.4544126, 0, -0.6428719 }
commonZones["Threat3"] = { 0.002246313, 0, -0.6430681 }
commonZones["Threat4"] = { 0.4590618, 0, -0.6432732 }
local playerMatGuids = {}
playerMatGuids["Red"] = "0840d5"
playerMatGuids["Orange"] = "bd0ff4"
playerMatGuids["White"] = "8b081b"
playerMatGuids["Green"] = "383d8b"
Zones = {}
Zones["White"] = {}
Zones["White"]["Investigator"] = commonZones["Investigator"]
Zones["White"]["Minicard"] = commonZones["Minicard"]
Zones["White"]["Deck"] = commonZones["Deck"]
Zones["White"]["Discard"] = commonZones["Discard"]
Zones["White"]["Ally"] = commonZones["Ally"]
Zones["White"]["Body"] = commonZones["Body"]
Zones["White"]["Hand1"] = commonZones["Hand1"]
Zones["White"]["Hand2"] = commonZones["Hand2"]
Zones["White"]["Arcane1"] = commonZones["Arcane1"]
Zones["White"]["Arcane2"] = commonZones["Arcane2"]
Zones["White"]["Tarot"] = commonZones["Tarot"]
Zones["White"]["Accessory"] = commonZones["Accessory"]
Zones["White"]["BlankTop"] = commonZones["BlankTop"]
Zones["White"]["BlankBottom"] = commonZones["BlankBottom"]
Zones["White"]["Threat1"] = commonZones["Threat1"]
Zones["White"]["Threat2"] = commonZones["Threat2"]
Zones["White"]["Threat3"] = commonZones["Threat3"]
Zones["White"]["Threat4"] = commonZones["Threat4"]
Zones["White"]["SetAside1"] = { 2.345893, 0, -0.520315 }
Zones["White"]["SetAside2"] = { 2.345893, 0, 0.042552 }
Zones["White"]["SetAside3"] = { 2.345893, 0, 0.605419 }
Zones["White"]["UnderSetAside3"] = { 2.495893, 0, 0.805419 }
Zones["White"]["SetAside4"] = { 2.775893, 0, -0.520315 }
Zones["White"]["SetAside5"] = { 2.775893, 0, 0.042552 }
Zones["White"]["SetAside6"] = { 2.775893, 0, 0.605419 }
Zones["White"]["UnderSetAside6"] = { 2.925893, 0, 0.805419 }
local commonZones = {}
commonZones["Investigator"] = { -1.17702, 0, 0.00209 }
commonZones["Minicard"] = { -0.16, 0, -1.222326 }
commonZones["Deck"] = { -1.822724, 0, -0.02940192 }
commonZones["Discard"] = { -1.822451, 0, 0.6092291 }
commonZones["Ally"] = { -0.6157398, 0, 0.02435675 }
commonZones["Body"] = { -0.6306521, 0, 0.553170 }
commonZones["Hand1"] = { 0.2155387, 0, 0.04257287 }
commonZones["Hand2"] = { -0.1803701, 0, 0.03745948 }
commonZones["Arcane1"] = { 0.2124223, 0, 0.5596902 }
commonZones["Arcane2"] = { -0.1711275, 0, 0.5567944 }
commonZones["Tarot"] = { 0.6016169, 0, 0.03273106 }
commonZones["Accessory"] = { 0.6049907, 0, 0.5546234 }
commonZones["BlankTop"] = { 1.758446, 0, 0.03965336 }
commonZones["BlankBottom"] = { 1.754469, 0, 0.5634764 }
commonZones["Threat1"] = { -0.9116555, 0, -0.6446251 }
commonZones["Threat2"] = { -0.4544126, 0, -0.6428719 }
commonZones["Threat3"] = { 0.002246313, 0, -0.6430681 }
commonZones["Threat4"] = { 0.4590618, 0, -0.6432732 }
Zones["Orange"] = {}
Zones["Orange"]["Investigator"] = commonZones["Investigator"]
Zones["Orange"]["Minicard"] = commonZones["Minicard"]
Zones["Orange"]["Deck"] = commonZones["Deck"]
Zones["Orange"]["Discard"] = commonZones["Discard"]
Zones["Orange"]["Ally"] = commonZones["Ally"]
Zones["Orange"]["Body"] = commonZones["Body"]
Zones["Orange"]["Hand1"] = commonZones["Hand1"]
Zones["Orange"]["Hand2"] = commonZones["Hand2"]
Zones["Orange"]["Arcane1"] = commonZones["Arcane1"]
Zones["Orange"]["Arcane2"] = commonZones["Arcane2"]
Zones["Orange"]["Tarot"] = commonZones["Tarot"]
Zones["Orange"]["Accessory"] = commonZones["Accessory"]
Zones["Orange"]["BlankTop"] = commonZones["BlankTop"]
Zones["Orange"]["BlankBottom"] = commonZones["BlankBottom"]
Zones["Orange"]["Threat1"] = commonZones["Threat1"]
Zones["Orange"]["Threat2"] = commonZones["Threat2"]
Zones["Orange"]["Threat3"] = commonZones["Threat3"]
Zones["Orange"]["Threat4"] = commonZones["Threat4"]
Zones["Orange"]["SetAside1"] = { -2.350362, 0, -0.520315 }
Zones["Orange"]["SetAside2"] = { -2.350362, 0, 0.042552 }
Zones["Orange"]["SetAside3"] = { -2.350362, 0, 0.605419 }
Zones["Orange"]["UnderSetAside3"] = { -2.500362, 0, 0.80419 }
Zones["Orange"]["SetAside4"] = { -2.7803627, 0, -0.520315 }
Zones["Orange"]["SetAside5"] = { -2.7803627, 0, 0.042552 }
Zones["Orange"]["SetAside6"] = { -2.7803627, 0, 0.605419 }
Zones["Orange"]["UnderSetAside6"] = { -2.9303627, 0, 0.80419 }
local zoneData = {}
zoneData["White"] = {}
zoneData["White"]["Investigator"] = commonZones["Investigator"]
zoneData["White"]["Minicard"] = commonZones["Minicard"]
zoneData["White"]["Deck"] = commonZones["Deck"]
zoneData["White"]["Discard"] = commonZones["Discard"]
zoneData["White"]["Ally"] = commonZones["Ally"]
zoneData["White"]["Body"] = commonZones["Body"]
zoneData["White"]["Hand1"] = commonZones["Hand1"]
zoneData["White"]["Hand2"] = commonZones["Hand2"]
zoneData["White"]["Arcane1"] = commonZones["Arcane1"]
zoneData["White"]["Arcane2"] = commonZones["Arcane2"]
zoneData["White"]["Tarot"] = commonZones["Tarot"]
zoneData["White"]["Accessory"] = commonZones["Accessory"]
zoneData["White"]["BlankTop"] = commonZones["BlankTop"]
zoneData["White"]["BlankBottom"] = commonZones["BlankBottom"]
zoneData["White"]["Threat1"] = commonZones["Threat1"]
zoneData["White"]["Threat2"] = commonZones["Threat2"]
zoneData["White"]["Threat3"] = commonZones["Threat3"]
zoneData["White"]["Threat4"] = commonZones["Threat4"]
zoneData["White"]["SetAside1"] = { 2.345893, 0, -0.520315 }
zoneData["White"]["SetAside2"] = { 2.345893, 0, 0.042552 }
zoneData["White"]["SetAside3"] = { 2.345893, 0, 0.605419 }
zoneData["White"]["UnderSetAside3"] = { 2.495893, 0, 0.805419 }
zoneData["White"]["SetAside4"] = { 2.775893, 0, -0.520315 }
zoneData["White"]["SetAside5"] = { 2.775893, 0, 0.042552 }
zoneData["White"]["SetAside6"] = { 2.775893, 0, 0.605419 }
zoneData["White"]["UnderSetAside6"] = { 2.925893, 0, 0.805419 }
-- Green positions are the same as White and Red the same as Orange
Zones["Red"] = Zones["Orange"]
Zones["Green"] = Zones["White"]
zoneData["Orange"] = {}
zoneData["Orange"]["Investigator"] = commonZones["Investigator"]
zoneData["Orange"]["Minicard"] = commonZones["Minicard"]
zoneData["Orange"]["Deck"] = commonZones["Deck"]
zoneData["Orange"]["Discard"] = commonZones["Discard"]
zoneData["Orange"]["Ally"] = commonZones["Ally"]
zoneData["Orange"]["Body"] = commonZones["Body"]
zoneData["Orange"]["Hand1"] = commonZones["Hand1"]
zoneData["Orange"]["Hand2"] = commonZones["Hand2"]
zoneData["Orange"]["Arcane1"] = commonZones["Arcane1"]
zoneData["Orange"]["Arcane2"] = commonZones["Arcane2"]
zoneData["Orange"]["Tarot"] = commonZones["Tarot"]
zoneData["Orange"]["Accessory"] = commonZones["Accessory"]
zoneData["Orange"]["BlankTop"] = commonZones["BlankTop"]
zoneData["Orange"]["BlankBottom"] = commonZones["BlankBottom"]
zoneData["Orange"]["Threat1"] = commonZones["Threat1"]
zoneData["Orange"]["Threat2"] = commonZones["Threat2"]
zoneData["Orange"]["Threat3"] = commonZones["Threat3"]
zoneData["Orange"]["Threat4"] = commonZones["Threat4"]
zoneData["Orange"]["SetAside1"] = { -2.350362, 0, -0.520315 }
zoneData["Orange"]["SetAside2"] = { -2.350362, 0, 0.042552 }
zoneData["Orange"]["SetAside3"] = { -2.350362, 0, 0.605419 }
zoneData["Orange"]["UnderSetAside3"] = { -2.500362, 0, 0.80419 }
zoneData["Orange"]["SetAside4"] = { -2.7803627, 0, -0.520315 }
zoneData["Orange"]["SetAside5"] = { -2.7803627, 0, 0.042552 }
zoneData["Orange"]["SetAside6"] = { -2.7803627, 0, 0.605419 }
zoneData["Orange"]["UnderSetAside6"] = { -2.9303627, 0, 0.80419 }
-- Gets the global position for the given zone on the specified player mat.
---@param playerColor: Color name of the player mat to get the zone position for (e.g. "Red")
---@param zoneName: Name of the zone to get the position for. See Zones object documentation for a list of valid zones.
---@return: Global position table, or nil if an invalid player color or zone is specified
function Zones.getZonePosition(playerColor, zoneName)
-- Green positions are the same as White and Red the same as Orange
zoneData["Red"] = zoneData["Orange"]
zoneData["Green"] = zoneData["White"]
-- Gets the global position for the given zone on the specified player mat.
---@param playerColor: Color name of the player mat to get the zone position for (e.g. "Red")
---@param zoneName: Name of the zone to get the position for. See Zones object documentation for a list of valid zones.
---@return: Global position table, or nil if an invalid player color or zone is specified
Zones.getZonePosition = function(playerColor, zoneName)
if (playerColor ~= "Red"
and playerColor ~= "Orange"
and playerColor ~= "White"
and playerColor ~= "Green") then
return nil
end
return getObjectFromGUID(playerMatGuids[playerColor]).positionToWorld(Zones[playerColor][zoneName])
end
return getObjectFromGUID(playerMatGuids[playerColor]).positionToWorld(zoneData[playerColor][zoneName])
end
-- Return the global rotation for a card on the given player mat, based on its metadata.
---@param playerColor: Color name of the player mat to get the rotation for (e.g. "Red")
---@param cardMetadata: Table of card metadata. Metadata fields type and permanent are required; all others are optional.
---@return: Global rotation vector for the given card. This will include the
-- Y rotation to orient the card on the given player mat as well as a
-- Z rotation to place the card face up or face down.
function Zones.getDefaultCardRotation(playerColor, zone)
-- Return the global rotation for a card on the given player mat, based on its metadata.
---@param playerColor: Color name of the player mat to get the rotation for (e.g. "Red")
---@param cardMetadata: Table of card metadata. Metadata fields type and permanent are required; all others are optional.
---@return: Global rotation vector for the given card. This will include the
-- Y rotation to orient the card on the given player mat as well as a
-- Z rotation to place the card face up or face down.
Zones.getDefaultCardRotation = function(playerColor, zone)
local deckRotation = getObjectFromGUID(playerMatGuids[playerColor]).getRotation()
if zone == "Deck" then
@ -134,4 +137,7 @@ function Zones.getDefaultCardRotation(playerColor, zone)
end
return deckRotation
end
return Zones
end