diff --git a/src/Game/Utils/PathFinding.utils.js b/src/Game/Utils/PathFinding.utils.js index 44219e4..c0bebe5 100644 --- a/src/Game/Utils/PathFinding.utils.js +++ b/src/Game/Utils/PathFinding.utils.js @@ -110,7 +110,7 @@ export class PathFinder { /** * @type Map */ - let cameFrom = new Map(); + let cameFrom = new Array(); let minFScoreNodeIndex = 0; //START LOOP WOOOOAAAW @@ -143,27 +143,22 @@ export class PathFinder { * @type Array */ let currentNeighbors = this._getNeighbors(currentNode.position); - // if(!currentNeighbors) return undefined; - // console.log(currentNeighbors); for (const neighbor of currentNeighbors) { - this._closedSet.push(neighbor); let tentativeGScore = currentNode.gScore + this._manhattanDistance(currentNode.position, neighbor.position); + if (this._existsInClosedSet(neighbor)) continue; + this._closedSet.push(neighbor); if (tentativeGScore < neighbor.gScore) { - // console.log(neighbor.id, currentNode.id); - cameFrom.set(neighbor.id, currentNode); + cameFrom.push({ id: neighbor.id, node: currentNode }); neighbor.gScore = tentativeGScore; - neighbor.fScore = tentativeGScore + this._heuristic(neighbor.position, neighbor.dScore); + neighbor.fScore = tentativeGScore + this._heuristic(neighbor.position, neighbor.dScore)*0; - let neighborFoundInOpenSet = false; - for (const node of this._openSet) { - if (node.id === neighbor.id) { - neighborFoundInOpenSet = true; - break; - } + if(!this._existsInOpenSet(neighbor)) + { + this._openSet.push(neighbor); } - if (!neighborFoundInOpenSet) this._openSet.push(neighbor); } } + // console.log([...this._openSet]); } return new NavigationResult(false, "", new NavigationPath()); @@ -176,13 +171,13 @@ export class PathFinder { */ _reconstructPath(cameFrom, current) { let totalPath = [current.position]; - // console.log(cameFrom); - let keys = [...cameFrom.keys()]; - while (keys.includes(current.id)) { - if (current.id === cameFrom.get(current.id).id) break; - current = cameFrom.get(current.id); - totalPath.push(current.position); - } + console.log(cameFrom); + // let keys = [...cameFrom.keys()]; + // while (keys.includes(current.id)) { + // if (current.id === cameFrom.get(current.id).id) break; + // current = cameFrom.get(current.id); + // totalPath.push(current.position); + // } return totalPath.reverse(); } @@ -201,7 +196,7 @@ export class PathFinder { if (currentTile && !currentTile.isObstacle) { currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); - if (!this._existsInClosedSet(node)) { + if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) { neighbors.push(node); } } @@ -210,7 +205,7 @@ export class PathFinder { if (currentTile && !currentTile.isObstacle) { currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); - if (!this._existsInClosedSet(node)) { + if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) { neighbors.push(node); } } @@ -219,7 +214,7 @@ export class PathFinder { if (currentTile && !currentTile.isObstacle) { currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); - if (!this._existsInClosedSet(node)) { + if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) { neighbors.push(node); } } @@ -228,7 +223,7 @@ export class PathFinder { if (currentTile && !currentTile.isObstacle) { currentTile.optionalTerrainTileRef.drawObject.tint = 0xff0000; node = new PathFinderNode(currentTile.position, 1e16, 1e16, currentTile.movementCost); - if (!this._existsInClosedSet(node)) { + if (/*!this._existsInClosedSet(node) &&*/ !currentTile.isObstacle) { neighbors.push(node); } } @@ -256,7 +251,7 @@ export class PathFinder { let x2 = this._start.getX() - this._goal.getX(); let y2 = this._start.getY() - this._goal.getY(); let cross = Math.abs(x1 * y2 - x2 * y1); - return this._manhattanDistance(current, this._goal) * d + cross * 0.001; + return this._manhattanDistance(current, this._goal) * d; } /**