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 ListMessageUserPositionInterface
} from "../../Connexion"; } from "../../Connexion";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer"; 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 { export enum StatusGameManagerEnum {
IN_PROGRESS = 1, IN_PROGRESS = 1,
@ -124,6 +128,18 @@ export class GameManager {
pushPlayerPosition(event: HasMovedEvent) { pushPlayerPosition(event: HasMovedEvent) {
this.ConnexionInstance.sharePosition(event.x, event.y, event.character, this.currentGameScene.scene.key, event.direction); 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(); 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){ private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
let exitSceneUrl = this.getExitSceneUrl(layer); 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 gameIndex = this.scene.getIndex(exitSceneKey);
let game : Phaser.Scene = null; let game : Phaser.Scene = null;
@ -187,7 +188,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
} }
if(!game){ if(!game){
return; return;
} }*/
let tiles : any = layer.data; let tiles : any = layer.data;
tiles.forEach((objectKey : number, key: number) => { tiles.forEach((objectKey : number, key: number) => {
if(objectKey === 0){ if(objectKey === 0){

View File

@ -103,9 +103,7 @@ export class LogincScene extends Phaser.Scene implements GameSceneInterface {
if (!scene) { if (!scene) {
return; return;
} }
let key = getMapKeyByUrl(scene.mapUrlStart); let key = gameManager.loadMap(scene.mapUrlStart, this.scene);
let game = new GameScene(key,`${MAP_FILE_URL}${scene.mapUrlStart}`);
this.scene.add(key, game, false);
this.scene.start(key); this.scene.start(key);
return scene; return scene;
}).catch((err) => { }).catch((err) => {