FIX: going back a 2nd time to SelectCharacterScene used to crash the game

This commit is contained in:
kharhamel 2020-12-16 15:09:58 +01:00
parent f5e77ad749
commit eb88db92be
6 changed files with 32 additions and 19 deletions

View File

@ -9,7 +9,7 @@
margin: 10px; margin: 10px;
} }
</style> </style>
<main id="menuIcon"> <main id="menuIcon" hidden>
<section> <section>
<button id="openMenuButton">Menu</button> <button id="openMenuButton">Menu</button>
</section> </section>

View File

@ -1,7 +1,7 @@
import {GameScene} from "./GameScene"; import {GameScene} from "./GameScene";
import {connectionManager} from "../../Connexion/ConnectionManager"; import {connectionManager} from "../../Connexion/ConnectionManager";
import {Room} from "../../Connexion/Room"; import {Room} from "../../Connexion/Room";
import {MenuSceneName} from "../Menu/MenuScene"; import {MenuScene, MenuSceneName} from "../Menu/MenuScene";
import {LoginSceneName} from "../Login/LoginScene"; import {LoginSceneName} from "../Login/LoginScene";
import {SelectCharacterSceneName} from "../Login/SelectCharacterScene"; import {SelectCharacterSceneName} from "../Login/SelectCharacterScene";
import {EnableCameraSceneName} from "../Login/EnableCameraScene"; import {EnableCameraSceneName} from "../Login/EnableCameraScene";
@ -74,20 +74,28 @@ export class GameManager {
public goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin): void { public goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin): void {
console.log('starting '+ (this.currentGameSceneName || this.startRoom.id)) console.log('starting '+ (this.currentGameSceneName || this.startRoom.id))
scenePlugin.start(this.currentGameSceneName || this.startRoom.id); scenePlugin.start(this.currentGameSceneName || this.startRoom.id);
//the menu scene launches faster than the gameScene, so we delay it to not have menu buttons on a black screen scenePlugin.launch(MenuSceneName);
setTimeout(() => scenePlugin.launch(MenuSceneName), 1000); }
public gameSceneIsCreated(scene: GameScene) {
this.currentGameSceneName = scene.scene.key;
const menuScene: MenuScene = scene.scene.get(MenuSceneName) as MenuScene;
menuScene.revealMenuIcon();
} }
/** /**
* Temporary leave a gameScene to go back to the loginScene for example. * Temporary leave a gameScene to go back to the loginScene for example.
* This will close the socket connections and stop the gameScene, but won't remove it. * This will close the socket connections and stop the gameScene, but won't remove it.
*/ */
leaveGame(scene: Phaser.Scene, targetSceneName: string): void { leaveGame(scene: Phaser.Scene, targetSceneName: string, sceneClass: Phaser.Scene): void {
if (this.currentGameSceneName === null) throw 'No current scene id set!'; if (this.currentGameSceneName === null) throw 'No current scene id set!';
const gameScene: GameScene = scene.scene.get(this.currentGameSceneName) as GameScene; const gameScene: GameScene = scene.scene.get(this.currentGameSceneName) as GameScene;
gameScene.cleanupClosingScene(); gameScene.cleanupClosingScene();
scene.scene.stop(this.currentGameSceneName); scene.scene.stop(this.currentGameSceneName);
scene.scene.stop(MenuSceneName); scene.scene.sleep(MenuSceneName);
if (!scene.scene.get(targetSceneName)) {
scene.scene.add(targetSceneName, sceneClass, false);
}
scene.scene.run(targetSceneName); scene.scene.run(targetSceneName);
} }
@ -97,8 +105,7 @@ export class GameManager {
tryResumingGame(scene: Phaser.Scene, fallbackSceneName: string) { tryResumingGame(scene: Phaser.Scene, fallbackSceneName: string) {
if (this.currentGameSceneName) { if (this.currentGameSceneName) {
scene.scene.start(this.currentGameSceneName); scene.scene.start(this.currentGameSceneName);
//the menu scene launches faster than the gameScene, so we delay it to not have menu buttons on a black screen scene.scene.wake(MenuSceneName);
setTimeout(() => scene.scene.launch(MenuSceneName), 1000);
} else { } else {
scene.scene.run(fallbackSceneName) scene.scene.run(fallbackSceneName)
} }

View File

@ -311,7 +311,7 @@ export class GameScene extends ResizableScene implements CenterListener {
//hook create scene //hook create scene
create(): void { create(): void {
gameManager.currentGameSceneName = this.scene.key; gameManager.gameSceneIsCreated(this);
urlManager.pushRoomIdToUrl(this.room); urlManager.pushRoomIdToUrl(this.room);
this.startLayerName = urlManager.getStartLayerNameFromUrl(); this.startLayerName = urlManager.getStartLayerNameFromUrl();

View File

@ -84,8 +84,9 @@ export class LoginScene extends ResizableScene {
private login(name: string): void { private login(name: string): void {
gameManager.setPlayerName(name); gameManager.setPlayerName(name);
this.scene.sleep(LoginSceneName) this.scene.stop(LoginSceneName)
gameManager.tryResumingGame(this, SelectCharacterSceneName); gameManager.tryResumingGame(this, SelectCharacterSceneName);
this.scene.remove(LoginSceneName)
} }
public onResize(ev: UIEvent): void { public onResize(ev: UIEvent): void {

View File

@ -116,13 +116,14 @@ export class SelectCharacterScene extends ResizableScene {
} }
private nextScene(): void { private nextScene(): void {
this.scene.sleep(SelectCharacterSceneName); this.scene.stop(SelectCharacterSceneName);
if (this.selectedPlayer !== null) { if (this.selectedPlayer !== null) {
gameManager.setCharacterLayers([this.selectedPlayer.texture.key]); gameManager.setCharacterLayers([this.selectedPlayer.texture.key]);
gameManager.tryResumingGame(this, EnableCameraSceneName); gameManager.tryResumingGame(this, EnableCameraSceneName);
} else { } else {
this.scene.run(CustomizeSceneName); this.scene.run(CustomizeSceneName);
} }
this.scene.remove(SelectCharacterSceneName);
} }
createCurrentPlayer(): void { createCurrentPlayer(): void {

View File

@ -1,5 +1,5 @@
import {LoginSceneName} from "../Login/LoginScene"; import {LoginScene, LoginSceneName} from "../Login/LoginScene";
import {SelectCharacterSceneName} from "../Login/SelectCharacterScene"; import {SelectCharacterScene, SelectCharacterSceneName} from "../Login/SelectCharacterScene";
import {gameManager} from "../Game/GameManager"; import {gameManager} from "../Game/GameManager";
import {localUserStore} from "../../Connexion/LocalUserStore"; import {localUserStore} from "../../Connexion/LocalUserStore";
import {mediaManager} from "../../WebRtc/MediaManager"; import {mediaManager} from "../../WebRtc/MediaManager";
@ -40,22 +40,22 @@ export class MenuScene extends Phaser.Scene {
create() { create() {
this.menuElement = this.add.dom(closedSideMenuX, 30).createFromCache(gameMenuKey); this.menuElement = this.add.dom(closedSideMenuX, 30).createFromCache(gameMenuKey);
this.menuElement.setOrigin(0); this.menuElement.setOrigin(0);
this.revealAfterInit(this.menuElement, 'gameMenu'); this.revealMenusAfterInit(this.menuElement, 'gameMenu');
this.gameQualityMenuElement = this.add.dom(300, -400).createFromCache(gameSettingsMenuKey); this.gameQualityMenuElement = this.add.dom(300, -400).createFromCache(gameSettingsMenuKey);
this.revealAfterInit(this.gameQualityMenuElement, 'gameQuality'); this.revealMenusAfterInit(this.gameQualityMenuElement, 'gameQuality');
this.input.keyboard.on('keyup-TAB', () => { this.input.keyboard.on('keyup-TAB', () => {
this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu(); this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu();
}); });
this.menuButton = this.add.dom(35, 20).createFromCache(gameMenuIconKey); this.menuButton = this.add.dom(0, 0).createFromCache(gameMenuIconKey);
this.menuButton.addListener('click'); this.menuButton.addListener('click');
this.menuButton.on('click', () => { this.menuButton.on('click', () => {
this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu(); this.sideMenuOpened ? this.closeSideMenu() : this.openSideMenu();
}); });
} }
private revealAfterInit(menuElement: Phaser.GameObjects.DOMElement, rootDomId: string) { private revealMenusAfterInit(menuElement: Phaser.GameObjects.DOMElement, rootDomId: string) {
//Dom elements will appear inside the viewer screen when creating before being moved out of it, which create a flicker effect. //Dom elements will appear inside the viewer screen when creating before being moved out of it, which create a flicker effect.
//To prevent this, we put a 'hidden' attribute on the root element, we remove it only after the init is done. //To prevent this, we put a 'hidden' attribute on the root element, we remove it only after the init is done.
setTimeout(() => { setTimeout(() => {
@ -63,6 +63,10 @@ export class MenuScene extends Phaser.Scene {
}, 250); }, 250);
} }
public revealMenuIcon(): void {
(this.menuButton.getChildByID('menuIcon') as HTMLElement).hidden = false
}
openSideMenu() { openSideMenu() {
if (this.sideMenuOpened) return; if (this.sideMenuOpened) return;
this.sideMenuOpened = true; this.sideMenuOpened = true;
@ -148,14 +152,14 @@ export class MenuScene extends Phaser.Scene {
case 'changeNameButton': case 'changeNameButton':
this.closeSideMenu(); this.closeSideMenu();
this.closeGameQualityMenu(); this.closeGameQualityMenu();
gameManager.leaveGame(this, LoginSceneName); gameManager.leaveGame(this, LoginSceneName, new LoginScene());
break; break;
case 'sparkButton': case 'sparkButton':
this.goToSpark(); this.goToSpark();
break; break;
case 'changeSkinButton': case 'changeSkinButton':
this.closeSideMenu(); this.closeSideMenu();
gameManager.leaveGame(this, SelectCharacterSceneName); gameManager.leaveGame(this, SelectCharacterSceneName, new SelectCharacterScene());
break; break;
case 'closeButton': case 'closeButton':
this.closeSideMenu(); this.closeSideMenu();