destroy actionsMenu on RemotePlayer destroy()

This commit is contained in:
Hanusiak Piotr 2022-01-25 17:13:52 +01:00
parent 88f3032298
commit 58227a39f8
4 changed files with 45 additions and 13 deletions

View File

@ -1,13 +1,16 @@
<script lang="typescript">
import { fly } from "svelte/transition";
import { ActionsMenuInterface, actionsMenuStore } from '../../Stores/ActionsMenuStore';
import { requestActionsMenuStore, requestVisitCardsStore } from '../../Stores/GameStore';
import { requestActionsMenuStore, actionsMenuPlayerNameStore, requestVisitCardsStore } from '../../Stores/GameStore';
import { onDestroy, onMount, } from "svelte";
import type { Unsubscriber } from "svelte/store";
let possibleActions: Map<string, ActionsMenuInterface>;
let backgroundHeight = 100;
const unsubscribe = actionsMenuStore.subscribe(value => {
possibleActions = value;
});
let playerName: string | null;
let actionsMenuStoreUnsubscriber: Unsubscriber | null;
let actionsMenuPlayerNameStoreUnsubscriber: Unsubscriber | null;
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
@ -19,13 +22,30 @@
requestActionsMenuStore.set(false);
}
actionsMenuStoreUnsubscriber = actionsMenuStore.subscribe(value => {
possibleActions = value;
});
actionsMenuPlayerNameStoreUnsubscriber = actionsMenuPlayerNameStore.subscribe(value => {
playerName = value;
});
onDestroy(() => {
if (actionsMenuStoreUnsubscriber) {
actionsMenuStoreUnsubscriber();
}
if (actionsMenuPlayerNameStoreUnsubscriber) {
actionsMenuPlayerNameStoreUnsubscriber();
}
});
</script>
<svelte:window on:keydown={onKeyDown} />
<div class="actions-menu nes-container is-rounded" style="--background-height: {backgroundHeight};">
<div class="actions-menu nes-container is-rounded">
<button type="button" class="nes-btn is-error close" on:click={closeActionsMenu}>&times</button>
<h2>Actions</h2>
<h2>{playerName}</h2>
<div class="actions">
{#each [...possibleActions] as [key, menuAction]}
<button

View File

@ -1,9 +1,11 @@
import { requestVisitCardsStore, requestActionsMenuStore, actionsMenuPlayerNameStore } from "../../Stores/GameStore";
import { actionsMenuStore } from '../../Stores/ActionsMenuStore';
import { Character } from "../Entity/Character";
import type { GameScene } from "../Game/GameScene";
import type { PointInterface } from "../../Connexion/ConnexionModels";
import { Character } from "../Entity/Character";
import type { PlayerAnimationDirections } from "../Player/Animation";
import { requestVisitCardsStore, requestActionsMenuStore } from "../../Stores/GameStore";
import { actionsMenuStore } from '../../Stores/ActionsMenuStore';
import type { Unsubscriber } from 'svelte/store';
/**
* Class representing the sprite of a remote player (a player that plays on another computer)
@ -13,6 +15,7 @@ export class RemotePlayer extends Character {
private visitCardUrl: string | null;
private actionsMenuRequested: boolean = false;
private actionsMenuRequestedUnsubscriber: Unsubscriber;
constructor(
userId: number,
@ -44,13 +47,14 @@ export class RemotePlayer extends Character {
//set data
this.userId = userId;
this.visitCardUrl = visitCardUrl;
requestActionsMenuStore.subscribe((value: boolean) => {
this.actionsMenuRequestedUnsubscriber = requestActionsMenuStore.subscribe((value: boolean) => {
this.actionsMenuRequested = value;
});
this.on("pointerdown", (event: Phaser.Input.Pointer) => {
if (event.downElement.nodeName === "CANVAS") {
if (this.actionsMenuRequested) {
actionsMenuPlayerNameStore.set(null);
requestActionsMenuStore.set(false);
return;
}
@ -78,12 +82,13 @@ export class RemotePlayer extends Character {
"Clear Actions", () => {
actionsMenuStore.clearActions();
});
actionsMenuPlayerNameStore.set(this.PlayerValue);
requestActionsMenuStore.set(true);
}
});
}
updatePosition(position: PointInterface): void {
public updatePosition(position: PointInterface): void {
this.playAnimation(position.direction as PlayerAnimationDirections, position.moving);
this.setX(position.x);
this.setY(position.y);
@ -94,4 +99,10 @@ export class RemotePlayer extends Character {
this.companion.setTarget(position.x, position.y, position.direction as PlayerAnimationDirections);
}
}
public destroy(): void {
this.actionsMenuRequestedUnsubscriber();
requestActionsMenuStore.set(false);
super.destroy();
}
}

View File

@ -30,7 +30,6 @@ export class GameSceneUserInputHandler implements UserInputHandlerInterface {
return;
}
}
console.log(gameObjects);
const camera = this.gameScene.getCameraManager().getCamera();
const index = this.gameScene
.getGameMap()

View File

@ -6,6 +6,8 @@ export const requestVisitCardsStore = writable<string | null>(null);
export const requestActionsMenuStore = writable(false);
export const actionsMenuPlayerNameStore = writable<string | null>(null);
export const userIsAdminStore = writable(false);
export const limitMapStore = writable(false);