2021-06-25 18:14:40 +02:00
|
|
|
import { GameScene } from "./GameScene";
|
|
|
|
import { connectionManager } from "../../Connexion/ConnectionManager";
|
|
|
|
import type { Room } from "../../Connexion/Room";
|
|
|
|
import { LoginSceneName } from "../Login/LoginScene";
|
|
|
|
import { SelectCharacterSceneName } from "../Login/SelectCharacterScene";
|
|
|
|
import { EnableCameraSceneName } from "../Login/EnableCameraScene";
|
|
|
|
import { localUserStore } from "../../Connexion/LocalUserStore";
|
|
|
|
import { get } from "svelte/store";
|
|
|
|
import { requestedCameraState, requestedMicrophoneState } from "../../Stores/MediaStore";
|
|
|
|
import { helpCameraSettingsVisibleStore } from "../../Stores/HelpCameraSettingsStore";
|
2021-08-26 12:01:07 +02:00
|
|
|
import { menuIconVisiblilityStore } from "../../Stores/MenuStore";
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
/**
|
|
|
|
* This class should be responsible for any scene starting/stopping
|
|
|
|
*/
|
2020-04-27 15:03:05 +02:00
|
|
|
export class GameManager {
|
2021-06-25 18:14:40 +02:00
|
|
|
private playerName: string | null;
|
|
|
|
private characterLayers: string[] | null;
|
|
|
|
private companion: string | null;
|
|
|
|
private startRoom!: Room;
|
2021-09-04 21:31:36 +02:00
|
|
|
private cameraSetup?: { video: unknown; audio: unknown };
|
2021-06-25 18:14:40 +02:00
|
|
|
currentGameSceneName: string | null = null;
|
2021-08-26 12:01:07 +02:00
|
|
|
// Note: this scenePlugin is the scenePlugin of the EntryScene. We should always provide a key in methods called on this scenePlugin.
|
|
|
|
private scenePlugin!: Phaser.Scenes.ScenePlugin;
|
2021-05-12 09:13:25 +02:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
constructor() {
|
|
|
|
this.playerName = localUserStore.getName();
|
|
|
|
this.characterLayers = localUserStore.getCharacterLayers();
|
2021-04-02 21:21:11 +02:00
|
|
|
this.companion = localUserStore.getCompanion();
|
2021-09-04 21:31:36 +02:00
|
|
|
this.cameraSetup = localUserStore.getCameraSetup();
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
public async init(scenePlugin: Phaser.Scenes.ScenePlugin): Promise<string> {
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin = scenePlugin;
|
2020-10-12 18:59:49 +02:00
|
|
|
this.startRoom = await connectionManager.initGameConnexion();
|
2021-08-16 11:43:29 +02:00
|
|
|
this.loadMap(this.startRoom);
|
2021-04-14 17:47:26 +02:00
|
|
|
|
2021-09-05 18:17:49 +02:00
|
|
|
//If player name was not set show login scene with player name
|
|
|
|
//If Room si not public and Auth was not set, show login scene to authenticate user (OpenID - SSO - Anonymous)
|
|
|
|
if (!this.playerName || (this.startRoom.authenticationMandatory && !localUserStore.getAuthToken())) {
|
2020-12-04 11:30:35 +01:00
|
|
|
return LoginSceneName;
|
2021-04-07 14:09:45 +02:00
|
|
|
} else if (!this.characterLayers || !this.characterLayers.length) {
|
2020-12-04 11:30:35 +01:00
|
|
|
return SelectCharacterSceneName;
|
2021-09-04 21:31:36 +02:00
|
|
|
} else if (this.cameraSetup == undefined) {
|
2020-12-04 11:30:35 +01:00
|
|
|
return EnableCameraSceneName;
|
2021-09-04 21:31:36 +02:00
|
|
|
} else {
|
|
|
|
this.activeMenuSceneAndHelpCameraSettings();
|
|
|
|
return this.startRoom.key;
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-07-28 15:53:44 +02:00
|
|
|
public setPlayerName(name: string): void {
|
2020-05-03 15:29:40 +02:00
|
|
|
this.playerName = name;
|
2020-12-04 11:30:35 +01:00
|
|
|
localUserStore.setName(name);
|
2020-07-28 15:53:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:57 +02:00
|
|
|
public setCharacterLayers(layers: string[]): void {
|
2020-07-28 15:53:44 +02:00
|
|
|
this.characterLayers = layers;
|
2020-12-04 11:30:35 +01:00
|
|
|
localUserStore.setCharacterLayers(layers);
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
getPlayerName(): string | null {
|
2020-05-03 15:29:40 +02:00
|
|
|
return this.playerName;
|
|
|
|
}
|
2020-05-03 15:51:16 +02:00
|
|
|
|
2021-01-07 17:11:22 +01:00
|
|
|
getCharacterLayers(): string[] {
|
|
|
|
if (!this.characterLayers) {
|
2021-06-25 18:14:40 +02:00
|
|
|
throw "characterLayers are not set";
|
2021-01-07 17:11:22 +01:00
|
|
|
}
|
2020-07-28 17:43:33 +02:00
|
|
|
return this.characterLayers;
|
2020-05-06 01:50:01 +02:00
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
setCompanion(companion: string | null): void {
|
2021-04-02 23:00:51 +02:00
|
|
|
this.companion = companion;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
getCompanion(): string | null {
|
2021-04-02 21:21:11 +02:00
|
|
|
return this.companion;
|
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public loadMap(room: Room) {
|
2021-07-13 19:09:07 +02:00
|
|
|
const roomID = room.key;
|
2020-10-13 17:08:24 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
const gameIndex = this.scenePlugin.getIndex(roomID);
|
2021-06-25 18:14:40 +02:00
|
|
|
if (gameIndex === -1) {
|
2021-07-13 19:09:07 +02:00
|
|
|
const game: Phaser.Scene = new GameScene(room, room.mapUrl);
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin.add(roomID, game, false);
|
2020-05-11 23:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:51:24 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public goToStartingMap(): void {
|
2021-07-13 19:09:07 +02:00
|
|
|
console.log("starting " + (this.currentGameSceneName || this.startRoom.key));
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin.start(this.currentGameSceneName || this.startRoom.key);
|
2021-09-04 21:31:36 +02:00
|
|
|
this.activeMenuSceneAndHelpCameraSettings();
|
|
|
|
}
|
2021-06-01 09:08:02 +02:00
|
|
|
|
2021-09-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private activeMenuSceneAndHelpCameraSettings(): void {
|
2021-06-25 18:14:40 +02:00
|
|
|
if (
|
|
|
|
!localUserStore.getHelpCameraSettingsShown() &&
|
|
|
|
(!get(requestedMicrophoneState) || !get(requestedCameraState))
|
|
|
|
) {
|
2021-06-01 09:08:02 +02:00
|
|
|
helpCameraSettingsVisibleStore.set(true);
|
|
|
|
localUserStore.setHelpCameraSettingsShown();
|
|
|
|
}
|
2020-12-16 15:09:58 +01:00
|
|
|
}
|
2021-05-12 09:13:25 +02:00
|
|
|
|
2020-12-16 15:09:58 +01:00
|
|
|
public gameSceneIsCreated(scene: GameScene) {
|
|
|
|
this.currentGameSceneName = scene.scene.key;
|
2021-08-26 12:01:07 +02:00
|
|
|
menuIconVisiblilityStore.set(true);
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-08-16 11:43:29 +02:00
|
|
|
leaveGame(targetSceneName: string, sceneClass: Phaser.Scene): void {
|
2021-06-25 18:14:40 +02:00
|
|
|
if (this.currentGameSceneName === null) throw "No current scene id set!";
|
2021-08-16 11:43:29 +02:00
|
|
|
const gameScene: GameScene = this.scenePlugin.get(this.currentGameSceneName) as GameScene;
|
2020-12-04 11:30:35 +01:00
|
|
|
gameScene.cleanupClosingScene();
|
2021-08-20 09:49:37 +02:00
|
|
|
gameScene.createSuccessorGameScene(false, false);
|
2021-08-26 12:01:07 +02:00
|
|
|
menuIconVisiblilityStore.set(false);
|
2021-08-16 11:43:29 +02:00
|
|
|
if (!this.scenePlugin.get(targetSceneName)) {
|
|
|
|
this.scenePlugin.add(targetSceneName, sceneClass, false);
|
2020-12-16 15:09:58 +01:00
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin.run(targetSceneName);
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2020-12-15 18:00:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* follow up to leaveGame()
|
|
|
|
*/
|
2021-08-16 11:43:29 +02:00
|
|
|
tryResumingGame(fallbackSceneName: string) {
|
2020-12-15 18:00:04 +01:00
|
|
|
if (this.currentGameSceneName) {
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin.start(this.currentGameSceneName);
|
2021-08-26 12:01:07 +02:00
|
|
|
menuIconVisiblilityStore.set(true);
|
2020-12-15 18:00:04 +01:00
|
|
|
} else {
|
2021-08-16 11:43:29 +02:00
|
|
|
this.scenePlugin.run(fallbackSceneName);
|
2020-12-15 18:00:04 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 09:13:25 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public getCurrentGameScene(): GameScene {
|
2021-06-25 18:14:40 +02:00
|
|
|
if (this.currentGameSceneName === null) throw "No current scene id set!";
|
2021-08-16 11:43:29 +02:00
|
|
|
return this.scenePlugin.get(this.currentGameSceneName) as GameScene;
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2021-09-05 18:17:49 +02:00
|
|
|
|
|
|
|
public get currentStartedRoom() {
|
|
|
|
return this.startRoom;
|
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|