Pathfinding manager will now return path steps in pixel units by default
This commit is contained in:
parent
f96eac4737
commit
f78392ceab
7 changed files with 50 additions and 45 deletions
|
@ -6,20 +6,23 @@ export class PathfindingManager {
|
|||
|
||||
private easyStar;
|
||||
private grid: number[][];
|
||||
private tileDimensions: { width: number; height: number };
|
||||
|
||||
constructor(scene: Phaser.Scene, collisionsGrid: number[][]) {
|
||||
constructor(scene: Phaser.Scene, collisionsGrid: number[][], tileDimensions: { width: number; height: number }) {
|
||||
this.scene = scene;
|
||||
|
||||
this.easyStar = new EasyStar.js();
|
||||
this.easyStar.enableDiagonals();
|
||||
|
||||
this.grid = collisionsGrid;
|
||||
this.tileDimensions = tileDimensions;
|
||||
this.setEasyStarGrid(collisionsGrid);
|
||||
}
|
||||
|
||||
public async findPath(
|
||||
start: { x: number; y: number },
|
||||
end: { x: number; y: number },
|
||||
measuredInPixels: boolean = true,
|
||||
tryFindingNearestAvailable: boolean = false
|
||||
): Promise<{ x: number; y: number }[]> {
|
||||
let endPoints: { x: number; y: number }[] = [end];
|
||||
|
@ -48,12 +51,21 @@ export class PathfindingManager {
|
|||
// rejected Promise will return undefined for path
|
||||
path = await this.getPath(start, endPoint).catch();
|
||||
if (path && path.length > 0) {
|
||||
return path;
|
||||
return measuredInPixels ? this.mapTileUnitsToPixels(path) : path;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
private mapTileUnitsToPixels(path: { x: number; y: number }[]): { x: number; y: number }[] {
|
||||
return path.map((step) => {
|
||||
return {
|
||||
x: step.x * this.tileDimensions.width + this.tileDimensions.width * 0.5,
|
||||
y: step.y * this.tileDimensions.height + this.tileDimensions.height * 0.5,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private getNeighbouringTiles(tile: { x: number; y: number }): { x: number; y: number }[] {
|
||||
const xOffsets = [-1, 0, 1, 1, 1, 0, -1, -1];
|
||||
const yOffsets = [-1, -1, -1, 0, 1, 1, 1, 0];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue