optional following path movement speed

This commit is contained in:
Hanusiak Piotr 2022-01-19 13:53:06 +01:00
parent 9b94705177
commit c023c4f8fd
5 changed files with 30 additions and 8 deletions

View File

@ -4,7 +4,7 @@ export const isMovePlayerToEventConfig = new tg.IsInterface()
.withProperties({
x: tg.isNumber,
y: tg.isNumber,
speed: tg.isNumber,
speed: tg.isOptional(tg.isNumber),
})
.get();

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 }> {
return await queryWorkadventure({
type: "movePlayerTo",
data: { x, y, speed },

View File

@ -1467,8 +1467,7 @@ ${escapedMessage}
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
path.shift();
return this.CurrentPlayer.setPathToFollow(path);
// return position;
return this.CurrentPlayer.setPathToFollow(path, message.speed);
});
}

View File

@ -13,6 +13,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 pathWalkingSpeed?: number;
constructor(
Scene: GameScene,
@ -70,15 +71,20 @@ export class Player extends Character {
this.scene.connection?.emitFollowConfirmation();
}
public async setPathToFollow(path: { x: number; y: number }[]): Promise<{ x: number; y: number }> {
public async setPathToFollow(path: { x: number; y: number }[], speed?: number): Promise<{ x: number; y: number }> {
// take collider offset into consideraton
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
this.pathWalkingSpeed = speed;
return new Promise((resolve) => {
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
this.followingPathPromiseResolve = resolve;
});
}
private deduceSpeed(speedUp: boolean, followMode: boolean): number {
return this.pathWalkingSpeed ? this.pathWalkingSpeed : speedUp && !followMode ? 25 : 9;
}
private adjustPathToFollowToColliderBounds(path: { x: number; y: number }[]): { x: number; y: number }[] {
return path.map((step) => {
return { x: step.x, y: step.y - this.getBody().offset.y };
@ -101,8 +107,8 @@ export class Player extends Character {
// Compute movement deltas
const followMode = get(followStateStore) !== "off";
const speedup = activeEvents.get(UserInputEvent.SpeedUp) && !followMode ? 25 : 9;
const moveAmount = speedup * 20;
const speed = this.deduceSpeed(activeEvents.get(UserInputEvent.SpeedUp), followMode);
const moveAmount = speed * 20;
x = x * moveAmount;
y = y * moveAmount;
@ -156,6 +162,7 @@ export class Player extends Character {
private computeFollowPathMovement(): number[] {
if (this.pathToFollow?.length === 0) {
this.pathToFollow = undefined;
this.pathWalkingSpeed = undefined;
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
}
if (!this.pathToFollow) {

View File

@ -8,11 +8,26 @@
WA.onInit().then(() => {
console.log('After WA init');
const movePlayerButton = document.getElementById('movePlayerButton');
const randomChainedMovementButton = document.getElementById('randomChainedMovementButton');
const xField = document.getElementById('x');
const yField = document.getElementById('y');
const speedField = document.getElementById('speed');
movePlayerButton.addEventListener('click', async () => {
randomChainedMovementButton.addEventListener('click', async () => {
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);
});
movePlayerButton.addEventListener('click', async () => {
const position = await WA.player.moveTo(
parseInt(xField.value),
parseInt(yField.value),
@ -30,6 +45,7 @@ Y: <input type="text" id="y" value="655" /><br/>
Speed: <input type="text" id="speed" value="20" /><br/>
<button id="movePlayerButton">Move Player</button>
<button id="randomChainedMovementButton">Do random chained movement</button>
</body>
</html>