From 0731bd39e500b181f527cceaae2f6ddaf0a295a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 12 Oct 2020 18:59:49 +0200 Subject: [PATCH] Moving back to using ScenePlugin and adding EntryScene --- front/src/Phaser/Game/GameManager.ts | 26 ++++++-------- front/src/Phaser/Game/GameScene.ts | 10 +++--- front/src/Phaser/Login/EnableCameraScene.ts | 2 +- front/src/Phaser/Login/EntryScene.ts | 40 +++++++++++++++++++++ front/src/Url/UrlManager.ts | 14 ++++---- front/src/index.ts | 5 ++- 6 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 front/src/Phaser/Login/EntryScene.ts diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index bed098ae..22123d1c 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -14,18 +14,10 @@ export class GameManager { private playerName!: string; private characterLayers!: string[]; private startRoom!:Room; - private sceneManager!: Phaser.Scenes.SceneManager; - public async init(sceneManager: Phaser.Scenes.SceneManager) { - this.sceneManager = sceneManager; - try { - this.startRoom = await connectionManager.initGameConnexion(); - } catch (e) { - this.sceneManager.start(FourOFourSceneName, { - url: window.location.pathname.toString() - }); - } - this.loadMap(this.startRoom.url, this.startRoom.ID); + public async init(scenePlugin: Phaser.Scenes.ScenePlugin) { + this.startRoom = await connectionManager.initGameConnexion(); + this.loadMap(this.startRoom.url, this.startRoom.ID, scenePlugin); } public setPlayerName(name: string): void { @@ -49,12 +41,13 @@ export class GameManager { } - public loadMap(mapUrl: string, roomID: string): void { + public loadMap(mapUrl: string, roomID: string, scenePlugin: Phaser.Scenes.ScenePlugin): void { console.log('Loading map '+roomID+' at url '+mapUrl); - const gameIndex = this.sceneManager.getIndex(roomID); + const gameIndex = scenePlugin.getIndex(mapUrl); if(gameIndex === -1){ const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, roomID); - this.sceneManager.add(roomID, game, false); + console.log('Adding scene '+mapUrl); + scenePlugin.add(mapUrl, game, false); } } @@ -65,8 +58,9 @@ export class GameManager { return mapUrlStart.substring(startPos, endPos); } - public async goToStartingMap() { - this.sceneManager.start(this.startRoom.ID, {startLayerName: 'global'}); + public async goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin) { + console.log('Starting scene '+this.startRoom.url); + scenePlugin.start(this.startRoom.url, {startLayerName: 'global'}); } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 602f697e..a22d973a 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -139,11 +139,11 @@ export class GameScene extends ResizableScene implements CenterListener { private userInputManager!: UserInputManager; static createFromUrl(mapUrlFile: string, instance: string, gameSceneKey: string|null = null): GameScene { - const mapKey = gameManager.getMapKeyByUrl(mapUrlFile); + // We use the map URL as a key if (gameSceneKey === null) { - gameSceneKey = mapKey; + gameSceneKey = mapUrlFile; } - return new GameScene(mapKey, mapUrlFile, instance, gameSceneKey); + return new GameScene(mapUrlFile, mapUrlFile, instance, gameSceneKey); } constructor(MapKey : string, MapUrlFile: string, instance: string, gameSceneKey: string) { @@ -418,7 +418,7 @@ export class GameScene extends ResizableScene implements CenterListener { context.strokeStyle = '#ffffff'; context.stroke(); this.circleTexture.refresh(); - + // Let's pause the scene if the connection is not established yet if (this.connection === undefined) { // Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking @@ -691,7 +691,7 @@ export class GameScene extends ResizableScene implements CenterListener { // TODO: eventually compute a relative URL const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href; - gameManager.loadMap(absoluteExitSceneUrl, instance); + gameManager.loadMap(absoluteExitSceneUrl, instance, this.scene); const exitSceneKey = instance; const tiles : number[] = layer.data as number[]; diff --git a/front/src/Phaser/Login/EnableCameraScene.ts b/front/src/Phaser/Login/EnableCameraScene.ts index 8695464b..3916587a 100644 --- a/front/src/Phaser/Login/EnableCameraScene.ts +++ b/front/src/Phaser/Login/EnableCameraScene.ts @@ -266,7 +266,7 @@ export class EnableCameraScene extends Phaser.Scene { mediaManager.stopCamera(); mediaManager.stopMicrophone(); - gameManager.goToStartingMap(); + gameManager.goToStartingMap(this.scene); } private async getDevices() { diff --git a/front/src/Phaser/Login/EntryScene.ts b/front/src/Phaser/Login/EntryScene.ts new file mode 100644 index 00000000..fec4e880 --- /dev/null +++ b/front/src/Phaser/Login/EntryScene.ts @@ -0,0 +1,40 @@ +import {gameManager} from "../Game/GameManager"; +import {TextField} from "../Components/TextField"; +import {TextInput} from "../Components/TextInput"; +import {ClickButton} from "../Components/ClickButton"; +import Image = Phaser.GameObjects.Image; +import Rectangle = Phaser.GameObjects.Rectangle; +import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Character"; +import {cypressAsserter} from "../../Cypress/CypressAsserter"; +import {SelectCharacterSceneName} from "./SelectCharacterScene"; +import {ResizableScene} from "./ResizableScene"; +import {Scene} from "phaser"; +import {LoginSceneName} from "./LoginScene"; +import {FourOFourSceneName} from "../Reconnecting/FourOFourScene"; + +export const EntrySceneName = "EntryScene"; + +/** + * The EntryScene is not a real scene. It is the first scene loaded and is only used to initialize the gameManager + * and to route to the next correct scene. + */ +export class EntryScene extends Scene { + constructor() { + super({ + key: EntrySceneName + }); + } + + preload() { + } + + create() { + gameManager.init(this.scene).then(() => { + this.scene.start(LoginSceneName); + }).catch(() => { + this.scene.start(FourOFourSceneName, { + url: window.location.pathname.toString() + }); + }); + } +} diff --git a/front/src/Url/UrlManager.ts b/front/src/Url/UrlManager.ts index 876e258e..ae8725bc 100644 --- a/front/src/Url/UrlManager.ts +++ b/front/src/Url/UrlManager.ts @@ -8,7 +8,7 @@ export enum GameConnexionTypes { //this class is responsible with analysing and editing the game's url class UrlManager { - + //todo: use that to detect if we can find a token in localstorage public getGameConnexionType(): GameConnexionTypes { const url = window.location.pathname.toString(); @@ -22,14 +22,14 @@ class UrlManager { return GameConnexionTypes.unknown } } - + public getAnonymousMapUrlStart():string { const match = /\/_\/global\/(.+)/.exec(window.location.pathname.toString()) if (!match) throw new Error('Could not extract startmap url from'+window.location.pathname); - return match[1]; - + return window.location.protocol+'//'+match[1]; + } - + public getOrganizationToken(): string|null { const match = /\/register\/(.+)/.exec(window.location.pathname.toString()); return match ? match [1] : null; @@ -46,7 +46,7 @@ class UrlManager { history.pushState({}, 'WorkAdventure', newUrl); return newUrl; } - + } -export const urlManager = new UrlManager(); \ No newline at end of file +export const urlManager = new UrlManager(); diff --git a/front/src/index.ts b/front/src/index.ts index f57474d7..e12d8707 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -13,6 +13,7 @@ import {CustomizeScene} from "./Phaser/Login/CustomizeScene"; import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager"; import {gameManager} from "./Phaser/Game/GameManager"; import {ResizableScene} from "./Phaser/Login/ResizableScene"; +import {EntryScene} from "./Phaser/Login/EntryScene"; //CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com'); @@ -30,7 +31,7 @@ const config: GameConfig = { width: width / RESOLUTION, height: height / RESOLUTION, parent: "game", - scene: [LoginScene, SelectCharacterScene, EnableCameraScene, ReconnectingScene, FourOFourScene, CustomizeScene], + scene: [EntryScene, LoginScene, SelectCharacterScene, EnableCameraScene, ReconnectingScene, FourOFourScene, CustomizeScene], zoom: RESOLUTION, physics: { default: "arcade", @@ -51,8 +52,6 @@ cypressAsserter.gameStarted(); const game = new Phaser.Game(config); -gameManager.init(game.scene); - window.addEventListener('resize', function (event) { const {width, height} = CoWebsiteManager.getGameSize();