2020-10-12 16:23:07 +02:00
|
|
|
import {GameScene} from "./GameScene";
|
2020-09-28 15:02:37 +02:00
|
|
|
import {connectionManager} from "../../Connexion/ConnectionManager";
|
2020-10-12 16:23:07 +02:00
|
|
|
import {Room} from "../../Connexion/Room";
|
2020-12-16 15:09:58 +01:00
|
|
|
import {MenuScene, MenuSceneName} from "../Menu/MenuScene";
|
2021-03-19 15:40:07 +01:00
|
|
|
import {HelpCameraSettingsScene, HelpCameraSettingsSceneName} from "../Menu/HelpCameraSettingsScene";
|
2020-12-04 11:30:35 +01:00
|
|
|
import {LoginSceneName} from "../Login/LoginScene";
|
|
|
|
import {SelectCharacterSceneName} from "../Login/SelectCharacterScene";
|
|
|
|
import {EnableCameraSceneName} from "../Login/EnableCameraScene";
|
|
|
|
import {localUserStore} from "../../Connexion/LocalUserStore";
|
2020-04-10 12:54:05 +02:00
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
export interface HasMovedEvent {
|
|
|
|
direction: string;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving: boolean;
|
2020-05-02 16:54:52 +02:00
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}
|
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 {
|
2020-12-04 11:30:35 +01:00
|
|
|
private playerName: string|null;
|
|
|
|
private characterLayers: string[]|null;
|
2021-04-02 21:21:11 +02:00
|
|
|
private companion: string|null;
|
2020-10-12 16:23:07 +02:00
|
|
|
private startRoom!:Room;
|
2020-12-15 18:00:04 +01:00
|
|
|
currentGameSceneName: string|null = null;
|
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();
|
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> {
|
2020-10-12 18:59:49 +02:00
|
|
|
this.startRoom = await connectionManager.initGameConnexion();
|
2020-10-13 17:08:24 +02:00
|
|
|
await this.loadMap(this.startRoom, scenePlugin);
|
2020-12-04 11:30:35 +01:00
|
|
|
|
|
|
|
if (!this.playerName) {
|
|
|
|
return LoginSceneName;
|
|
|
|
} else if (!this.characterLayers) {
|
|
|
|
return SelectCharacterSceneName;
|
|
|
|
} else {
|
|
|
|
return EnableCameraSceneName;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-12-04 11:30:35 +01: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) {
|
|
|
|
throw 'characterLayers are not set';
|
|
|
|
}
|
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-04-02 23:00:51 +02:00
|
|
|
|
|
|
|
setCompanion(companion: string|null): void {
|
|
|
|
this.companion = companion;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:21:11 +02:00
|
|
|
getCompanion(): string|null {
|
|
|
|
return this.companion;
|
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2020-10-13 17:08:24 +02:00
|
|
|
public async loadMap(room: Room, scenePlugin: Phaser.Scenes.ScenePlugin): Promise<void> {
|
|
|
|
const roomID = room.id;
|
|
|
|
const mapUrl = await room.getMapUrl();
|
|
|
|
|
2020-11-18 18:15:57 +01:00
|
|
|
const gameIndex = scenePlugin.getIndex(roomID);
|
2020-05-11 23:26:40 +02:00
|
|
|
if(gameIndex === -1){
|
2020-11-19 14:32:18 +01:00
|
|
|
const game : Phaser.Scene = new GameScene(room, mapUrl);
|
2020-11-18 18:15:57 +01:00
|
|
|
scenePlugin.add(roomID, game, false);
|
2020-05-11 23:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:51:24 +02:00
|
|
|
|
2020-11-18 18:15:57 +01:00
|
|
|
public goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin): void {
|
2020-12-15 18:00:04 +01:00
|
|
|
console.log('starting '+ (this.currentGameSceneName || this.startRoom.id))
|
|
|
|
scenePlugin.start(this.currentGameSceneName || this.startRoom.id);
|
2020-12-16 15:09:58 +01:00
|
|
|
scenePlugin.launch(MenuSceneName);
|
2021-03-24 15:59:18 +01:00
|
|
|
|
|
|
|
if (!localUserStore.getHelpCameraSettingsShown()) {
|
|
|
|
scenePlugin.launch(HelpCameraSettingsSceneName);//700
|
|
|
|
}
|
2020-12-16 15:09:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public gameSceneIsCreated(scene: GameScene) {
|
|
|
|
this.currentGameSceneName = scene.scene.key;
|
|
|
|
const menuScene: MenuScene = scene.scene.get(MenuSceneName) as MenuScene;
|
|
|
|
menuScene.revealMenuIcon();
|
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.
|
|
|
|
*/
|
2020-12-16 15:09:58 +01:00
|
|
|
leaveGame(scene: Phaser.Scene, targetSceneName: string, sceneClass: Phaser.Scene): void {
|
2020-12-15 18:00:04 +01:00
|
|
|
if (this.currentGameSceneName === null) throw 'No current scene id set!';
|
|
|
|
const gameScene: GameScene = scene.scene.get(this.currentGameSceneName) as GameScene;
|
2020-12-04 11:30:35 +01:00
|
|
|
gameScene.cleanupClosingScene();
|
2020-12-15 18:00:04 +01:00
|
|
|
scene.scene.stop(this.currentGameSceneName);
|
2020-12-16 15:09:58 +01:00
|
|
|
scene.scene.sleep(MenuSceneName);
|
|
|
|
if (!scene.scene.get(targetSceneName)) {
|
|
|
|
scene.scene.add(targetSceneName, sceneClass, false);
|
|
|
|
}
|
2020-12-04 11:30:35 +01:00
|
|
|
scene.scene.run(targetSceneName);
|
|
|
|
}
|
2020-12-15 18:00:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* follow up to leaveGame()
|
|
|
|
*/
|
|
|
|
tryResumingGame(scene: Phaser.Scene, fallbackSceneName: string) {
|
|
|
|
if (this.currentGameSceneName) {
|
|
|
|
scene.scene.start(this.currentGameSceneName);
|
2020-12-16 15:09:58 +01:00
|
|
|
scene.scene.wake(MenuSceneName);
|
2020-12-15 18:00:04 +01:00
|
|
|
} else {
|
|
|
|
scene.scene.run(fallbackSceneName)
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 11:30:35 +01:00
|
|
|
|
|
|
|
public getCurrentGameScene(scene: Phaser.Scene): GameScene {
|
2020-12-15 18:00:04 +01:00
|
|
|
if (this.currentGameSceneName === null) throw 'No current scene id set!';
|
|
|
|
return scene.scene.get(this.currentGameSceneName) as GameScene
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|