From f69f99f44903efccf573b1a1dae2be78ab8d2bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Tue, 26 May 2020 22:26:50 +0200 Subject: [PATCH] Adding localStorage for selected user --- front/src/Phaser/Login/LoginScene.ts | 4 ++-- front/src/Phaser/Login/SelectCharacterScene.ts | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/front/src/Phaser/Login/LoginScene.ts b/front/src/Phaser/Login/LoginScene.ts index e406e752..da684da2 100644 --- a/front/src/Phaser/Login/LoginScene.ts +++ b/front/src/Phaser/Login/LoginScene.ts @@ -27,8 +27,8 @@ export class LoginScene extends Phaser.Scene { super({ key: LoginSceneName }); - if (window.localStorage && window.localStorage.playerName) { - this.name = window.localStorage.getItem('playerName'); + if (window.localStorage) { + this.name = window.localStorage.getItem('playerName') ?? ''; } } diff --git a/front/src/Phaser/Login/SelectCharacterScene.ts b/front/src/Phaser/Login/SelectCharacterScene.ts index 222b84ce..98b5262e 100644 --- a/front/src/Phaser/Login/SelectCharacterScene.ts +++ b/front/src/Phaser/Login/SelectCharacterScene.ts @@ -100,6 +100,14 @@ export class SelectCharacterScene extends Phaser.Scene { /*create user*/ this.createCurrentPlayer(); + + if (window.localStorage) { + let playerNumberStr: string = window.localStorage.getItem('selectedPlayer') ?? '0'; + let playerNumber: number = Number(playerNumberStr); + this.selectedRectangleXPos = playerNumber % this.nbCharactersPerRow; + this.selectedRectangleYPos = Math.floor(playerNumber / this.nbCharactersPerRow); + this.updateSelectedPlayer(); + } } update(time: number, delta: number): void { @@ -170,13 +178,9 @@ export class SelectCharacterScene extends Phaser.Scene { repeat: -1 }); player.setInteractive().on("pointerdown", () => { - this.selectedPlayer.anims.pause(); - this.selectedRectangle.setY(player.y); - this.selectedRectangle.setX(player.x); - player.play(playerResource.name); - this.selectedPlayer = player; this.selectedRectangleXPos = col; this.selectedRectangleYPos = row; + this.updateSelectedPlayer(); }); this.players.push(player); } @@ -203,5 +207,8 @@ export class SelectCharacterScene extends Phaser.Scene { let player = this.players[playerNumber]; player.play(PLAYER_RESOURCES[playerNumber].name); this.selectedPlayer = player; + if (window.localStorage) { + window.localStorage.setItem('selectedPlayer', String(playerNumber)); + } } }