So I'm trying to make a function for a text RPG that allows people to click a "wander" button and end up in a random location near their current one. I've created several arrays that contain the information for which locations are near others, and I'd like to be able to press a button, generate a new location, save that location as the current location, and generate a new wander result from the new location the next time the button is pressed.
//Starting area locations
const startingAreaLocations = [Clearing, BigForrest, SmallForrest, Cliffs, Cave, Pond, Start, River, TownEnterance];
const locationsNearStart = [BigForrest, Cliffs, Cave];
const locationsNearBigForrest = [Start, Clearing, River];
const locationsNearCliffs = [Start, Cave, River];
const locationsNearCave = [Cliffs, Pond, River, Start];
const locationsNearClearing = [BigForrest, River, Start];
const locationsNearSmallForrest = [Pond, River, TownEnterance];
const locationsNearPond = [SmallForrest, Cave, River];
const locationsNearRiver = [BigForrest, Cliffs, Cave, Clearing, SmallForrest, Pond, TownEnterance];
const locationsNearTowerEnterance = [SmallForrest, River];
My issue at the moment is that when I generate and load a new location, I don't know how to tell the system which array it should be referencing for the next locations. I tried to name the variables so that I could add the location name variable to a string and come out with the array name, but even when I put it through a JSON.parse(), it reads the string, not the contents of my array.
function wander(location) {
wanderLocation = location[Math.floor(Math.random()*location.length)];
console.log(wanderLocation);
locationsNearCurrentArea = "locationsNear" + wanderLocation.name;
console.log(locationsNearCurrentArea);
callPannel(wanderLocation.string);
}
How can I better make a feature that will let me bounce between locations like this?
I would use an explicit data structure for this instead of a bunch of variables. A Map with the current location as key and nearby locations in an array as the value should suffice
const nearbyLocations = new Map([
//[ key, [ values, ... ] ],
[ Start, [ BigForrest, Cliffs, Cave ] ],
[ BigForrest, [ Start, Clearing, River ] ],
[ Cliffs, [ Start, Cave, River ] ],
// etc
])
Then you can use something like this to get a random, nearby location
const wander = (currentLocation) => {
const nearby = nearbyLocations.get(currentLocation)
return nearby?.[Math.floor(Math.random() * nearby?.length)]
}
You should put the data at least in JSON, object, or list. Here is an example using object:
let data = {
startingAreaLocations: ["Clearing", "BigForrest", "SmallForrest"],
locationsNearStart: ["BigForrest", "Cliffs", "Cave"],
locationsNearBigForrest: ["Start", "Clearing", "River"],
};
let newLocation = "River";
let newWander = ["SmallForrest", "BigForrest"];
let dataKey = Object.values(data);
data[Object.keys(data)[Object.keys(data).length - 1]].forEach((e) => {
if (e === newLocation) {
data[`locationsNear${e}`] = newWander;
}
});
console.log(data);