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-12-12 16:56:26 +01:00
|
|
|
|
|
|
|
import { get } from "svelte/store";
|
2021-09-05 18:36:22 +02:00
|
|
|
import { userMovingStore } from "../../Stores/GameStore";
|
2021-12-12 16:56:26 +01:00
|
|
|
import {
|
|
|
|
followStateStore,
|
|
|
|
followRoleStore,
|
|
|
|
followUsersStore,
|
|
|
|
followRoles,
|
|
|
|
followStates,
|
|
|
|
} from "../../Stores/InteractStore";
|
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 {
|
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-12-15 00:10:28 +01:00
|
|
|
private inputStep(activeEvents: ActiveEventList, x: number, y: number) {
|
|
|
|
// Process input events
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveUp)) {
|
2021-12-15 00:10:28 +01:00
|
|
|
y = y - 1;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
|
2021-12-15 00:10:28 +01:00
|
|
|
y = y + 1;
|
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)) {
|
2021-12-15 00:10:28 +01:00
|
|
|
x = x - 1;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
|
2021-12-15 00:10:28 +01:00
|
|
|
x = x + 1;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2021-10-19 15:35:19 +02:00
|
|
|
|
2021-12-15 00:10:28 +01:00
|
|
|
// Compute movement deltas
|
|
|
|
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
|
|
|
const moveAmount = speedMultiplier * 20;
|
|
|
|
x = x * moveAmount;
|
|
|
|
y = y * moveAmount;
|
|
|
|
|
|
|
|
// Compute moving state
|
|
|
|
const joystickMovement = activeEvents.get(UserInputEvent.JoystickMove);
|
|
|
|
const moving = x !== 0 || y !== 0 || joystickMovement;
|
|
|
|
|
|
|
|
// Compute direction
|
|
|
|
let direction = this.lastDirection;
|
|
|
|
if (moving && !joystickMovement) {
|
|
|
|
if (Math.abs(x) > Math.abs(y)) {
|
|
|
|
direction = x < 0 ? PlayerAnimationDirections.Left : PlayerAnimationDirections.Right;
|
|
|
|
} else {
|
|
|
|
direction = y < 0 ? PlayerAnimationDirections.Up : PlayerAnimationDirections.Down;
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 17:14:34 +02:00
|
|
|
|
2021-12-15 00:10:28 +01:00
|
|
|
// Send movement events
|
|
|
|
const emit = () => this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y });
|
|
|
|
if (moving) {
|
2020-04-30 19:36:28 +02:00
|
|
|
this.move(x, y);
|
2021-12-15 00:10:28 +01:00
|
|
|
emit();
|
|
|
|
} else if (get(userMovingStore)) {
|
2021-01-23 02:21:16 +01:00
|
|
|
this.stop();
|
2021-12-15 00:10:28 +01:00
|
|
|
emit();
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 00:10:28 +01:00
|
|
|
// Update state
|
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-12-15 00:10:28 +01:00
|
|
|
private computeFollowMovement(): number[] {
|
|
|
|
// Find followed WOKA and abort following if we lost it
|
2021-12-15 14:48:45 +01:00
|
|
|
const player = this.scene.MapPlayersByKey.get(get(followUsersStore)[0]);
|
2021-12-14 18:47:51 +01:00
|
|
|
if (!player) {
|
2021-12-15 14:48:45 +01:00
|
|
|
this.scene.connection?.emitFollowAbort();
|
2021-12-14 18:47:51 +01:00
|
|
|
followStateStore.set(followStates.off);
|
2021-12-15 00:10:28 +01:00
|
|
|
return [0, 0];
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 00:10:28 +01:00
|
|
|
// Compute movement direction
|
|
|
|
const xDistance = player.x - this.x;
|
|
|
|
const yDistance = player.y - this.y;
|
|
|
|
const distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
|
2021-11-30 11:48:59 +01:00
|
|
|
if (distance < 2000) {
|
2021-12-15 00:10:28 +01:00
|
|
|
return [0, 0];
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
2021-12-15 00:10:28 +01:00
|
|
|
const xMovement = xDistance / Math.sqrt(distance);
|
|
|
|
const yMovement = yDistance / Math.sqrt(distance);
|
|
|
|
return [xMovement, yMovement];
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-12-12 16:56:26 +01:00
|
|
|
public enableFollowing() {
|
2021-12-14 18:47:51 +01:00
|
|
|
followStateStore.set(followStates.active);
|
2021-12-12 16:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public moveUser(delta: number): void {
|
2021-10-19 15:35:19 +02:00
|
|
|
const activeEvents = this.userInputManager.getEventListForGameTick();
|
2021-12-12 16:56:26 +01:00
|
|
|
const state = get(followStateStore);
|
|
|
|
const role = get(followRoleStore);
|
2021-10-19 15:35:19 +02:00
|
|
|
|
|
|
|
if (activeEvents.get(UserInputEvent.Interact)) {
|
2021-12-12 16:56:26 +01:00
|
|
|
if (state === followStates.off && this.scene.groups.size > 0) {
|
|
|
|
followStateStore.set(followStates.requesting);
|
|
|
|
followRoleStore.set(followRoles.leader);
|
|
|
|
} else if (state === followStates.active) {
|
|
|
|
followStateStore.set(followStates.ending);
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 00:10:28 +01:00
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
|
|
|
if ((state === followStates.active || state === followStates.ending) && role === followRoles.follower) {
|
|
|
|
[x, y] = this.computeFollowMovement();
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
2021-12-15 00:10:28 +01:00
|
|
|
this.inputStep(activeEvents, x, y);
|
2021-10-19 15:35:19 +02:00
|
|
|
}
|
2020-04-18 17:16:39 +02:00
|
|
|
}
|