update pixi to 8.1.0; fixed navigation issue when path was unreachable
This commit is contained in:
parent
2a69d4ad25
commit
6df6956133
@ -35,20 +35,22 @@ export class NPCController extends GameObject
|
||||
{
|
||||
let pf = new PathFinder();
|
||||
let nPath = pf.search(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
|
||||
if(!nPath)
|
||||
if(nPath.error)
|
||||
{
|
||||
console.log("failed");
|
||||
callback("failed");
|
||||
console.log(nPath);
|
||||
return;
|
||||
}
|
||||
else if (nPath.path.length < 2)
|
||||
else if (nPath.result.path.length < 2)
|
||||
{
|
||||
console.log("success");
|
||||
callback("success");
|
||||
console.log(nPath);
|
||||
return;
|
||||
}
|
||||
for (let i = nPath.path.length-1; i > 0; i--) {
|
||||
this.navigationPathQueue.push(nPath.path[i]);
|
||||
for (let i = nPath.result.path.length-1; i > 0; i--) {
|
||||
this.navigationPathQueue.push(nPath.result.path[i]);
|
||||
}
|
||||
this.navigationCallback = callback;
|
||||
this.navigationInProgress = true;
|
||||
|
2
src/Game/NPC/NPCObserver/NPCObserver.js
Normal file
2
src/Game/NPC/NPCObserver/NPCObserver.js
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
// export class NPCObserver
|
@ -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 {
|
||||
/**
|
||||
* @type Array<PathFinderNode>
|
||||
@ -90,30 +113,31 @@ export class PathFinder {
|
||||
//find node with min f score
|
||||
//better to rewrite it to priority queue
|
||||
for (let i = 0; i < this._openSet.length; i++) {
|
||||
if(!this._openSet[i]){
|
||||
return undefined;
|
||||
}
|
||||
// if(!this._openSet[i] || !this._openSet[minFScoreNodeIndex]){
|
||||
// return new NavigationResult(true, "Failed to access element in openSet", undefined);
|
||||
// }
|
||||
if (this._openSet[i].fScore < this._openSet[minFScoreNodeIndex].fScore) minFScoreNodeIndex = i;
|
||||
}
|
||||
//wow! node is found and set!!
|
||||
currentNode = this._openSet[minFScoreNodeIndex];
|
||||
minFScoreNodeIndex = 0;
|
||||
|
||||
if (PointInt2D.isEqual(currentNode.position, this._goal)) {
|
||||
//wow!!! we have found an end of the path!!! this is so cool!!!
|
||||
// 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...
|
||||
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);
|
||||
|
||||
/**
|
||||
* @type Array<PathFinderNode>
|
||||
*/
|
||||
let currentNeighbors = this._getNeighbors(currentNode.position);
|
||||
if(!currentNeighbors) return undefined;
|
||||
// if(!currentNeighbors) return undefined;
|
||||
// console.log(currentNeighbors);
|
||||
for (const neighbor of currentNeighbors) {
|
||||
this._closedSet.push(neighbor);
|
||||
@ -136,7 +160,7 @@ export class PathFinder {
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
return new NavigationResult(false, "", new NavigationPath());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,38 +191,47 @@ export class PathFinder {
|
||||
/**
|
||||
* @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
|
||||
let currentTile = getTileAt(root.getX(), root.getY() + BC_TERRAIN_SETTINGS.totalSize, ChunkStorageTypes.TYPE_TERRAIN);
|
||||
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
|
||||
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);
|
||||
if (!this._existsInClosedSet(node)) {
|
||||
neighbors.push(node);
|
||||
}
|
||||
}
|
||||
//east
|
||||
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);
|
||||
if (!this._existsInClosedSet(node)) {
|
||||
neighbors.push(node);
|
||||
}
|
||||
}
|
||||
//west
|
||||
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);
|
||||
if (!this._existsInClosedSet(node)) {
|
||||
neighbors.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
return neighbors;
|
||||
}
|
||||
|
42122
src/pixi/pixi.mjs
42122
src/pixi/pixi.mjs
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user