Small refactor
This commit is contained in:
+10
-10
@@ -41,23 +41,23 @@ export function wipePathfindingCache() {
|
|||||||
function getNode(pos, initialize=true) {
|
function getNode(pos, initialize=true) {
|
||||||
pos = {layer: 0, ...pos}; // Copy pos and set pos.layer to the default value if it's unset
|
pos = {layer: 0, ...pos}; // Copy pos and set pos.layer to the default value if it's unset
|
||||||
if (!cachedNodes)
|
if (!cachedNodes)
|
||||||
cachedNodes = new Map();
|
cachedNodes = new Array(2);
|
||||||
let cachedLayer = cachedNodes.get(pos.layer);
|
if (!cachedNodes[pos.layer]) {
|
||||||
if (!cachedLayer) {
|
|
||||||
// TODO Check if ceil is the right thing to do here
|
// TODO Check if ceil is the right thing to do here
|
||||||
cachedLayer = new Array(Math.ceil(canvas.dimensions.sceneHeight / canvas.dimensions.size));
|
cachedNodes[pos.layer] = new Array(Math.ceil(canvas.dimensions.sceneHeight / canvas.dimensions.size));
|
||||||
cachedNodes.set(pos.layer, cachedLayer);
|
|
||||||
}
|
}
|
||||||
if (!cachedLayer[pos.y])
|
if (!cachedLayer[pos.layer][pos.y])
|
||||||
cachedLayer[pos.y] = new Array(Math.ceil(canvas.dimensions.sceneWidth / canvas.dimensions.size));
|
cachedLayer[pos.layer][pos.y] = new Array(Math.ceil(canvas.dimensions.sceneWidth / canvas.dimensions.size));
|
||||||
if (!cachedLayer[pos.y][pos.x]) {
|
if (!cachedLayer[pos.layer][pos.y][pos.x]) {
|
||||||
cachedLayer[pos.y][pos.x] = pos;
|
cachedLayer[pos.layer][pos.y][pos.x] = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = cachedLayer[pos.y][pos.x];
|
const node = cachedLayer[pos.layer][pos.y][pos.x];
|
||||||
if (initialize && !node.edges) {
|
if (initialize && !node.edges) {
|
||||||
node.edges = [];
|
node.edges = [];
|
||||||
for (const neighborPos of neighbors(pos)) {
|
for (const neighborPos of neighbors(pos)) {
|
||||||
|
if (neighborPos.x < 0 || neighborPos.y < 0)
|
||||||
|
continue;
|
||||||
// TODO Work with pixels instead of grid locations
|
// TODO Work with pixels instead of grid locations
|
||||||
if (!canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(pos), getCenterFromGridPositionObj(neighborPos)))) {
|
if (!canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(pos), getCenterFromGridPositionObj(neighborPos)))) {
|
||||||
const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y;
|
const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y;
|
||||||
|
|||||||
Reference in New Issue
Block a user