workadventure/front/src/Phaser/Entity/RemotePlayer.ts

109 lines
3.9 KiB
TypeScript
Raw Normal View History

import { requestVisitCardsStore, requestActionsMenuStore, actionsMenuPlayerNameStore } from "../../Stores/GameStore";
import { actionsMenuStore } from '../../Stores/ActionsMenuStore';
import { Character } from "../Entity/Character";
2021-09-01 10:11:12 +02:00
import type { GameScene } from "../Game/GameScene";
import type { PointInterface } from "../../Connexion/ConnexionModels";
import type { PlayerAnimationDirections } from "../Player/Animation";
import type { Unsubscriber } from 'svelte/store';
2020-06-04 18:54:34 +02:00
/**
* Class representing the sprite of a remote player (a player that plays on another computer)
*/
export class RemotePlayer extends Character {
userId: number;
2021-09-01 10:11:12 +02:00
private visitCardUrl: string | null;
2020-06-04 18:54:34 +02:00
private actionsMenuRequested: boolean = false;
private actionsMenuRequestedUnsubscriber: Unsubscriber;
2020-06-04 18:54:34 +02:00
constructor(
userId: number,
2020-06-04 18:54:34 +02:00
Scene: GameScene,
x: number,
y: number,
name: string,
texturesPromise: Promise<string[]>,
direction: PlayerAnimationDirections,
2021-04-02 21:21:11 +02:00
moving: boolean,
2021-09-01 10:11:12 +02:00
visitCardUrl: string | null,
companion: string | null,
companionTexturePromise?: Promise<string>
2020-06-04 18:54:34 +02:00
) {
2021-09-01 10:11:12 +02:00
super(
Scene,
x,
y,
texturesPromise,
name,
direction,
moving,
1,
!!visitCardUrl,
companion,
companionTexturePromise
);
2020-06-04 18:54:34 +02:00
//set data
this.userId = userId;
this.visitCardUrl = visitCardUrl;
this.actionsMenuRequestedUnsubscriber = requestActionsMenuStore.subscribe((value: boolean) => {
this.actionsMenuRequested = value;
});
2021-09-01 10:11:12 +02:00
this.on("pointerdown", (event: Phaser.Input.Pointer) => {
if (event.downElement.nodeName === "CANVAS") {
if (this.actionsMenuRequested) {
actionsMenuPlayerNameStore.set(null);
requestActionsMenuStore.set(false);
return;
}
actionsMenuStore.addPossibleAction(
"visit-card",
"Visiting Card", () => {
requestVisitCardsStore.set(this.visitCardUrl);
actionsMenuStore.clearActions();
requestActionsMenuStore.set(false);
});
actionsMenuStore.addPossibleAction(
"log-hello",
"Log Hello", () => {
console.log('HELLO');
// requestActionsMenuStore.set(false);
});
actionsMenuStore.addPossibleAction(
"log-goodbye",
"Log Goodbye", () => {
console.log('GOODBYE');
// requestActionsMenuStore.set(false);
});
actionsMenuStore.addPossibleAction(
"clear",
"Clear Actions", () => {
actionsMenuStore.clearActions();
});
actionsMenuPlayerNameStore.set(this.PlayerValue);
2022-01-24 16:27:57 +01:00
requestActionsMenuStore.set(true);
2021-09-01 10:11:12 +02:00
}
});
2020-06-04 18:54:34 +02:00
}
public updatePosition(position: PointInterface): void {
this.playAnimation(position.direction as PlayerAnimationDirections, position.moving);
2020-06-04 18:54:34 +02:00
this.setX(position.x);
this.setY(position.y);
this.setDepth(position.y); //this is to make sure the perspective (player models closer the bottom of the screen will appear in front of models nearer the top of the screen).
2021-04-01 18:51:51 +02:00
if (this.companion) {
this.companion.setTarget(position.x, position.y, position.direction as PlayerAnimationDirections);
}
2020-06-04 18:54:34 +02:00
}
public destroy(): void {
this.actionsMenuRequestedUnsubscriber();
requestActionsMenuStore.set(false);
super.destroy();
}
2020-06-04 18:54:34 +02:00
}