From 8d8b879ed626a20710b4ec1b5bfe0e9573ac104b Mon Sep 17 00:00:00 2001 From: kharhamel Date: Thu, 19 Nov 2020 14:32:18 +0100 Subject: [PATCH] FIX: the game now uses the url hash to choose the start layer --- front/src/Connexion/Room.ts | 4 ---- front/src/Phaser/Game/GameManager.ts | 2 +- front/src/Phaser/Game/GameScene.ts | 26 ++++++++++++-------------- front/src/Url/UrlManager.ts | 14 ++++++-------- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/front/src/Connexion/Room.ts b/front/src/Connexion/Room.ts index fcc71ff1..5cd26045 100644 --- a/front/src/Connexion/Room.ts +++ b/front/src/Connexion/Room.ts @@ -6,10 +6,8 @@ export class Room { public readonly isPublic: boolean; private mapUrl: string|undefined; private instance: string|undefined; - public readonly hash: string; constructor(id: string) { - this.hash = ''; if (id.startsWith('/')) { id = id.substr(1); } @@ -24,9 +22,7 @@ export class Room { const indexOfHash = this.id.indexOf('#'); if (indexOfHash !== -1) { - const idWithHash = this.id; this.id = this.id.substr(0, indexOfHash); - this.hash = idWithHash.substr(indexOfHash + 1); } } diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 0d58a4ea..cc575b6a 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -42,7 +42,7 @@ export class GameManager { const gameIndex = scenePlugin.getIndex(roomID); if(gameIndex === -1){ - const game : Phaser.Scene = GameScene.createFromUrl(room, mapUrl); + const game : Phaser.Scene = new GameScene(room, mapUrl); scenePlugin.add(roomID, game, false); } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 34ede8dc..4d6a0be1 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -95,6 +95,8 @@ interface DeleteGroupEventInterface { groupId: number } +const defaultStartLayerName = 'start'; + export class GameScene extends ResizableScene implements CenterListener { GameManager : GameManager; Terrains : Array; @@ -144,11 +146,7 @@ export class GameScene extends ResizableScene implements CenterListener { // The item that can be selected by pressing the space key. private outlinedItem: ActionableItem|null = null; private userInputManager!: UserInputManager; - - static createFromUrl(room: Room, mapUrlFile: string): GameScene { - // We use the map URL as a key - return new GameScene(room, mapUrlFile); - } + private startLayerName!: string | null; constructor(private room: Room, MapUrlFile: string) { super({ @@ -303,7 +301,7 @@ export class GameScene extends ResizableScene implements CenterListener { //hook create scene create(): void { - urlManager.editUrlForCurrentRoom(this.room); + this.startLayerName = urlManager.getStartLayerNameFromUrl(); //initalise map this.Map = this.add.tilemap(this.MapUrlFile); @@ -507,7 +505,7 @@ export class GameScene extends ResizableScene implements CenterListener { this.simplePeer.unregister(); const gameSceneKey = 'somekey' + Math.round(Math.random() * 10000); - const game: Phaser.Scene = GameScene.createFromUrl(this.room, this.MapUrlFile); + const game: Phaser.Scene = new GameScene(this.room, this.MapUrlFile); this.scene.add(gameSceneKey, game, true, { initPosition: { @@ -636,18 +634,18 @@ export class GameScene extends ResizableScene implements CenterListener { private onMapExit(exitKey: string) { const {roomId, hash} = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance); - //todo: push the hash into the url if (!roomId) throw new Error('Could not find the room from its exit key: '+exitKey); + urlManager.pushStartLayerNameToUrl(hash); if (roomId !== this.scene.key) { // We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map. this.connection.closeConnection(); this.simplePeer.unregister(); this.scene.stop(); this.scene.remove(this.scene.key); - this.scene.start(roomId, {hash}); + this.scene.start(roomId); } else { //if the exit points to the current map, we simply teleport the user back to the startLayer - this.initPositionFromLayerName(this.room.hash || 'start'); + this.initPositionFromLayerName(hash || defaultStartLayerName); this.CurrentPlayer.x = this.startX; this.CurrentPlayer.y = this.startY; } @@ -677,12 +675,12 @@ export class GameScene extends ResizableScene implements CenterListener { this.startY = this.initPosition.y; } else { // Now, let's find the start layer - if (this.room.hash) { - this.initPositionFromLayerName(this.room.hash); + if (this.startLayerName) { + this.initPositionFromLayerName(this.startLayerName); } if (this.startX === undefined) { // If we have no start layer specified or if the hash passed does not exist, let's go with the default start position. - this.initPositionFromLayerName("start"); + this.initPositionFromLayerName(defaultStartLayerName); } } // Still no start position? Something is wrong with the map, we need a "start" layer. @@ -696,7 +694,7 @@ export class GameScene extends ResizableScene implements CenterListener { private initPositionFromLayerName(layerName: string) { for (const layer of this.mapFile.layers) { - if (layerName === layer.name && layer.type === 'tilelayer' && (layerName === "start" || this.isStartLayer(layer))) { + if (layerName === layer.name && layer.type === 'tilelayer' && (layerName === defaultStartLayerName || this.isStartLayer(layer))) { const startPosition = this.startUser(layer); this.startX = startPosition.x; this.startY = startPosition.y; diff --git a/front/src/Url/UrlManager.ts b/front/src/Url/UrlManager.ts index 051d841f..8d0738e3 100644 --- a/front/src/Url/UrlManager.ts +++ b/front/src/Url/UrlManager.ts @@ -1,4 +1,3 @@ -import {Room} from "../Connexion/Room"; export enum GameConnexionTypes { anonymous=1, @@ -46,15 +45,14 @@ class UrlManager { return newUrl; } - //todo: is it duplicated with editUrlForRoom() ? - public editUrlForCurrentRoom(room: Room): void { - let path = room.id; - if (room.hash) { - path += '#'+room.hash; - } - history.pushState({}, 'WorkAdventure', path); + public getStartLayerNameFromUrl(): string|null { + let hash = window.location.hash; + return hash.length > 1 ? hash.substring(1) : null; } + pushStartLayerNameToUrl(startLayerName: string): void { + window.location.hash = startLayerName; + } } export const urlManager = new UrlManager();