Clean up follow implementation; stop following when leader leaves the scene
This commit is contained in:
parent
7bff782f7f
commit
290e5131e9
@ -32,7 +32,7 @@ export abstract class Character extends Container {
|
|||||||
private readonly playerName: Text;
|
private readonly playerName: Text;
|
||||||
public PlayerValue: string;
|
public PlayerValue: string;
|
||||||
public sprites: Map<string, Sprite>;
|
public sprites: Map<string, Sprite>;
|
||||||
private lastDirection: PlayerAnimationDirections = PlayerAnimationDirections.Down;
|
protected lastDirection: PlayerAnimationDirections = PlayerAnimationDirections.Down;
|
||||||
//private teleportation: Sprite;
|
//private teleportation: Sprite;
|
||||||
private invisible: boolean;
|
private invisible: boolean;
|
||||||
public companion?: Companion;
|
public companion?: Companion;
|
||||||
@ -266,24 +266,20 @@ export abstract class Character extends Container {
|
|||||||
|
|
||||||
body.setVelocity(x, y);
|
body.setVelocity(x, y);
|
||||||
|
|
||||||
// up or down animations are prioritized over left and right
|
if (Math.abs(body.velocity.x) > Math.abs(body.velocity.y)) {
|
||||||
if (body.velocity.y < 0) {
|
if (body.velocity.x < 0) {
|
||||||
//moving up
|
|
||||||
this.lastDirection = PlayerAnimationDirections.Up;
|
|
||||||
this.playAnimation(PlayerAnimationDirections.Up, true);
|
|
||||||
} else if (body.velocity.y > 0) {
|
|
||||||
//moving down
|
|
||||||
this.lastDirection = PlayerAnimationDirections.Down;
|
|
||||||
this.playAnimation(PlayerAnimationDirections.Down, true);
|
|
||||||
} else if (body.velocity.x > 0) {
|
|
||||||
//moving right
|
|
||||||
this.lastDirection = PlayerAnimationDirections.Right;
|
|
||||||
this.playAnimation(PlayerAnimationDirections.Right, true);
|
|
||||||
} else if (body.velocity.x < 0) {
|
|
||||||
//moving left
|
|
||||||
this.lastDirection = PlayerAnimationDirections.Left;
|
this.lastDirection = PlayerAnimationDirections.Left;
|
||||||
this.playAnimation(PlayerAnimationDirections.Left, true);
|
} else if (body.velocity.x > 0) {
|
||||||
|
this.lastDirection = PlayerAnimationDirections.Right;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (body.velocity.y < 0) {
|
||||||
|
this.lastDirection = PlayerAnimationDirections.Up;
|
||||||
|
} else if (body.velocity.y > 0) {
|
||||||
|
this.lastDirection = PlayerAnimationDirections.Down;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.playAnimation(this.lastDirection, true);
|
||||||
|
|
||||||
this.setDepth(this.y);
|
this.setDepth(this.y);
|
||||||
|
|
||||||
|
@ -1715,6 +1715,10 @@ ${escapedMessage}
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public findPlayer(testFunction: (player: RemotePlayer) => boolean): RemotePlayer | undefined {
|
||||||
|
return Array.from(this.MapPlayersByKey.values()).find(testFunction);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called by the connexion when a new player arrives on a map
|
* Called by the connexion when a new player arrives on a map
|
||||||
*/
|
*/
|
||||||
|
@ -18,11 +18,6 @@ export const hasMovedEventName = "hasMoved";
|
|||||||
export const requestEmoteEventName = "requestEmote";
|
export const requestEmoteEventName = "requestEmote";
|
||||||
|
|
||||||
export class Player extends Character {
|
export class Player extends Character {
|
||||||
private previousDirection: PlayerAnimationDirections = PlayerAnimationDirections.Down;
|
|
||||||
private wasMoving: boolean = false;
|
|
||||||
private timeCounter: number = 0;
|
|
||||||
private follow: { followPlayer: RemotePlayer; direction: PlayerAnimationDirections } | null = null;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
Scene: GameScene,
|
Scene: GameScene,
|
||||||
x: number,
|
x: number,
|
||||||
@ -41,9 +36,9 @@ export class Player extends Character {
|
|||||||
this.getBody().setImmovable(false);
|
this.getBody().setImmovable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private inputStep(activeEvents: ActiveEventList, delta: number) {
|
private inputStep(activeEvents: ActiveEventList) {
|
||||||
//if user client on shift, camera and player speed
|
//if user client on shift, camera and player speed
|
||||||
let direction = null;
|
let direction = this.lastDirection;
|
||||||
let moving = false;
|
let moving = false;
|
||||||
|
|
||||||
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
||||||
@ -77,22 +72,22 @@ export class Player extends Character {
|
|||||||
if (x !== 0 || y !== 0) {
|
if (x !== 0 || y !== 0) {
|
||||||
this.move(x, y);
|
this.move(x, y);
|
||||||
this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y, oldX: x, oldY: y });
|
this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y, oldX: x, oldY: y });
|
||||||
} else if (this.wasMoving && moving) {
|
} else if (get(userMovingStore) && moving) {
|
||||||
// slow joystick movement
|
// slow joystick movement
|
||||||
this.move(0, 0);
|
this.move(0, 0);
|
||||||
this.emit(hasMovedEventName, {
|
this.emit(hasMovedEventName, {
|
||||||
moving,
|
moving,
|
||||||
direction: this.previousDirection,
|
direction: direction,
|
||||||
x: this.x,
|
x: this.x,
|
||||||
y: this.y,
|
y: this.y,
|
||||||
oldX: x,
|
oldX: x,
|
||||||
oldY: y,
|
oldY: y,
|
||||||
});
|
});
|
||||||
} else if (this.wasMoving && !moving) {
|
} else if (get(userMovingStore) && !moving) {
|
||||||
this.stop();
|
this.stop();
|
||||||
this.emit(hasMovedEventName, {
|
this.emit(hasMovedEventName, {
|
||||||
moving,
|
moving,
|
||||||
direction: this.previousDirection,
|
direction: direction,
|
||||||
x: this.x,
|
x: this.x,
|
||||||
y: this.y,
|
y: this.y,
|
||||||
oldX: x,
|
oldX: x,
|
||||||
@ -100,35 +95,27 @@ export class Player extends Character {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (direction !== null) {
|
|
||||||
this.previousDirection = direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.wasMoving = moving;
|
|
||||||
userMovingStore.set(moving);
|
userMovingStore.set(moving);
|
||||||
}
|
}
|
||||||
|
|
||||||
private followStep(activeEvents: ActiveEventList, delta: number) {
|
private followStep(delta: number) {
|
||||||
let moving = false;
|
const player = this.scene.findPlayer((p) => p.PlayerValue === get(followUsersStore)[0]);
|
||||||
|
if (!player) {
|
||||||
if (this.follow === null) {
|
this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue);
|
||||||
|
followStateStore.set(followStates.off);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timeCounter += delta;
|
const xDist = player.x - this.x;
|
||||||
if (this.timeCounter < 128) {
|
const yDist = player.y - this.y;
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.timeCounter = 0;
|
|
||||||
|
|
||||||
const xDist = this.follow.followPlayer.x - this.x;
|
|
||||||
const yDist = this.follow.followPlayer.y - this.y;
|
|
||||||
|
|
||||||
const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2);
|
const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2);
|
||||||
|
|
||||||
|
let moving = false;
|
||||||
|
let direction = this.lastDirection;
|
||||||
if (distance < 2000) {
|
if (distance < 2000) {
|
||||||
this.stop();
|
this.stop();
|
||||||
} else {
|
} else {
|
||||||
|
moving = true;
|
||||||
const moveAmount = 9 * 20;
|
const moveAmount = 9 * 20;
|
||||||
const xDir = xDist / Math.sqrt(distance);
|
const xDir = xDist / Math.sqrt(distance);
|
||||||
const yDir = yDist / Math.sqrt(distance);
|
const yDir = yDist / Math.sqrt(distance);
|
||||||
@ -136,46 +123,24 @@ export class Player extends Character {
|
|||||||
this.move(xDir * moveAmount, yDir * moveAmount);
|
this.move(xDir * moveAmount, yDir * moveAmount);
|
||||||
|
|
||||||
if (Math.abs(xDist) > Math.abs(yDist)) {
|
if (Math.abs(xDist) > Math.abs(yDist)) {
|
||||||
if (xDist < 0) {
|
direction = xDist < 0 ? PlayerAnimationDirections.Left : PlayerAnimationDirections.Right;
|
||||||
this.follow.direction = PlayerAnimationDirections.Left;
|
|
||||||
} else {
|
} else {
|
||||||
this.follow.direction = PlayerAnimationDirections.Right;
|
direction = yDist < 0 ? PlayerAnimationDirections.Up : PlayerAnimationDirections.Down;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (yDist < 0) {
|
|
||||||
this.follow.direction = PlayerAnimationDirections.Up;
|
|
||||||
} else {
|
|
||||||
this.follow.direction = PlayerAnimationDirections.Down;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
moving = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit(hasMovedEventName, {
|
this.emit(hasMovedEventName, {
|
||||||
moving: moving,
|
moving: moving,
|
||||||
direction: this.follow.direction,
|
direction: direction,
|
||||||
x: this.x,
|
x: this.x,
|
||||||
y: this.y,
|
y: this.y,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.previousDirection = this.follow.direction;
|
|
||||||
|
|
||||||
this.wasMoving = moving;
|
|
||||||
userMovingStore.set(moving);
|
userMovingStore.set(moving);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enableFollowing() {
|
public enableFollowing() {
|
||||||
Array.from(this.scene.MapPlayersByKey.values()).forEach((player) => {
|
|
||||||
if (player.PlayerValue !== get(followUsersStore)[0]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.follow = {
|
|
||||||
followPlayer: player,
|
|
||||||
direction: this.previousDirection,
|
|
||||||
};
|
|
||||||
followStateStore.set(followStates.active);
|
followStateStore.set(followStates.active);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public moveUser(delta: number): void {
|
public moveUser(delta: number): void {
|
||||||
@ -193,13 +158,9 @@ export class Player extends Character {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((state !== followStates.active && state !== followStates.ending) || role !== followRoles.follower) {
|
if ((state !== followStates.active && state !== followStates.ending) || role !== followRoles.follower) {
|
||||||
this.inputStep(activeEvents, delta);
|
this.inputStep(activeEvents);
|
||||||
} else {
|
} else {
|
||||||
this.followStep(activeEvents, delta);
|
this.followStep(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public isMoving(): boolean {
|
|
||||||
return this.wasMoving;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user