Refactoring/centralizing map loading in GameManager
authorDavid Négrier <d.negrier@thecodingmachine.com>
Mon, 11 May 2020 21:26:40 +0000 (23:26 +0200)
committerDavid Négrier <d.negrier@thecodingmachine.com>
Tue, 12 May 2020 20:38:44 +0000 (22:38 +0200)
front/src/Phaser/Game/GameManager.ts
front/src/Phaser/Game/GameScene.ts
front/src/Phaser/Login/LogincScene.ts

index 78019f5d488b8e489f76672d9b7ca33e02d4ff68..428e5fb0c9e5b5cc6d0c9bda8d1acf521580fd6f 100644 (file)
@@ -5,6 +5,10 @@ import {
     ListMessageUserPositionInterface
 } from "../../Connexion";
 import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
+import {MAP_FILE_URL} from "../../Enum/EnvironmentVariable";
+import {getMapKeyByUrl} from "../Login/LogincScene";
+import SceneManager = Phaser.Scenes.SceneManager;
+import ScenePlugin = Phaser.Scenes.ScenePlugin;
 
 export enum StatusGameManagerEnum {
     IN_PROGRESS = 1,
@@ -124,6 +128,18 @@ export class GameManager {
     pushPlayerPosition(event: HasMovedEvent) {
         this.ConnexionInstance.sharePosition(event.x, event.y, event.character, this.currentGameScene.scene.key, event.direction);
     }
+
+    loadMap(mapUrl: string, scene: ScenePlugin): string {
+        let sceneKey = getMapKeyByUrl(mapUrl);
+
+        let gameIndex = scene.getIndex(sceneKey);
+        let game : Phaser.Scene = null;
+        if(gameIndex === -1){
+            game = new GameScene(sceneKey, `${MAP_FILE_URL}${mapUrl}`);
+            scene.add(sceneKey, game, false);
+        }
+        return sceneKey;
+    }
 }
 
 export const gameManager = new GameManager();
index 9120378dba89887494954b85b80f1e557cb13011..bc6b746e85e85277fe5eac7660b74eedd4cc9983 100644 (file)
@@ -175,7 +175,8 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
     private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
         let exitSceneUrl = this.getExitSceneUrl(layer);
 
-        let exitSceneKey = getMapKeyByUrl(exitSceneUrl);
+        let exitSceneKey = gameManager.loadMap(exitSceneUrl, this.scene);
+        /*let exitSceneKey = getMapKeyByUrl(exitSceneUrl);
 
         let gameIndex = this.scene.getIndex(exitSceneKey);
         let game : Phaser.Scene = null;
@@ -187,7 +188,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
         }
         if(!game){
             return;
-        }
+        }*/
         let tiles : any = layer.data;
         tiles.forEach((objectKey : number, key: number) => {
             if(objectKey === 0){
index 49487376308cfcd3efa401849cda23b8d0f775d9..6eac78055780570ce05bdbfddb72868c0ac0d135 100644 (file)
@@ -103,9 +103,7 @@ export class LogincScene extends Phaser.Scene implements GameSceneInterface {
                 if (!scene) {
                     return;
                 }
-                let key = getMapKeyByUrl(scene.mapUrlStart);
-                let game = new GameScene(key,`${MAP_FILE_URL}${scene.mapUrlStart}`);
-                this.scene.add(key, game, false);
+                let key = gameManager.loadMap(scene.mapUrlStart, this.scene);
                 this.scene.start(key);
                 return scene;
             }).catch((err) => {