From 8f40e7a33cb54c5eeaecd54104fb37cbbad28881 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Wed, 19 Jan 2022 14:16:05 +0100 Subject: [PATCH] reject path following path promise if interrupted --- front/src/Phaser/Player/Player.ts | 20 +++++++++++++------- maps/tests/MovePlayer/script.php | 26 +++++++++++++++----------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ae661e54..62576d16 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -13,6 +13,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 pathWalkingSpeed?: number; constructor( @@ -45,8 +46,7 @@ export class Player extends Character { } if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) { - this.pathToFollow = undefined; - this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); + this.finishFollowingPath(); } let x = 0; @@ -75,9 +75,10 @@ export class Player extends Character { // take collider offset into consideraton this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); this.pathWalkingSpeed = speed; - return new Promise((resolve) => { - this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); + return new Promise((resolve, reject) => { + this.followingPathPromiseReject?.call(this, { x: this.x, y: this.y }); this.followingPathPromiseResolve = resolve; + this.followingPathPromiseReject = reject; }); } @@ -161,9 +162,7 @@ export class Player extends Character { private computeFollowPathMovement(): number[] { if (this.pathToFollow?.length === 0) { - this.pathToFollow = undefined; - this.pathWalkingSpeed = undefined; - this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); + this.finishFollowingPath(); } if (!this.pathToFollow) { return [0, 0]; @@ -180,6 +179,13 @@ export class Player extends Character { return this.getMovementDirection(xDistance, yDistance, distance); } + 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 }); + } + private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] { return [xDistance / Math.sqrt(distance), yDistance / Math.sqrt(distance)]; } diff --git a/maps/tests/MovePlayer/script.php b/maps/tests/MovePlayer/script.php index 30d250a0..c505b939 100644 --- a/maps/tests/MovePlayer/script.php +++ b/maps/tests/MovePlayer/script.php @@ -14,17 +14,21 @@ const speedField = document.getElementById('speed'); randomChainedMovementButton.addEventListener('click', async () => { - 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); + 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); + } catch (err) { + console.log('movement was stopped forcefully'); + } }); movePlayerButton.addEventListener('click', async () => {