diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index fc41c074..5b61ebc5 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1461,24 +1461,14 @@ ${escapedMessage} }; }); - iframeListener.registerAnswerer("movePlayerTo", (message) => { + iframeListener.registerAnswerer("movePlayerTo", async (message) => { // TODO: walk player to position, wait for promise to resolve const index = this.getGameMap().getTileIndexAt(message.x, message.y); const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y); - this.getPathfindingManager() - .findPath(startTile, index, true, true) - .then((path) => { - // Remove first step as it is for the tile we are currently standing on - path.shift(); - this.CurrentPlayer.setPathToFollow(path); - }) - .catch((reason) => { - console.warn(reason); - }); - return { - x: this.CurrentPlayer.x, - y: this.CurrentPlayer.y, - }; + const path = await this.getPathfindingManager().findPath(startTile, index, true, true); + path.shift(); + return this.CurrentPlayer.setPathToFollow(path); + // return position; }); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 451a057b..60e5cdcf 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -12,6 +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; constructor( Scene: GameScene, @@ -44,6 +45,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 }); } let x = 0; @@ -68,9 +70,13 @@ export class Player extends Character { this.scene.connection?.emitFollowConfirmation(); } - public setPathToFollow(path: { x: number; y: number }[]): void { + public async setPathToFollow(path: { x: number; y: number }[]): Promise<{ x: number; y: number }> { // take collider offset into consideraton this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); + return new Promise((resolve) => { + this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); + this.followingPathPromiseResolve = resolve; + }); } private adjustPathToFollowToColliderBounds(path: { x: number; y: number }[]): { x: number; y: number }[] { @@ -150,6 +156,7 @@ export class Player extends Character { private computeFollowPathMovement(): number[] { if (this.pathToFollow?.length === 0) { this.pathToFollow = undefined; + this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); } if (!this.pathToFollow) { return [0, 0]; diff --git a/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts b/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts index 9874794c..9fdb0ce8 100644 --- a/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts +++ b/front/src/Phaser/UserInput/GameSceneUserInputHandler.ts @@ -35,7 +35,9 @@ 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); + this.gameScene.CurrentPlayer.setPathToFollow(path).catch((reason) => { + console.warn(reason); + }); }) .catch((reason) => { console.warn(reason);