Adding localStorage for selected user

This commit is contained in:
David Négrier 2020-05-26 22:26:50 +02:00
parent 52e3b47cc1
commit f69f99f449
2 changed files with 14 additions and 7 deletions

View File

@ -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') ?? '';
}
}

View File

@ -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));
}
}
}