Refactoring/centralizing map loading in GameManager

This commit is contained in:
David Négrier 2020-05-11 23:26:40 +02:00
parent c846aff456
commit 9417e4a4d2
3 changed files with 20 additions and 5 deletions

View 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();

View 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){

View 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) => {