From 43e4489d4d4a09748f36f5cf196084205cb6e71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 10 Jun 2020 14:57:32 +0200 Subject: [PATCH 1/4] Fixing start position on disconnect --- front/src/Phaser/Game/GameScene.ts | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 245b2f32..387b7ad3 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -160,23 +160,29 @@ export class GameScene extends Phaser.Scene { throw new Error('Your map MUST contain a layer of type "objectgroup" whose name is "floorLayer" that represents the layer characters are drawn at.'); } - // Now, let's find the start layer - if (this.startLayerName) { - for (let layer of this.mapFile.layers) { - if (this.startLayerName === layer.name && layer.type === 'tilelayer' && this.isStartLayer(layer)) { - let startPosition = this.startUser(layer); - this.startX = startPosition.x; - this.startY = startPosition.y; + // If there is an init position passed + if (this.initPosition !== null) { + this.startX = this.initPosition.x; + this.startY = this.initPosition.y; + } else { + // Now, let's find the start layer + if (this.startLayerName) { + for (let layer of this.mapFile.layers) { + if (this.startLayerName === layer.name && layer.type === 'tilelayer' && this.isStartLayer(layer)) { + let startPosition = this.startUser(layer); + this.startX = startPosition.x; + this.startY = startPosition.y; + } } } - } - 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. - for (let layer of this.mapFile.layers) { - if (layer.type === 'tilelayer' && layer.name === "start") { - let startPosition = this.startUser(layer); - this.startX = startPosition.x; - this.startY = startPosition.y; + 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. + for (let layer of this.mapFile.layers) { + if (layer.type === 'tilelayer' && layer.name === "start") { + let startPosition = this.startUser(layer); + this.startX = startPosition.x; + this.startY = startPosition.y; + } } } } From 500dc83a85872ede1037846a05fbbfe3f71a8c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 10 Jun 2020 15:43:22 +0200 Subject: [PATCH 2/4] Completely resetting GameScene on disconnect --- front/src/Phaser/Entity/Character.ts | 4 +++ front/src/Phaser/Game/GameManager.ts | 44 ++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/front/src/Phaser/Entity/Character.ts b/front/src/Phaser/Entity/Character.ts index ec0167eb..ba6a8228 100644 --- a/front/src/Phaser/Entity/Character.ts +++ b/front/src/Phaser/Entity/Character.ts @@ -117,6 +117,10 @@ export abstract class Character extends Phaser.Physics.Arcade.Sprite { } protected playAnimation(direction : string, moving: boolean): void { + if (!this.anims) { + console.error('ANIMS IS NOT DEFINED!!!'); + return; + } if (moving && (!this.anims.currentAnim || this.anims.currentAnim.key !== direction)) { this.play(this.PlayerTexture+'-'+direction, true); } else if (!moving) { diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 7eef49b4..c804a8b9 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -11,7 +11,9 @@ import { } from "../../Connection"; import {SimplePeer} from "../../WebRtc/SimplePeer"; import {AddPlayerInterface} from "./AddPlayerInterface"; -import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; +import {ReconnectingScene, ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; +import ScenePlugin = Phaser.Scenes.ScenePlugin; +import {Scene} from "phaser"; /*export enum StatusGameManagerEnum { IN_PROGRESS = 1, @@ -33,7 +35,7 @@ export interface MapObject { export class GameManager { //status: number; private ConnectionInstance: Connection; - private currentGameScene: GameScene; + private currentGameScene: GameScene|null; private playerName: string; SimplePeer : SimplePeer; private characterUserSelected: string; @@ -87,15 +89,15 @@ export class GameManager { name: message.name, position: message.position } - this.currentGameScene.addPlayer(userMessage); + this.getCurrentGameScene().addPlayer(userMessage); } onUserMoved(message: MessageUserMovedInterface): void { - this.currentGameScene.updatePlayerPosition(message); + this.getCurrentGameScene().updatePlayerPosition(message); } onUserLeft(userId: string): void { - this.currentGameScene.removePlayer(userId); + this.getCurrentGameScene().removePlayer(userId); } initUsersPosition(usersPosition: MessageUserPositionInterface[]): void { @@ -104,7 +106,7 @@ export class GameManager { return; }*/ try { - this.currentGameScene.initUsersPosition(usersPosition) + this.getCurrentGameScene().initUsersPosition(usersPosition) } catch (e) { console.error(e); } @@ -118,7 +120,7 @@ export class GameManager { return; }*/ try { - this.currentGameScene.shareGroupPosition(groupPositionMessage) + this.getCurrentGameScene().shareGroupPosition(groupPositionMessage) } catch (e) { console.error(e); } @@ -129,7 +131,7 @@ export class GameManager { return; }*/ try { - this.currentGameScene.deleteGroup(groupId) + this.getCurrentGameScene().deleteGroup(groupId) } catch (e) { console.error(e); } @@ -163,13 +165,37 @@ export class GameManager { } private oldSceneKey : string; + private oldMapUrlFile : string; + private oldInstance : string; + private scenePlugin: ScenePlugin; + private reconnectScene: Scene; switchToDisconnectedScene(): void { + if (this.currentGameScene === null) { + return; + } + console.log('Switching to disconnected scene'); this.oldSceneKey = this.currentGameScene.scene.key; + this.oldMapUrlFile = this.currentGameScene.MapUrlFile; + this.oldInstance = this.currentGameScene.instance; this.currentGameScene.scene.start(ReconnectingSceneName); + this.reconnectScene = this.currentGameScene.scene.get(ReconnectingSceneName); + // Let's completely delete an purge the disconnected scene. We will start again from 0. + this.currentGameScene.scene.remove(this.oldSceneKey); + this.scenePlugin = this.currentGameScene.scene; + this.currentGameScene = null; } reconnectToGameScene(lastPositionShared: PointInterface) { - this.currentGameScene.scene.start(this.oldSceneKey, { initPosition: lastPositionShared }); + const game : Phaser.Scene = GameScene.createFromUrl(this.oldMapUrlFile, this.oldInstance); + this.reconnectScene.scene.add(this.oldSceneKey, game, false); + this.reconnectScene.scene.start(this.oldSceneKey, { initPosition: lastPositionShared }); + } + + private getCurrentGameScene(): GameScene { + if (this.currentGameScene === null) { + throw new Error('No current game scene enabled'); + } + return this.currentGameScene; } } From f518830073c996cb9f2fd942a6416928b8ace0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 10 Jun 2020 16:00:54 +0200 Subject: [PATCH 3/4] Minor change to make sure the scene starts even if in background (does this really work?) --- front/src/Phaser/Game/GameManager.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index c804a8b9..f9c7c820 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -187,8 +187,7 @@ export class GameManager { reconnectToGameScene(lastPositionShared: PointInterface) { const game : Phaser.Scene = GameScene.createFromUrl(this.oldMapUrlFile, this.oldInstance); - this.reconnectScene.scene.add(this.oldSceneKey, game, false); - this.reconnectScene.scene.start(this.oldSceneKey, { initPosition: lastPositionShared }); + this.reconnectScene.scene.add(this.oldSceneKey, game, true, { initPosition: lastPositionShared }); } private getCurrentGameScene(): GameScene { From 80fddb3c69cd5e9d9b4fe4b23ef0fd441681a69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 10 Jun 2020 16:01:01 +0200 Subject: [PATCH 4/4] Removing dead code --- front/src/Phaser/Game/GameScene.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 387b7ad3..2b60fd62 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -310,15 +310,6 @@ export class GameScene extends Phaser.Scene { * @param layer */ private startUser(layer: ITiledMapLayer): PositionInterface { - if (this.initPosition !== null) { - this.startX = this.initPosition.x; - this.startY = this.initPosition.y; - return { - x: this.initPosition.x, - y: this.initPosition.y - }; - } - let tiles : any = layer.data; let possibleStartPositions : PositionInterface[] = []; tiles.forEach((objectKey : number, key: number) => {