因此,我正在嘗試為文本 RPG 制作一個功能,該功能允許人們單擊“漫游”按鈕并最終到達當前位置附近的隨機位置。我創建了幾個陣列,其中包含哪些位置靠近其他位置的資訊,我希望能夠按下按鈕,生成一個新位置,將該位置保存為當前位置,并生成一個新的漫游結果下一次按下按鈕時的新位置。
//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];
我目前的問題是,當我生成并加載一個新位置時,我不知道如何告訴系統它應該為下一個位置參考哪個陣列。我嘗試命名變數,以便可以將位置名稱變數添加到字串并得到陣列名稱,但即使我將它放在 aJSON.parse()中,它也會讀取字串,而不是陣列的內容。
function wander(location) {
wanderLocation = location[Math.floor(Math.random()*location.length)];
console.log(wanderLocation);
locationsNearCurrentArea = "locationsNear" wanderLocation.name;
console.log(locationsNearCurrentArea);
callPannel(wanderLocation.string);
}
我怎樣才能更好地制作一個能讓我在這樣的位置之間反彈的功能?
uj5u.com熱心網友回復:
我會為此使用顯式資料結構而不是一堆變數。以當前位置為鍵,以陣列中的附近位置為值的Map就足夠了
const nearbyLocations = new Map([
//[ key, [ values, ... ] ],
[ Start, [ BigForrest, Cliffs, Cave ] ],
[ BigForrest, [ Start, Clearing, River ] ],
[ Cliffs, [ Start, Cave, River ] ],
// etc
])
然后你可以使用這樣的東西來獲得一個隨機的附近位置
const wander = (currentLocation) => {
const nearby = nearbyLocations.get(currentLocation)
return nearby?.[Math.floor(Math.random() * nearby?.length)]
}
uj5u.com熱心網友回復:
const objectOfArrays = {
"startingAreaLocations":['Clearing', 'BigForrest', 'SmallForrest', 'Cliffs', 'Cave', 'Pond', 'Start', 'River', 'TownEnterance'],
"locationsNearStart":['BigForrest', 'Cliffs', 'Cave'],
"locationsNearBigForrest":['Start', 'Clearing', 'River'],
"locationsNearCliffs":['Start', 'Cave', 'River'],
"locationsNearCave":['Cliffs', 'Pond', 'River', 'Start'],
"locationsNearClearing":['BigForrest', 'River', 'Start'],
"locationsNearSmallForrest":['Pond', 'River', 'TownEnterance'],
"locationsNearPond":['SmallForrest', 'Cave', 'River'],
"locationsNearRiver":['BigForrest', 'Cliffs', 'Cave', 'Clearing', 'SmallForrest', 'Pond', 'TownEnterance'],
"locationsNearTowerEnterance":['SmallForrest', 'River']
}
console.log(objectOfArrays['startingAreaLocations']);
console.log(objectOfArrays['startingAreaLocations'][0]);
uj5u.com熱心網友回復:
您應該至少將資料放在 JSON、物件或串列中。下面是一個使用物件的例子:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435721.html
標籤:javascript 数组 随机的
