manually resolve promise on certain events for player path following

This commit is contained in:
Hanusiak Piotr 2022-01-19 13:09:17 +01:00
parent f78392ceab
commit 9b94705177
3 changed files with 16 additions and 17 deletions

View file

@ -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;
});
}

View file

@ -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];

View file

@ -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);