42 lines
1.7 KiB
JavaScript
42 lines
1.7 KiB
JavaScript
import { ECellType } from "../Simulation/index.js";
|
|
import { Hex } from "./index.js";
|
|
export class PathFinding {
|
|
static bfs(level, startIndex, endIndex) {
|
|
const listToExplore = [startIndex];
|
|
let cameFrom = new Array(level.simCells.length);
|
|
cameFrom.fill(-1);
|
|
while (listToExplore.length > 0) {
|
|
const nodeIndex = listToExplore.shift();
|
|
const cell = level.simCells[nodeIndex];
|
|
for (let i = 0; i < 6; ++i) {
|
|
const neighbourHex = Hex.neighbour(cell.hex, i);
|
|
const neighbourIndex = level.getCellIndex(neighbourHex);
|
|
if (neighbourIndex === -1) {
|
|
continue;
|
|
}
|
|
const neighbourCell = level.simCells[neighbourIndex];
|
|
if (neighbourCell.type === ECellType.Blocked || neighbourCell.type === ECellType.Reserved || neighbourCell.simTower !== null) {
|
|
continue;
|
|
}
|
|
if (cameFrom[neighbourIndex] === -1) {
|
|
cameFrom[neighbourIndex] = nodeIndex;
|
|
if (neighbourIndex !== endIndex) {
|
|
listToExplore.push(neighbourIndex);
|
|
}
|
|
else {
|
|
let idx = neighbourIndex;
|
|
const path = [idx];
|
|
while (idx !== startIndex) {
|
|
const prev = cameFrom[idx];
|
|
idx = prev;
|
|
path.unshift(idx);
|
|
}
|
|
return path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
//# sourceMappingURL=PathFinding.js.map
|