updated playarea

This commit is contained in:
Chr1Z93 2022-11-12 11:46:05 +01:00
parent bc01f28c29
commit d0fdaefe42

View File

@ -1,80 +1,62 @@
---------------------------------------------------------
-- general setup
---------------------------------------------------------
-- set true to enable debug logging -- set true to enable debug logging
DEBUG = false DEBUG = false
-- we use this to turn off collision handling (for clue spawning) -- we use this to turn off collision handling until onLoad() is complete
-- until after load is complete (probably a better way to do this)
COLLISION_ENABLED = false COLLISION_ENABLED = false
-- TODO get the log function from global instead local COUNTER = getObjectFromGUID('f182ee')
-- log = Global.call('getLogFunction', this) local TOKEN_DATA = Global.getTable('TOKEN_DATA')
function getLogFunction(object) local clueData = {
return function (message) thickness = 0.1,
if DEBUG then stackable = true,
print(message) type = 2,
end image = TOKEN_DATA.clue.image,
end image_bottom = TOKEN_DATA.doom.image
end }
log = getLogFunction(self) ---------------------------------------------------------
-- general code
---------------------------------------------------------
function onSave() return JSON.encode(SPAWNED_LOCATION_GUIDS) end
function onLoad(save_state)
-- records locations we have spawned clues for
SPAWNED_LOCATION_GUIDS = JSON.decode(save_state) or {}
function onload(save_state)
self.interactable = DEBUG
local dataHelper = getObjectFromGUID('708279') local dataHelper = getObjectFromGUID('708279')
LOCATIONS = dataHelper.getTable('LOCATIONS_DATA') LOCATIONS = dataHelper.getTable('LOCATIONS_DATA')
TOKEN_PLAYER_ONE = Global.getTable('tokenplayerone') self.interactable = DEBUG
COUNTER = getObjectFromGUID('f182ee') Wait.time(function() COLLISION_ENABLED = true end, 1)
log('attempting to load state: ' .. save_state)
if save_state ~= '' then
SPAWNED_LOCATION_GUIDS = JSON.decode(save_state)
end
COLLISION_ENABLED = true
end end
function onSave() function log(message)
local spawned_locations = JSON.encode(SPAWNED_LOCATION_GUIDS) if DEBUG then print(message) end
self.script_state = spawned_locations
end end
--[[ ---------------------------------------------------------
records locations we have spawned clues for, we write this to the save -- clue spawning
file onsave() so we don't spawn clues again after a load ---------------------------------------------------------
]]
SPAWNED_LOCATION_GUIDS = {}
function isAlreadySpawned(object)
return SPAWNED_LOCATION_GUIDS[object.getGUID()] ~= nil
end
function markSpawned(object)
SPAWNED_LOCATION_GUIDS[object.getGUID()] = 1
end
function buildKey(object)
return object.getName() .. '_' .. object.getGUID()
end
-- try the compound key then the name alone as default -- try the compound key then the name alone as default
function getLocation(object) function getLocation(object)
return LOCATIONS[buildKey(object)] or LOCATIONS[object.getName()] return LOCATIONS[object.getName() .. '_' .. object.getGUID()] or LOCATIONS[object.getName()]
end end
function isLocationWithClues(object) -- Return the number of clues to spawn on this location
return getLocation(object) ~= nil
end
--[[
Return the number of clues to spawn on this location
]]
function getClueCount(object, isFaceDown, playerCount) function getClueCount(object, isFaceDown, playerCount)
if not isLocationWithClues(object) then local details = getLocation(object)
if details == nil then
error('attempted to get clue for unexpected object: ' .. object.getName()) error('attempted to get clue for unexpected object: ' .. object.getName())
end end
local details = getLocation(object)
log(object.getName() .. ' : ' .. details['type'] .. ' : ' .. details['value'] .. ' : ' .. details['clueSide']) log(object.getName() .. ' : ' .. details['type'] .. ' : ' .. details['value'] .. ' : ' .. details['clueSide'])
if ((isFaceDown and details['clueSide'] == 'back') if ((isFaceDown and details['clueSide'] == 'back') or (not isFaceDown and details['clueSide'] == 'front')) then
or (not isFaceDown and details['clueSide'] == 'front')) then
if details['type'] == 'fixed' then if details['type'] == 'fixed' then
return details['value'] return details['value']
elseif details['type'] == 'perPlayer' then elseif details['type'] == 'perPlayer' then
@ -85,130 +67,54 @@ function getClueCount(object, isFaceDown, playerCount)
return 0 return 0
end end
function spawnToken(position, number) function spawnCluesAtLocation(clueCount, object)
local obj_parameters = { if SPAWNED_LOCATION_GUIDS[object.getGUID()] ~= nil then
position = position,
rotation = {3.87674022, -90, 0.239081308}
}
local custom = {
thickness = 0.1,
stackable = true
}
if number == '1' or number == '2' then
obj_parameters.type = 'Custom_Token'
custom.merge_distance = 5.0
local token = spawnObject(obj_parameters)
if number == '1' then
custom.image = TOKEN_PLAYER_ONE.damageone
token.setCustomObject(custom)
token.scale {0.17, 1, 0.17}
return token
end
if number == '2' then
custom.image = TOKEN_PLAYER_ONE.damagethree
token.setCustomObject(custom)
token.scale {0.18, 1, 0.18}
return token
end
end
if number == '3' or number == '4' then
obj_parameters.type = 'Custom_Tile'
custom.type = 2
local token = spawnObject(obj_parameters)
if number == '3' then
custom.image = TOKEN_PLAYER_ONE.clue
custom.image_bottom = TOKEN_PLAYER_ONE.doom
token.setCustomObject(custom)
token.scale {0.25, 1, 0.25}
token.use_snap_points=false
return token
end
if number == '4' then
custom.image = TOKEN_PLAYER_ONE.doom
custom.image_bottom = TOKEN_PLAYER_ONE.clue
token.setCustomObject(custom)
token.scale {0.25, 1, 0.25}
token.use_snap_points=false
return token
end
end
end
function spawnCluesAtLocation(clueCount, collision_info)
local object = collision_info.collision_object
if isAlreadySpawned(object) then
error('tried to spawn clue for already spawned location:' .. object.getName()) error('tried to spawn clue for already spawned location:' .. object.getName())
end end
local obj_parameters = {}
obj_parameters.type = 'Custom_Token'
obj_parameters.position = {
object.getPosition()[1],
object.getPosition()[2] + 1,
object.getPosition()[3]
}
log('spawning clues for ' .. object.getName() .. '_' .. object.getGUID()) log('spawning clues for ' .. object.getName() .. '_' .. object.getGUID())
local playerCount = COUNTER.getVar('val') log('player count is ' .. COUNTER.getVar('val') .. ', clue count is ' .. clueCount)
log('player count is ' .. playerCount .. ', clue count is ' .. clueCount)
-- mark this location as spawned, can't happen again -- mark this location as spawned, can't happen again
markSpawned(object) SPAWNED_LOCATION_GUIDS[object.getGUID()] = 1
i = 0
while i < clueCount do -- spawn clues (starting top right, moving to the next row after 4 clues)
if i < 4 then local pos = object.getPosition()
obj_parameters.position = { for i = 1, clueCount do
collision_info.collision_object.getPosition()[1] + 0.3, local row = math.floor(1 + (i - 1) / 4)
collision_info.collision_object.getPosition()[2] + 0.2, local column = (i - 1) % 4
collision_info.collision_object.getPosition()[3] - 0.8 + (0.55 * i) spawnClues({ pos[1] + 1.5 - 0.55 * row, pos[2], pos[3] - 0.825 + 0.55 * column })
}
elseif i < 8 then
obj_parameters.position = {
collision_info.collision_object.getPosition()[1] + 0.85,
collision_info.collision_object.getPosition()[2] + 0.2,
collision_info.collision_object.getPosition()[3] - 3 + (0.55 * i)
}
else
obj_parameters.position = {
collision_info.collision_object.getPosition()[1] + 0.575,
collision_info.collision_object.getPosition()[2] + 0.4,
collision_info.collision_object.getPosition()[3] - 5.2 + (0.55 * i)}
end
spawnToken(obj_parameters.position, '3')
i = i + 1
end end
end end
function spawnClues(position)
local token = spawnObject({
position = position,
rotation = { 3.88, -90, 0.24 },
type = 'Custom_Tile'
})
token.setCustomObject(clueData)
token.scale { 0.25, 1, 0.25 }
token.use_snap_points = false
end
function updateLocations(args) function updateLocations(args)
local custom_data_helper = getObjectFromGUID(args[1]) custom_data_helper_guid = args[1]
data_locations = custom_data_helper.getTable("LOCATIONS_DATA") local custom_data_helper = getObjectFromGUID(args[1])
for k, v in pairs(data_locations) do
LOCATIONS[k] = v for k, v in pairs(custom_data_helper.getTable("LOCATIONS_DATA")) do
end LOCATIONS[k] = v
end
end end
function onCollisionEnter(collision_info) function onCollisionEnter(collision_info)
-- short circuit all collision stuff until we've loaded state if not COLLISION_ENABLED then return end
if not COLLISION_ENABLED then
return
end
-- check if we should spawn clues here -- check if we should spawn clues here and do so according to playercount
local object = collision_info.collision_object local object = collision_info.collision_object
if isLocationWithClues(object) and not isAlreadySpawned(object) then if getLocation(object) ~= nil and SPAWNED_LOCATION_GUIDS[object.getGUID()] == nil then
-- this isn't an either/or as down/up here means at a relatively low angle local clueCount = getClueCount(object, object.is_face_down, COUNTER.getVar('val'))
-- local isFaceUp = not object.is_face_down if clueCount > 0 then spawnCluesAtLocation(clueCount, object) end
-- local isFaceDown = (object.getRotation()[3] > 160 and object.getRotation()[3] < 200)
local playerCount = COUNTER.getVar('val')
local clueCount = getClueCount(object, object.is_face_down, playerCount)
if clueCount > 0 then
spawnCluesAtLocation(clueCount, collision_info)
end
end end
end end