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 // TODO: walk player to position, wait for promise to resolve
const index = this.getGameMap().getTileIndexAt(message.x, message.y); const index = this.getGameMap().getTileIndexAt(message.x, message.y);
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y); const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
this.getPathfindingManager() const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
.findPath(startTile, index, true, true)
.then((path) => {
// Remove first step as it is for the tile we are currently standing on
path.shift(); path.shift();
this.CurrentPlayer.setPathToFollow(path); return this.CurrentPlayer.setPathToFollow(path);
}) // return position;
.catch((reason) => {
console.warn(reason);
});
return {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y,
};
}); });
} }

View File

@ -12,6 +12,7 @@ export const requestEmoteEventName = "requestEmote";
export class Player extends Character { export class Player extends Character {
private pathToFollow?: { x: number; y: number }[]; private pathToFollow?: { x: number; y: number }[];
private followingPathPromiseResolve?: (position: { x: number; y: number }) => void;
constructor( constructor(
Scene: GameScene, Scene: GameScene,
@ -44,6 +45,7 @@ export class Player extends Character {
if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) { if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) {
this.pathToFollow = undefined; this.pathToFollow = undefined;
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
} }
let x = 0; let x = 0;
@ -68,9 +70,13 @@ export class Player extends Character {
this.scene.connection?.emitFollowConfirmation(); 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 // take collider offset into consideraton
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); 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 }[] { private adjustPathToFollowToColliderBounds(path: { x: number; y: number }[]): { x: number; y: number }[] {
@ -150,6 +156,7 @@ export class Player extends Character {
private computeFollowPathMovement(): number[] { private computeFollowPathMovement(): number[] {
if (this.pathToFollow?.length === 0) { if (this.pathToFollow?.length === 0) {
this.pathToFollow = undefined; this.pathToFollow = undefined;
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
} }
if (!this.pathToFollow) { if (!this.pathToFollow) {
return [0, 0]; return [0, 0];

View File

@ -35,7 +35,9 @@ export class GameSceneUserInputHandler implements UserInputHandlerInterface {
.then((path) => { .then((path) => {
// Remove first step as it is for the tile we are currently standing on // Remove first step as it is for the tile we are currently standing on
path.shift(); path.shift();
this.gameScene.CurrentPlayer.setPathToFollow(path); this.gameScene.CurrentPlayer.setPathToFollow(path).catch((reason) => {
console.warn(reason);
});
}) })
.catch((reason) => { .catch((reason) => {
console.warn(reason); console.warn(reason);