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:
parent
ef02a06ad7
commit
62b00f852d
6 changed files with 26 additions and 26 deletions
|
@ -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({
|
return await queryWorkadventure({
|
||||||
type: "movePlayerTo",
|
type: "movePlayerTo",
|
||||||
data: { x, y, speed },
|
data: { x, y, speed },
|
||||||
|
|
|
@ -1467,6 +1467,9 @@ ${escapedMessage}
|
||||||
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
|
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
|
||||||
const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
|
const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
|
||||||
path.shift();
|
path.shift();
|
||||||
|
if (path.length === 0) {
|
||||||
|
throw new Error("no path available");
|
||||||
|
}
|
||||||
return this.CurrentPlayer.setPathToFollow(path, message.speed);
|
return this.CurrentPlayer.setPathToFollow(path, message.speed);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +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;
|
private followingPathPromiseResolve?: (result: { x: number; y: number; cancelled: boolean }) => void;
|
||||||
private followingPathPromiseReject?: (position: { x: number; y: number }) => void;
|
|
||||||
private pathWalkingSpeed?: number;
|
private pathWalkingSpeed?: number;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -46,7 +45,7 @@ export class Player extends Character {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) {
|
if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) {
|
||||||
this.finishFollowingPath();
|
this.finishFollowingPath(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let x = 0;
|
let x = 0;
|
||||||
|
@ -71,14 +70,17 @@ export class Player extends Character {
|
||||||
this.scene.connection?.emitFollowConfirmation();
|
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
|
// take collider offset into consideraton
|
||||||
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
|
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
|
||||||
this.pathWalkingSpeed = speed;
|
this.pathWalkingSpeed = speed;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
this.followingPathPromiseReject?.call(this, { x: this.x, y: this.y });
|
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y, cancelled: isPreviousPathInProgress });
|
||||||
this.followingPathPromiseResolve = resolve;
|
this.followingPathPromiseResolve = resolve;
|
||||||
this.followingPathPromiseReject = reject;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +163,7 @@ export class Player extends Character {
|
||||||
}
|
}
|
||||||
|
|
||||||
private computeFollowPathMovement(): number[] {
|
private computeFollowPathMovement(): number[] {
|
||||||
if (this.pathToFollow?.length === 0) {
|
if (this.pathToFollow !== undefined && this.pathToFollow.length === 0) {
|
||||||
this.finishFollowingPath();
|
this.finishFollowingPath();
|
||||||
}
|
}
|
||||||
if (!this.pathToFollow) {
|
if (!this.pathToFollow) {
|
||||||
|
@ -182,8 +184,7 @@ export class Player extends Character {
|
||||||
private finishFollowingPath(cancelled: boolean = false): void {
|
private finishFollowingPath(cancelled: boolean = false): void {
|
||||||
this.pathToFollow = undefined;
|
this.pathToFollow = undefined;
|
||||||
this.pathWalkingSpeed = undefined;
|
this.pathWalkingSpeed = undefined;
|
||||||
const func = cancelled ? this.followingPathPromiseReject : this.followingPathPromiseResolve;
|
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y, cancelled });
|
||||||
func?.call(this, { x: this.x, y: this.y });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] {
|
private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] {
|
||||||
|
|
|
@ -35,9 +35,7 @@ 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).catch((reason) => {
|
this.gameScene.CurrentPlayer.setPathToFollow(path).catch((reason) => {});
|
||||||
console.warn(reason);
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch((reason) => {
|
.catch((reason) => {
|
||||||
console.warn(reason);
|
console.warn(reason);
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
"y":0
|
"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,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 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 18, 17, 18, 17, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 18, 17, 18, 17, 18, 17, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 29, 28, 29, 28, 29, 28, 29, 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, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 0, 0, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 0, 0, 0, 0, 0, 0, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 0, 0, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 0, 0, 0, 0, 0, 0, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 0, 0, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 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, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 17, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 28, 29, 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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, 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],
|
||||||
"height":30,
|
"height":30,
|
||||||
"id":6,
|
"id":6,
|
||||||
"name":"furnitures",
|
"name":"furnitures",
|
||||||
|
|
|
@ -15,17 +15,15 @@
|
||||||
|
|
||||||
randomChainedMovementButton.addEventListener('click', async () => {
|
randomChainedMovementButton.addEventListener('click', async () => {
|
||||||
try {
|
try {
|
||||||
let pos;
|
WA.player.moveTo(100, 100, 10).then((result) => {
|
||||||
pos = await WA.player.moveTo(100, 100, 10);
|
console.log(result);
|
||||||
console.log(pos);
|
WA.player.moveTo(500, 100, 20).then((result) => {
|
||||||
pos = await WA.player.moveTo(500, 100, 10);
|
console.log(result);
|
||||||
console.log(pos);
|
WA.player.moveTo(500, 500, 10).then((result) => {
|
||||||
pos = await WA.player.moveTo(500, 500, 10);
|
console.log(result);
|
||||||
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) {
|
} catch (err) {
|
||||||
console.log('movement was stopped forcefully');
|
console.log('movement was stopped forcefully');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue