Refactoring reconnection: putting it into the GameScene directly.

This commit is contained in:
David Négrier 2020-06-22 15:00:23 +02:00
parent d785a8a1bf
commit f88f28db3f
5 changed files with 101 additions and 58 deletions

View file

@ -122,7 +122,7 @@ export class GameManager {
this.currentGameScene = null;
}
private timeoutCallback: NodeJS.Timeout|null = null;
/*private timeoutCallback: NodeJS.Timeout|null = null;
reconnectToGameScene(lastPositionShared: PointInterface): void {
if (this.timeoutCallback !== null) {
console.log('Reconnect called but setTimeout in progress for the reconnection');
@ -150,7 +150,7 @@ export class GameManager {
const game : Phaser.Scene = GameScene.createFromUrl(this.oldMapUrlFile, this.oldInstance);
this.reconnectScene.scene.add(this.oldSceneKey, game, true, { initPosition: lastPositionShared });
this.reconnectScene = null;
}
}*/
private getCurrentGameScene(): GameScene {
if (this.currentGameScene === null) {

View file

@ -25,6 +25,7 @@ import {RemotePlayer} from "../Entity/RemotePlayer";
import GameObject = Phaser.GameObjects.GameObject;
import { Queue } from 'queue-typescript';
import {SimplePeer} from "../../WebRtc/SimplePeer";
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
export enum Textures {
@ -123,8 +124,7 @@ export class GameScene extends Phaser.Scene {
this.MapKey = MapKey;
this.MapUrlFile = MapUrlFile;
this.RoomId = this.instance + '__' + this.MapKey;
this.RoomId = this.instance + '__' + GameScene.getMapKeyByUrl(MapUrlFile);
}
//hook preload scene
@ -186,11 +186,29 @@ export class GameScene extends Phaser.Scene {
}
})
connection.onServerDisconnected(() => {
console.log('Player disconnected from server. Reloading scene.');
this.simplePeer.closeAllConnections();
let key = 'somekey'+Math.round(Math.random()*10000);
const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, key);
this.scene.add(key, game, false,
{
initPosition: {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y
}
});
this.scene.start(key);
})
// When connection is performed, let's connect SimplePeer
this.simplePeer = new SimplePeer(this.connection);
if (this.scene.isPaused()) {
this.scene.resume();
if (this.scene.isSleeping()) {
this.scene.wake();
this.scene.sleep(ReconnectingSceneName);
}
return connection;
@ -326,7 +344,13 @@ export class GameScene extends Phaser.Scene {
// Let's pause the scene if the connection is not established yet
if (this.connection === undefined) {
this.scene.pause();
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
setTimeout(() => {
if (this.connection === undefined) {
this.scene.sleep();
this.scene.launch(ReconnectingSceneName);
}
}, 500);
}
}