Moving back to using ScenePlugin and adding EntryScene

This commit is contained in:
David Négrier 2020-10-12 18:59:49 +02:00
parent 2852f204f5
commit 0731bd39e5
6 changed files with 65 additions and 32 deletions

View file

@ -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'});
}
}

View file

@ -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[];

View file

@ -266,7 +266,7 @@ export class EnableCameraScene extends Phaser.Scene {
mediaManager.stopCamera();
mediaManager.stopMicrophone();
gameManager.goToStartingMap();
gameManager.goToStartingMap(this.scene);
}
private async getDevices() {

View file

@ -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()
});
});
}
}