GameManager has an attribute scenePlugin

This commit is contained in:
GRL 2021-08-16 11:43:29 +02:00
parent 8d57886bae
commit d2413896c7
14 changed files with 109 additions and 125 deletions

View file

@ -18,6 +18,7 @@ export class GameManager {
private characterLayers: string[] | null;
private companion: string | null;
private startRoom!: Room;
private scenePlugin!: Phaser.Scenes.ScenePlugin;
currentGameSceneName: string | null = null;
constructor() {
@ -27,8 +28,9 @@ export class GameManager {
}
public async init(scenePlugin: Phaser.Scenes.ScenePlugin): Promise<string> {
this.scenePlugin = scenePlugin;
this.startRoom = await connectionManager.initGameConnexion();
this.loadMap(this.startRoom, scenePlugin);
this.loadMap(this.startRoom);
if (!this.playerName) {
return LoginSceneName;
@ -68,20 +70,20 @@ export class GameManager {
return this.companion;
}
public loadMap(room: Room, scenePlugin: Phaser.Scenes.ScenePlugin) {
public loadMap(room: Room) {
const roomID = room.key;
const gameIndex = scenePlugin.getIndex(roomID);
const gameIndex = this.scenePlugin.getIndex(roomID);
if (gameIndex === -1) {
const game: Phaser.Scene = new GameScene(room, room.mapUrl);
scenePlugin.add(roomID, game, false);
this.scenePlugin.add(roomID, game, false);
}
}
public goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin): void {
public goToStartingMap(): void {
console.log("starting " + (this.currentGameSceneName || this.startRoom.key));
scenePlugin.start(this.currentGameSceneName || this.startRoom.key);
scenePlugin.launch(MenuSceneName);
this.scenePlugin.start(this.currentGameSceneName || this.startRoom.key);
this.scenePlugin.launch(MenuSceneName);
if (
!localUserStore.getHelpCameraSettingsShown() &&
@ -102,33 +104,33 @@ export class GameManager {
* 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.
*/
leaveGame(scene: Phaser.Scene, targetSceneName: string, sceneClass: Phaser.Scene): void {
leaveGame(targetSceneName: string, sceneClass: Phaser.Scene): void {
if (this.currentGameSceneName === null) throw "No current scene id set!";
const gameScene: GameScene = scene.scene.get(this.currentGameSceneName) as GameScene;
const gameScene: GameScene = this.scenePlugin.get(this.currentGameSceneName) as GameScene;
gameScene.cleanupClosingScene();
scene.scene.stop(this.currentGameSceneName);
scene.scene.sleep(MenuSceneName);
if (!scene.scene.get(targetSceneName)) {
scene.scene.add(targetSceneName, sceneClass, false);
this.scenePlugin.stop(this.currentGameSceneName);
this.scenePlugin.sleep(MenuSceneName);
if (!this.scenePlugin.get(targetSceneName)) {
this.scenePlugin.add(targetSceneName, sceneClass, false);
}
scene.scene.run(targetSceneName);
this.scenePlugin.run(targetSceneName);
}
/**
* follow up to leaveGame()
*/
tryResumingGame(scene: Phaser.Scene, fallbackSceneName: string) {
tryResumingGame(fallbackSceneName: string) {
if (this.currentGameSceneName) {
scene.scene.start(this.currentGameSceneName);
scene.scene.wake(MenuSceneName);
this.scenePlugin.start(this.currentGameSceneName);
this.scenePlugin.wake(MenuSceneName);
} else {
scene.scene.run(fallbackSceneName);
this.scenePlugin.run(fallbackSceneName);
}
}
public getCurrentGameScene(scene: Phaser.Scene): GameScene {
public getCurrentGameScene(): GameScene {
if (this.currentGameSceneName === null) throw "No current scene id set!";
return scene.scene.get(this.currentGameSceneName) as GameScene;
return this.scenePlugin.get(this.currentGameSceneName) as GameScene;
}
}

View file

@ -1443,7 +1443,7 @@ ${escapedMessage}
private async loadNextGame(exitRoomPath: URL): Promise<void> {
try {
const room = await Room.createRoom(exitRoomPath);
return gameManager.loadMap(room, this.scene);
return gameManager.loadMap(room);
} catch (e /*: unknown*/) {
console.warn('Error while pre-loading exit room "' + exitRoomPath.toString() + '"', e);
}
@ -1504,7 +1504,7 @@ ${escapedMessage}
});
} catch (err) {
if (err instanceof TextureError) {
gameManager.leaveGame(this, SelectCharacterSceneName, new SelectCharacterScene());
gameManager.leaveGame(SelectCharacterSceneName, new SelectCharacterScene());
}
throw err;
}