From 0196eae0552cdcc6e866f7a836368b80e8499716 Mon Sep 17 00:00:00 2001 From: Piotr Hanusiak Date: Tue, 8 Mar 2022 16:28:54 +0100 Subject: [PATCH] async call for recalculating tiles costs (#1890) * async call for recalculating tiles costs * fixed setWalkingCostGrid still being a blocking process * set exits tiles cost from the start * removed typing error from code Co-authored-by: Hanusiak Piotr --- front/src/Phaser/Game/GameMap.ts | 23 +++++------------------ front/src/Phaser/Game/GameScene.ts | 3 +-- front/src/Utils/PathfindingManager.ts | 22 ++++------------------ maps/tests/DoorTest/map.json | 2 +- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index 67d31776..d0ca4850 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -126,19 +126,7 @@ export class GameMap { for (let y = 0; y < this.map.height; y += 1) { const row: number[] = []; for (let x = 0; x < this.map.width; x += 1) { - row.push(this.isCollidingAt(x, y) ? 1 : 0); - } - grid.push(row); - } - return grid; - } - - public getWalkingCostGrid(): number[][] { - const grid: number[][] = []; - for (let y = 0; y < this.map.height; y += 1) { - const row: number[] = []; - for (let x = 0; x < this.map.width; x += 1) { - row.push(this.getWalkingCostAt(x, y)); + row.push(this.isCollidingAt(x, y) ? 1 : this.isExitTile(x, y) ? 2 : 0); } grid.push(row); } @@ -355,8 +343,7 @@ export class GameMap { return false; } - private getWalkingCostAt(x: number, y: number): number { - const bigCost = 100; + private isExitTile(x: number, y: number): boolean { for (const layer of this.phaserLayers) { if (!layer.visible) { continue; @@ -369,16 +356,16 @@ export class GameMap { tile && (tile.properties[GameMapProperties.EXIT_URL] || tile.properties[GameMapProperties.EXIT_SCENE_URL]) ) { - return bigCost; + return true; } for (const property of layer.layer.properties) { //@ts-ignore if (property.name && property.name === "exitUrl") { - return bigCost; + return true; } } } - return 0; + return false; } private triggerAllProperties(): void { diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index b356f5f0..67d9ebfe 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -571,7 +571,6 @@ export class GameScene extends DirtyScene { this.pathfindingManager = new PathfindingManager( this, this.gameMap.getCollisionGrid(), - this.gameMap.getWalkingCostGrid(), this.gameMap.getTileDimensions() ); @@ -1450,7 +1449,7 @@ ${escapedMessage} phaserLayers[i].setCollisionByProperty({ collides: true }, visible); } } - this.pathfindingManager.setCollisionGrid(this.gameMap.getCollisionGrid(), this.gameMap.getWalkingCostGrid()); + this.pathfindingManager.setCollisionGrid(this.gameMap.getCollisionGrid()); this.markDirty(); } diff --git a/front/src/Utils/PathfindingManager.ts b/front/src/Utils/PathfindingManager.ts index 4b8bd033..1f88342f 100644 --- a/front/src/Utils/PathfindingManager.ts +++ b/front/src/Utils/PathfindingManager.ts @@ -8,27 +8,21 @@ export class PathfindingManager { private grid: number[][]; private tileDimensions: { width: number; height: number }; - constructor( - scene: Phaser.Scene, - collisionsGrid: number[][], - walkingCostGrid: number[][], - tileDimensions: { width: number; height: number } - ) { + constructor(scene: Phaser.Scene, collisionsGrid: number[][], tileDimensions: { width: number; height: number }) { this.scene = scene; this.easyStar = new EasyStar.js(); this.easyStar.enableDiagonals(); this.easyStar.disableCornerCutting(); + this.easyStar.setTileCost(2, 100); this.grid = collisionsGrid; this.tileDimensions = tileDimensions; this.setEasyStarGrid(collisionsGrid); - this.setWalkingCostGrid(walkingCostGrid); } - public setCollisionGrid(collisionGrid: number[][], walkingCostGrid: number[][]): void { + public setCollisionGrid(collisionGrid: number[][]): void { this.setEasyStarGrid(collisionGrid); - this.setWalkingCostGrid(walkingCostGrid); } public async findPath( @@ -120,15 +114,7 @@ export class PathfindingManager { private setEasyStarGrid(grid: number[][]): void { this.easyStar.setGrid(grid); - this.easyStar.setAcceptableTiles([0]); // zeroes are walkable - } - - private setWalkingCostGrid(grid: number[][]): void { - for (let y = 0; y < grid.length; y += 1) { - for (let x = 0; x < grid[y].length; x += 1) { - this.easyStar.setAdditionalPointCost(x, y, grid[y][x]); - } - } + this.easyStar.setAcceptableTiles([0, 2]); // zeroes are walkable, 2 are exits, also walkable } private logGridToTheConsole(grid: number[][]): void { diff --git a/maps/tests/DoorTest/map.json b/maps/tests/DoorTest/map.json index b954b97c..ee3fcf1f 100644 --- a/maps/tests/DoorTest/map.json +++ b/maps/tests/DoorTest/map.json @@ -104,7 +104,7 @@ "x":0, "y":0 }], - "nextlayerid":9, + "nextlayerid":11, "nextobjectid":13, "orientation":"orthogonal", "properties":[