update pixi to 8.1.0; fixed navigation issue when path was unreachable

This commit is contained in:
TorgaW 2024-04-19 12:16:59 +03:00
parent 2a69d4ad25
commit 6df6956133
4 changed files with 41093 additions and 1202 deletions

View File

@ -35,20 +35,22 @@ export class NPCController extends GameObject
{ {
let pf = new PathFinder(); let pf = new PathFinder();
let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position); let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
if(!nPath) if(nPath.error)
{ {
console.log("failed"); console.log("failed");
callback("failed"); callback("failed");
console.log(nPath);
return; return;
} }
else if (nPath.path.length < 2) else if (nPath.result.path.length < 2)
{ {
console.log("success"); console.log("success");
callback("success"); callback("success");
console.log(nPath);
return; return;
} }
for (let i = nPath.path.length-1; i > 0; i--) { for (let i = nPath.result.path.length-1; i > 0; i--) {
this.navigationPathQueue.push(nPath.path[i]); this.navigationPathQueue.push(nPath.result.path[i]);
} }
this.navigationCallback = callback; this.navigationCallback = callback;
this.navigationInProgress = true; this.navigationInProgress = true;

View File

@ -0,0 +1,2 @@
// export class NPCObserver

View File

@ -38,6 +38,29 @@ export class NavigationPath {
} }
}; };
export class NavigationResult
{
/**
* @type Boolean
*/
error;
/**
* @type String
*/
errorText;
/**
* @type NavigationPath | undefined
*/
result;
constructor(error, errorText, result)
{
this.error = error;
this.errorText = errorText;
this.result = result;
}
};
export class PathFinder { export class PathFinder {
/** /**
* @type Array<PathFinderNode> * @type Array<PathFinderNode>
@ -90,30 +113,31 @@ export class PathFinder {
//find node with min f score //find node with min f score
//better to rewrite it to priority queue //better to rewrite it to priority queue
for (let i = 0; i < this._openSet.length; i++) { for (let i = 0; i < this._openSet.length; i++) {
if(!this._openSet[i]){ // if(!this._openSet[i] || !this._openSet[minFScoreNodeIndex]){
return undefined; // return new NavigationResult(true, "Failed to access element in openSet", undefined);
} // }
if (this._openSet[i].fScore < this._openSet[minFScoreNodeIndex].fScore) minFScoreNodeIndex = i; if (this._openSet[i].fScore < this._openSet[minFScoreNodeIndex].fScore) minFScoreNodeIndex = i;
} }
//wow! node is found and set!! //wow! node is found and set!!
currentNode = this._openSet[minFScoreNodeIndex]; currentNode = this._openSet[minFScoreNodeIndex];
minFScoreNodeIndex = 0;
if (PointInt2D.isEqual(currentNode.position, this._goal)) { if (PointInt2D.isEqual(currentNode.position, this._goal)) {
//wow!!! we have found an end of the path!!! this is so cool!!! //wow!!! we have found an end of the path!!! this is so cool!!!
// console.log(cameFrom); // console.log(cameFrom);
return new NavigationPath(this._reconstructPath(cameFrom, currentNode)); //return something weird stuff return new NavigationResult(false, "", new NavigationPath(this._reconstructPath(cameFrom, currentNode))); //return something weird stuff
} }
//and now we must delete this node... what a sad situation... //and now we must delete this node... what a sad situation...
this._openSet.splice(minFScoreNodeIndex, 1); this._openSet.splice(minFScoreNodeIndex, 1);
//but wait! we add it to closed set! nice!!! (why we are doing this??? idk... okay) //but wait! we add it to closed set! nice!!!
this._closedSet.push(currentNode); this._closedSet.push(currentNode);
/** /**
* @type Array<PathFinderNode> * @type Array<PathFinderNode>
*/ */
let currentNeighbors = this._getNeighbors(currentNode.position); let currentNeighbors = this._getNeighbors(currentNode.position);
if(!currentNeighbors) return undefined; // if(!currentNeighbors) return undefined;
// console.log(currentNeighbors); // console.log(currentNeighbors);
for (const neighbor of currentNeighbors) { for (const neighbor of currentNeighbors) {
this._closedSet.push(neighbor); this._closedSet.push(neighbor);
@ -136,7 +160,7 @@ export class PathFinder {
} }
} }
return undefined; return new NavigationResult(false, "", new NavigationPath());
} }
/** /**
@ -167,37 +191,46 @@ export class PathFinder {
/** /**
* @type SceneObject * @type SceneObject
*/ */
let currentTile = getTileAt(root.getX(), root.getY() + BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
if(!currentTile) return undefined;
// currentTile.drawObject.tint = 0xff0000;
let node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
//north //north
if (!this._existsInClosedSet(node)) { let currentTile = getTileAt(root.getX(), root.getY() + BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
neighbors.push(node); let node;
if(currentTile)
{
// currentTile.drawObject.tint = 0xff0000;
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
} }
//south //south
currentTile = getTileAt(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN); currentTile = getTileAt(root.getX(), root.getY() - BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
if(!currentTile) return undefined; if(currentTile)
// currentTile.drawObject.tint = 0xff0000; {
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost); // currentTile.drawObject.tint = 0xff0000;
if (!this._existsInClosedSet(node)) { node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
neighbors.push(node); if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
} }
//east //east
currentTile = getTileAt(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN); currentTile = getTileAt(root.getX() + BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
if(!currentTile) return undefined; if(currentTile)
// currentTile.drawObject.tint = 0xff0000; {
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost); // currentTile.drawObject.tint = 0xff0000;
if (!this._existsInClosedSet(node)) { node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
neighbors.push(node); if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
} }
//west //west
currentTile = getTileAt(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN); currentTile = getTileAt(root.getX() - BC_TERRAIN_SETTINGS.totalSize, root.getY(), ChunkStorageTypes.TYPE_TERRAIN);
if(!currentTile) return undefined; if(currentTile)
// currentTile.drawObject.tint = 0xff0000; {
node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost); // currentTile.drawObject.tint = 0xff0000;
if (!this._existsInClosedSet(node)) { node = new PathFinderNode(new PointInt2D(currentTile.worldPosition.getX(), currentTile.worldPosition.getY()), 1e16, 1e16, currentTile.props.navigationCost);
neighbors.push(node); if (!this._existsInClosedSet(node)) {
neighbors.push(node);
}
} }
return neighbors; return neighbors;

File diff suppressed because one or more lines are too long