2021-09-05 18:36:22 +02:00
|
|
|
import { PlayerAnimationDirections } from "./Animation";
|
|
|
|
import type { GameScene } from "../Game/GameScene";
|
2021-10-19 15:35:19 +02:00
|
|
|
import { ActiveEventList, UserInputEvent, UserInputManager } from "../UserInput/UserInputManager";
|
2021-09-05 18:36:22 +02:00
|
|
|
import { Character } from "../Entity/Character";
|
2021-10-19 15:35:19 +02:00
|
|
|
import type { RemotePlayer } from "../Entity/RemotePlayer";
|
2021-09-05 18:36:22 +02:00
|
|
|
import { userMovingStore } from "../../Stores/GameStore";
|
2020-05-02 16:54:52 +02:00
|
|
|
|
|
|
|
export const hasMovedEventName = "hasMoved";
|
2021-05-10 17:10:41 +02:00
|
|
|
export const requestEmoteEventName = "requestEmote";
|
2020-04-12 13:57:00 +02:00
|
|
|
|
2021-05-10 17:10:41 +02:00
|
|
|
export class Player extends Character {
|
2021-10-19 15:35:19 +02:00
|
|
|
private previousDirection: PlayerAnimationDirections = PlayerAnimationDirections.Down;
|
2020-08-17 22:51:37 +02:00
|
|
|
private wasMoving: boolean = false;
|
2021-10-19 15:35:19 +02:00
|
|
|
private timeCounter: number = 0;
|
|
|
|
private follow: { followPlayer: RemotePlayer; direction: PlayerAnimationDirections } | null = null;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-05-19 19:11:12 +02:00
|
|
|
Scene: GameScene,
|
2020-04-13 15:34:09 +02:00
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-05-01 23:38:09 +02:00
|
|
|
name: string,
|
2021-01-07 17:11:22 +01:00
|
|
|
texturesPromise: Promise<string[]>,
|
2021-03-11 16:13:05 +01:00
|
|
|
direction: PlayerAnimationDirections,
|
2020-07-27 22:36:07 +02:00
|
|
|
moving: boolean,
|
2021-04-02 21:21:11 +02:00
|
|
|
private userInputManager: UserInputManager,
|
2021-09-05 18:36:22 +02:00
|
|
|
companion: string | null,
|
2021-04-06 19:10:18 +02:00
|
|
|
companionTexturePromise?: Promise<string>
|
2020-04-07 19:23:21 +02:00
|
|
|
) {
|
2021-06-08 16:30:58 +02:00
|
|
|
super(Scene, x, y, texturesPromise, name, direction, moving, 1, true, companion, companionTexturePromise);
|
2020-04-13 13:42:21 +02:00
|
|
|
|
|
|
|
//the current player model should be push away by other players to prevent conflict
|
2020-07-28 17:43:33 +02:00
|
|
|
this.getBody().setImmovable(false);
|
2020-05-26 17:43:25 +02:00
|
|
|
}
|
2020-05-26 17:25:29 +02:00
|
|
|
|
2021-10-19 15:35:19 +02:00
|
|
|
private inputStep(activeEvents: ActiveEventList, delta: number) {
|
2020-04-07 19:23:21 +02:00
|
|
|
//if user client on shift, camera and player speed
|
2020-04-07 21:02:23 +02:00
|
|
|
let direction = null;
|
2020-05-22 22:59:43 +02:00
|
|
|
let moving = false;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-06-09 23:13:26 +02:00
|
|
|
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
|
|
|
const moveAmount = speedMultiplier * 20;
|
2020-04-13 13:42:21 +02:00
|
|
|
|
2020-04-30 19:36:28 +02:00
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
2021-10-19 15:35:19 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveUp)) {
|
2021-01-23 02:21:16 +01:00
|
|
|
y = -moveAmount;
|
2021-03-11 16:13:05 +01:00
|
|
|
direction = PlayerAnimationDirections.Up;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving = true;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
|
|
|
|
y = moveAmount;
|
2021-03-11 16:13:05 +01:00
|
|
|
direction = PlayerAnimationDirections.Down;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving = true;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2021-10-19 15:35:19 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveLeft)) {
|
2020-04-30 19:36:28 +02:00
|
|
|
x = -moveAmount;
|
2021-03-11 16:13:05 +01:00
|
|
|
direction = PlayerAnimationDirections.Left;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving = true;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
|
|
|
|
x = moveAmount;
|
2021-03-11 16:13:05 +01:00
|
|
|
direction = PlayerAnimationDirections.Right;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving = true;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2021-10-19 15:35:19 +02:00
|
|
|
|
2021-01-23 02:21:16 +01:00
|
|
|
moving = moving || activeEvents.get(UserInputEvent.JoystickMove);
|
2021-04-02 17:14:34 +02:00
|
|
|
|
2020-04-30 19:36:28 +02:00
|
|
|
if (x !== 0 || y !== 0) {
|
|
|
|
this.move(x, y);
|
2021-10-29 16:44:51 +02:00
|
|
|
this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y, oldX: x, oldY: y });
|
2021-01-23 02:21:16 +01:00
|
|
|
} else if (this.wasMoving && moving) {
|
|
|
|
// slow joystick movement
|
|
|
|
this.move(0, 0);
|
2021-11-09 18:10:55 +01:00
|
|
|
this.emit(hasMovedEventName, {
|
|
|
|
moving,
|
|
|
|
direction: this.previousDirection,
|
|
|
|
x: this.x,
|
|
|
|
y: this.y,
|
|
|
|
oldX: x,
|
|
|
|
oldY: y,
|
|
|
|
});
|
2021-01-23 02:21:16 +01:00
|
|
|
} else if (this.wasMoving && !moving) {
|
|
|
|
this.stop();
|
2021-11-09 18:10:55 +01:00
|
|
|
this.emit(hasMovedEventName, {
|
|
|
|
moving,
|
|
|
|
direction: this.previousDirection,
|
|
|
|
x: this.x,
|
|
|
|
y: this.y,
|
|
|
|
oldX: x,
|
|
|
|
oldY: y,
|
|
|
|
});
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 23:11:59 +02:00
|
|
|
if (direction !== null) {
|
2020-05-22 22:59:43 +02:00
|
|
|
this.previousDirection = direction;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2021-10-19 15:35:19 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
this.wasMoving = moving;
|
2021-05-20 18:05:03 +02:00
|
|
|
userMovingStore.set(moving);
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2021-04-23 13:29:23 +02:00
|
|
|
|
2021-10-19 15:35:19 +02:00
|
|
|
private followStep(activeEvents: ActiveEventList, delta: number) {
|
2021-11-18 12:23:10 +01:00
|
|
|
let moving = false;
|
|
|
|
|
2021-10-19 15:35:19 +02:00
|
|
|
if (this.follow === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timeCounter += delta;
|
|
|
|
if (this.timeCounter < 128) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (distance < 650) {
|
|
|
|
this.stop();
|
|
|
|
} else {
|
|
|
|
const moveAmount = 9 * 20;
|
|
|
|
const xDir = xDist / Math.sqrt(distance);
|
|
|
|
const yDir = yDist / Math.sqrt(distance);
|
|
|
|
|
|
|
|
this.move(xDir * moveAmount, yDir * moveAmount);
|
|
|
|
|
|
|
|
if (Math.abs(xDist) > Math.abs(yDist)) {
|
|
|
|
if (xDist < 0) {
|
|
|
|
this.follow.direction = PlayerAnimationDirections.Left;
|
|
|
|
} else {
|
|
|
|
this.follow.direction = PlayerAnimationDirections.Right;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (yDist < 0) {
|
|
|
|
this.follow.direction = PlayerAnimationDirections.Up;
|
|
|
|
} else {
|
|
|
|
this.follow.direction = PlayerAnimationDirections.Down;
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 12:23:10 +01:00
|
|
|
|
|
|
|
moving = true;
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.emit(hasMovedEventName, {
|
2021-11-18 12:23:10 +01:00
|
|
|
moving: moving,
|
2021-10-19 15:35:19 +02:00
|
|
|
direction: this.follow.direction,
|
|
|
|
x: this.x,
|
|
|
|
y: this.y,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.previousDirection = this.follow.direction;
|
|
|
|
|
2021-11-18 12:23:10 +01:00
|
|
|
this.wasMoving = moving;
|
|
|
|
userMovingStore.set(moving);
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
moveUser(delta: number): void {
|
|
|
|
const activeEvents = this.userInputManager.getEventListForGameTick();
|
|
|
|
|
|
|
|
if (activeEvents.get(UserInputEvent.Interact)) {
|
|
|
|
const sortedPlayers = Array.from(this.scene.MapPlayersByKey.values()).sort((p1, p2) => {
|
2021-10-28 16:20:57 +02:00
|
|
|
const sdistToP1 = Math.pow(p1.x - this.x, 2) + Math.pow(p1.y - this.y, 2);
|
|
|
|
const sdistToP2 = Math.pow(p2.x - this.x, 2) + Math.pow(p2.y - this.y, 2);
|
|
|
|
if (sdistToP1 > sdistToP2) {
|
2021-10-19 15:35:19 +02:00
|
|
|
return 1;
|
2021-10-28 16:20:57 +02:00
|
|
|
} else if (sdistToP1 < sdistToP2) {
|
2021-10-19 15:35:19 +02:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-28 16:20:57 +02:00
|
|
|
const minFollowDist = 10000;
|
2021-10-19 15:35:19 +02:00
|
|
|
if (typeof sortedPlayers !== "undefined" && sortedPlayers.length > 0) {
|
2021-10-28 16:20:57 +02:00
|
|
|
const sdist = Math.pow(sortedPlayers[0].x - this.x, 2) + Math.pow(sortedPlayers[0].y - this.y, 2);
|
|
|
|
if (sdist < minFollowDist) {
|
|
|
|
this.follow = {
|
|
|
|
followPlayer: sortedPlayers[0],
|
|
|
|
direction: this.previousDirection,
|
|
|
|
};
|
|
|
|
}
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
activeEvents.get(UserInputEvent.MoveUp) ||
|
|
|
|
activeEvents.get(UserInputEvent.MoveDown) ||
|
|
|
|
activeEvents.get(UserInputEvent.MoveLeft) ||
|
|
|
|
activeEvents.get(UserInputEvent.MoveRight)
|
|
|
|
) {
|
|
|
|
this.follow = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.follow === null) {
|
|
|
|
this.inputStep(activeEvents, delta);
|
|
|
|
} else {
|
|
|
|
this.followStep(activeEvents, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 13:29:23 +02:00
|
|
|
public isMoving(): boolean {
|
|
|
|
return this.wasMoving;
|
|
|
|
}
|
2020-04-18 17:16:39 +02:00
|
|
|
}
|