mardi 1 mars 2022

How can I find a specific array assigned to a randomly selected variable?

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?




Aucun commentaire:

Enregistrer un commentaire