From 9417e4a4d231074557e25899880af69521e26ea4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20N=C3=A9grier?= Date: Mon, 11 May 2020 23:26:40 +0200 Subject: [PATCH] Refactoring/centralizing map loading in GameManager --- front/src/Phaser/Game/GameManager.ts | 16 ++++++++++++++++ front/src/Phaser/Game/GameScene.ts | 5 +++-- front/src/Phaser/Login/LogincScene.ts | 4 +--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 78019f5..428e5fb 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -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(); diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 9120378..bc6b746 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -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){ diff --git a/front/src/Phaser/Login/LogincScene.ts b/front/src/Phaser/Login/LogincScene.ts index 4948737..6eac780 100644 --- a/front/src/Phaser/Login/LogincScene.ts +++ b/front/src/Phaser/Login/LogincScene.ts @@ -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) => { -- 2.25.1