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"> <script lang="typescript">
import { fly } from "svelte/transition"; import { fly } from "svelte/transition";
import { ActionsMenuInterface, actionsMenuStore } from '../../Stores/ActionsMenuStore'; 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 possibleActions: Map<string, ActionsMenuInterface>;
let backgroundHeight = 100; let playerName: string | null;
const unsubscribe = actionsMenuStore.subscribe(value => {
possibleActions = value; let actionsMenuStoreUnsubscriber: Unsubscriber | null;
}); let actionsMenuPlayerNameStoreUnsubscriber: Unsubscriber | null;
function onKeyDown(e: KeyboardEvent) { function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") { if (e.key === "Escape") {
@ -19,13 +22,30 @@
requestActionsMenuStore.set(false); requestActionsMenuStore.set(false);
} }
actionsMenuStoreUnsubscriber = actionsMenuStore.subscribe(value => {
possibleActions = value;
});
actionsMenuPlayerNameStoreUnsubscriber = actionsMenuPlayerNameStore.subscribe(value => {
playerName = value;
});
onDestroy(() => {
if (actionsMenuStoreUnsubscriber) {
actionsMenuStoreUnsubscriber();
}
if (actionsMenuPlayerNameStoreUnsubscriber) {
actionsMenuPlayerNameStoreUnsubscriber();
}
});
</script> </script>
<svelte:window on:keydown={onKeyDown} /> <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> <button type="button" class="nes-btn is-error close" on:click={closeActionsMenu}>&times</button>
<h2>Actions</h2> <h2>{playerName}</h2>
<div class="actions"> <div class="actions">
{#each [...possibleActions] as [key, menuAction]} {#each [...possibleActions] as [key, menuAction]}
<button <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 { GameScene } from "../Game/GameScene";
import type { PointInterface } from "../../Connexion/ConnexionModels"; import type { PointInterface } from "../../Connexion/ConnexionModels";
import { Character } from "../Entity/Character";
import type { PlayerAnimationDirections } from "../Player/Animation"; import type { PlayerAnimationDirections } from "../Player/Animation";
import { requestVisitCardsStore, requestActionsMenuStore } from "../../Stores/GameStore"; import type { Unsubscriber } from 'svelte/store';
import { actionsMenuStore } from '../../Stores/ActionsMenuStore';
/** /**
* Class representing the sprite of a remote player (a player that plays on another computer) * 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 visitCardUrl: string | null;
private actionsMenuRequested: boolean = false; private actionsMenuRequested: boolean = false;
private actionsMenuRequestedUnsubscriber: Unsubscriber;
constructor( constructor(
userId: number, userId: number,
@ -44,13 +47,14 @@ export class RemotePlayer extends Character {
//set data //set data
this.userId = userId; this.userId = userId;
this.visitCardUrl = visitCardUrl; this.visitCardUrl = visitCardUrl;
requestActionsMenuStore.subscribe((value: boolean) => { this.actionsMenuRequestedUnsubscriber = requestActionsMenuStore.subscribe((value: boolean) => {
this.actionsMenuRequested = value; this.actionsMenuRequested = value;
}); });
this.on("pointerdown", (event: Phaser.Input.Pointer) => { this.on("pointerdown", (event: Phaser.Input.Pointer) => {
if (event.downElement.nodeName === "CANVAS") { if (event.downElement.nodeName === "CANVAS") {
if (this.actionsMenuRequested) { if (this.actionsMenuRequested) {
actionsMenuPlayerNameStore.set(null);
requestActionsMenuStore.set(false); requestActionsMenuStore.set(false);
return; return;
} }
@ -78,12 +82,13 @@ export class RemotePlayer extends Character {
"Clear Actions", () => { "Clear Actions", () => {
actionsMenuStore.clearActions(); actionsMenuStore.clearActions();
}); });
actionsMenuPlayerNameStore.set(this.PlayerValue);
requestActionsMenuStore.set(true); requestActionsMenuStore.set(true);
} }
}); });
} }
updatePosition(position: PointInterface): void { public updatePosition(position: PointInterface): void {
this.playAnimation(position.direction as PlayerAnimationDirections, position.moving); this.playAnimation(position.direction as PlayerAnimationDirections, position.moving);
this.setX(position.x); this.setX(position.x);
this.setY(position.y); this.setY(position.y);
@ -94,4 +99,10 @@ export class RemotePlayer extends Character {
this.companion.setTarget(position.x, position.y, position.direction as PlayerAnimationDirections); 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; return;
} }
} }
console.log(gameObjects);
const camera = this.gameScene.getCameraManager().getCamera(); const camera = this.gameScene.getCameraManager().getCamera();
const index = this.gameScene const index = this.gameScene
.getGameMap() .getGameMap()

View File

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