From 62b00f852ddd994f2a79075a74d9f5fe9f1d12e8 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Wed, 19 Jan 2022 17:22:28 +0100 Subject: [PATCH] throw an exception if no path found when using player.moveTo command. Cancelling path following no longer rejects the promise --- front/src/Api/iframe/player.ts | 2 +- front/src/Phaser/Game/GameScene.ts | 3 +++ front/src/Phaser/Player/Player.ts | 21 ++++++++++--------- .../UserInput/GameSceneUserInputHandler.ts | 4 +--- .../MovePlayer/move_player_api_test.json | 2 +- maps/tests/MovePlayer/script.php | 20 ++++++++---------- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/front/src/Api/iframe/player.ts b/front/src/Api/iframe/player.ts index f2014f55..cf10dfa5 100644 --- a/front/src/Api/iframe/player.ts +++ b/front/src/Api/iframe/player.ts @@ -84,7 +84,7 @@ export class WorkadventurePlayerCommands extends IframeApiContribution { + public async moveTo(x: number, y: number, speed?: number): Promise<{ x: number; y: number; cancelled: boolean }> { return await queryWorkadventure({ type: "movePlayerTo", data: { x, y, speed }, diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index a92e65be..739e195d 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1467,6 +1467,9 @@ ${escapedMessage} const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y); const path = await this.getPathfindingManager().findPath(startTile, index, true, true); path.shift(); + if (path.length === 0) { + throw new Error("no path available"); + } return this.CurrentPlayer.setPathToFollow(path, message.speed); }); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 62576d16..c5134f1c 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -12,8 +12,7 @@ export const requestEmoteEventName = "requestEmote"; export class Player extends Character { private pathToFollow?: { x: number; y: number }[]; - private followingPathPromiseResolve?: (position: { x: number; y: number }) => void; - private followingPathPromiseReject?: (position: { x: number; y: number }) => void; + private followingPathPromiseResolve?: (result: { x: number; y: number; cancelled: boolean }) => void; private pathWalkingSpeed?: number; constructor( @@ -46,7 +45,7 @@ export class Player extends Character { } if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) { - this.finishFollowingPath(); + this.finishFollowingPath(true); } let x = 0; @@ -71,14 +70,17 @@ export class Player extends Character { this.scene.connection?.emitFollowConfirmation(); } - public async setPathToFollow(path: { x: number; y: number }[], speed?: number): Promise<{ x: number; y: number }> { + public async setPathToFollow( + path: { x: number; y: number }[], + speed?: number + ): Promise<{ x: number; y: number; cancelled: boolean }> { + const isPreviousPathInProgress = this.pathToFollow !== undefined && this.pathToFollow.length > 0; // take collider offset into consideraton this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); this.pathWalkingSpeed = speed; - return new Promise((resolve, reject) => { - this.followingPathPromiseReject?.call(this, { x: this.x, y: this.y }); + return new Promise((resolve) => { + this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y, cancelled: isPreviousPathInProgress }); this.followingPathPromiseResolve = resolve; - this.followingPathPromiseReject = reject; }); } @@ -161,7 +163,7 @@ export class Player extends Character { } private computeFollowPathMovement(): number[] { - if (this.pathToFollow?.length === 0) { + if (this.pathToFollow !== undefined && this.pathToFollow.length === 0) { this.finishFollowingPath(); } if (!this.pathToFollow) { @@ -182,8 +184,7 @@ export class Player extends Character { private finishFollowingPath(cancelled: boolean = false): void { this.pathToFollow = undefined; this.pathWalkingSpeed = undefined; - const func = cancelled ? this.followingPathPromiseReject : this.followingPathPromiseResolve; - func?.call(this, { x: this.x, y: this.y }); + this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y, cancelled }); } private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] { diff --git a/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts b/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts index 9fdb0ce8..19bb02bb 100644 --- a/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts +++ b/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts @@ -35,9 +35,7 @@ export class GameSceneUserInputHandler implements UserInputHandlerInterface { .then((path) => { // Remove first step as it is for the tile we are currently standing on path.shift(); - this.gameScene.CurrentPlayer.setPathToFollow(path).catch((reason) => { - console.warn(reason); - }); + this.gameScene.CurrentPlayer.setPathToFollow(path).catch((reason) => {}); }) .catch((reason) => { console.warn(reason); diff --git a/maps/tests/MovePlayer/move_player_api_test.json b/maps/tests/MovePlayer/move_player_api_test.json index 965428e7..d9dc63c6 100644 --- a/maps/tests/MovePlayer/move_player_api_test.json +++ b/maps/tests/MovePlayer/move_player_api_test.json @@ -26,7 +26,7 @@ "y":0 }, { - "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 28, 29, 28, 29, 28, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "height":30, "id":6, "name":"furnitures", diff --git a/maps/tests/MovePlayer/script.php b/maps/tests/MovePlayer/script.php index c505b939..9676c914 100644 --- a/maps/tests/MovePlayer/script.php +++ b/maps/tests/MovePlayer/script.php @@ -15,17 +15,15 @@ randomChainedMovementButton.addEventListener('click', async () => { try { - let pos; - pos = await WA.player.moveTo(100, 100, 10); - console.log(pos); - pos = await WA.player.moveTo(500, 100, 10); - console.log(pos); - pos = await WA.player.moveTo(500, 500, 10); - console.log(pos); - pos = await WA.player.moveTo(100, 500, 10); - console.log(pos); - pos = await WA.player.moveTo(100, 100, 10); - console.log(pos); + WA.player.moveTo(100, 100, 10).then((result) => { + console.log(result); + WA.player.moveTo(500, 100, 20).then((result) => { + console.log(result); + WA.player.moveTo(500, 500, 10).then((result) => { + console.log(result); + }); + }); + }); } catch (err) { console.log('movement was stopped forcefully'); }