FIX: the game now uses the url hash to choose the start layer

This commit is contained in:
kharhamel 2020-11-19 14:32:18 +01:00
parent 09d6d22a5d
commit 8d8b879ed6
4 changed files with 19 additions and 27 deletions

View File

@ -6,10 +6,8 @@ export class Room {
public readonly isPublic: boolean; public readonly isPublic: boolean;
private mapUrl: string|undefined; private mapUrl: string|undefined;
private instance: string|undefined; private instance: string|undefined;
public readonly hash: string;
constructor(id: string) { constructor(id: string) {
this.hash = '';
if (id.startsWith('/')) { if (id.startsWith('/')) {
id = id.substr(1); id = id.substr(1);
} }
@ -24,9 +22,7 @@ export class Room {
const indexOfHash = this.id.indexOf('#'); const indexOfHash = this.id.indexOf('#');
if (indexOfHash !== -1) { if (indexOfHash !== -1) {
const idWithHash = this.id;
this.id = this.id.substr(0, indexOfHash); this.id = this.id.substr(0, indexOfHash);
this.hash = idWithHash.substr(indexOfHash + 1);
} }
} }

View File

@ -42,7 +42,7 @@ export class GameManager {
const gameIndex = scenePlugin.getIndex(roomID); const gameIndex = scenePlugin.getIndex(roomID);
if(gameIndex === -1){ if(gameIndex === -1){
const game : Phaser.Scene = GameScene.createFromUrl(room, mapUrl); const game : Phaser.Scene = new GameScene(room, mapUrl);
scenePlugin.add(roomID, game, false); scenePlugin.add(roomID, game, false);
} }
} }

View File

@ -95,6 +95,8 @@ interface DeleteGroupEventInterface {
groupId: number groupId: number
} }
const defaultStartLayerName = 'start';
export class GameScene extends ResizableScene implements CenterListener { export class GameScene extends ResizableScene implements CenterListener {
GameManager : GameManager; GameManager : GameManager;
Terrains : Array<Phaser.Tilemaps.Tileset>; Terrains : Array<Phaser.Tilemaps.Tileset>;
@ -144,11 +146,7 @@ export class GameScene extends ResizableScene implements CenterListener {
// The item that can be selected by pressing the space key. // The item that can be selected by pressing the space key.
private outlinedItem: ActionableItem|null = null; private outlinedItem: ActionableItem|null = null;
private userInputManager!: UserInputManager; private userInputManager!: UserInputManager;
private startLayerName!: string | null;
static createFromUrl(room: Room, mapUrlFile: string): GameScene {
// We use the map URL as a key
return new GameScene(room, mapUrlFile);
}
constructor(private room: Room, MapUrlFile: string) { constructor(private room: Room, MapUrlFile: string) {
super({ super({
@ -303,7 +301,7 @@ export class GameScene extends ResizableScene implements CenterListener {
//hook create scene //hook create scene
create(): void { create(): void {
urlManager.editUrlForCurrentRoom(this.room); this.startLayerName = urlManager.getStartLayerNameFromUrl();
//initalise map //initalise map
this.Map = this.add.tilemap(this.MapUrlFile); this.Map = this.add.tilemap(this.MapUrlFile);
@ -507,7 +505,7 @@ export class GameScene extends ResizableScene implements CenterListener {
this.simplePeer.unregister(); this.simplePeer.unregister();
const gameSceneKey = 'somekey' + Math.round(Math.random() * 10000); 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, this.scene.add(gameSceneKey, game, true,
{ {
initPosition: { initPosition: {
@ -636,18 +634,18 @@ export class GameScene extends ResizableScene implements CenterListener {
private onMapExit(exitKey: string) { private onMapExit(exitKey: string) {
const {roomId, hash} = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance); 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); if (!roomId) throw new Error('Could not find the room from its exit key: '+exitKey);
urlManager.pushStartLayerNameToUrl(hash);
if (roomId !== this.scene.key) { 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. // 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.connection.closeConnection();
this.simplePeer.unregister(); this.simplePeer.unregister();
this.scene.stop(); this.scene.stop();
this.scene.remove(this.scene.key); this.scene.remove(this.scene.key);
this.scene.start(roomId, {hash}); this.scene.start(roomId);
} else { } else {
//if the exit points to the current map, we simply teleport the user back to the startLayer //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.x = this.startX;
this.CurrentPlayer.y = this.startY; this.CurrentPlayer.y = this.startY;
} }
@ -677,12 +675,12 @@ export class GameScene extends ResizableScene implements CenterListener {
this.startY = this.initPosition.y; this.startY = this.initPosition.y;
} else { } else {
// Now, let's find the start layer // Now, let's find the start layer
if (this.room.hash) { if (this.startLayerName) {
this.initPositionFromLayerName(this.room.hash); this.initPositionFromLayerName(this.startLayerName);
} }
if (this.startX === undefined) { 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. // 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. // 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) { private initPositionFromLayerName(layerName: string) {
for (const layer of this.mapFile.layers) { 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); const startPosition = this.startUser(layer);
this.startX = startPosition.x; this.startX = startPosition.x;
this.startY = startPosition.y; this.startY = startPosition.y;

View File

@ -1,4 +1,3 @@
import {Room} from "../Connexion/Room";
export enum GameConnexionTypes { export enum GameConnexionTypes {
anonymous=1, anonymous=1,
@ -46,15 +45,14 @@ class UrlManager {
return newUrl; return newUrl;
} }
//todo: is it duplicated with editUrlForRoom() ? public getStartLayerNameFromUrl(): string|null {
public editUrlForCurrentRoom(room: Room): void { let hash = window.location.hash;
let path = room.id; return hash.length > 1 ? hash.substring(1) : null;
if (room.hash) {
path += '#'+room.hash;
}
history.pushState({}, 'WorkAdventure', path);
} }
pushStartLayerNameToUrl(startLayerName: string): void {
window.location.hash = startLayerName;
}
} }
export const urlManager = new UrlManager(); export const urlManager = new UrlManager();