throw an exception if no path found when using player.moveTo command. Cancelling path following no longer rejects the promise

This commit is contained in:
Hanusiak Piotr 2022-01-19 17:22:28 +01:00
parent ef02a06ad7
commit 62b00f852d
6 changed files with 26 additions and 26 deletions

View file

@ -84,7 +84,7 @@ export class WorkadventurePlayerCommands extends IframeApiContribution<Workadven
});
}
public async moveTo(x: number, y: number, speed?: number): Promise<{ x: number; y: number }> {
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 },

View file

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

View file

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

View file

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

View file

@ -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, 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ton.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');
}