diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 3c1bc2d4..15e58707 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -232,6 +232,8 @@ export class Connection implements ConnectionInterface { } joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void { + let point = new Point(startX, startY, direction, moving); + this.lastPositionShared = point; this.getSocket().emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => { this.GameManager.initUsersPosition(userPositions); }); diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index f9c7c820..c275da53 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -35,7 +35,7 @@ export interface MapObject { export class GameManager { //status: number; private ConnectionInstance: Connection; - private currentGameScene: GameScene|null; + private currentGameScene: GameScene|null = null; private playerName: string; SimplePeer : SimplePeer; private characterUserSelected: string; @@ -168,7 +168,7 @@ export class GameManager { private oldMapUrlFile : string; private oldInstance : string; private scenePlugin: ScenePlugin; - private reconnectScene: Scene; + private reconnectScene: Scene|null = null; switchToDisconnectedScene(): void { if (this.currentGameScene === null) { return; @@ -186,8 +186,15 @@ export class GameManager { } reconnectToGameScene(lastPositionShared: PointInterface) { + if (this.reconnectScene === null && this.currentGameScene) { + // In case we are asked to reconnect even if switchToDisconnectedScene was not triggered (can happen when a laptop goes to sleep) + this.switchToDisconnectedScene(); + // Wait a bit for scene to load. Otherwise, starting ReconnectingSceneName and then starting GameScene one after the other fails for some reason. + setTimeout(() => this.reconnectToGameScene(lastPositionShared), 500); + return; + } const game : Phaser.Scene = GameScene.createFromUrl(this.oldMapUrlFile, this.oldInstance); - this.reconnectScene.scene.add(this.oldSceneKey, game, true, { initPosition: lastPositionShared }); + this.reconnectScene?.scene.add(this.oldSceneKey, game, true, { initPosition: lastPositionShared }); } private getCurrentGameScene(): GameScene {