Merge pull request #177 from thecodingmachine/fixsuspend

Fixing suspend/resume
This commit is contained in:
David Négrier 2020-06-11 10:50:36 +02:00 committed by GitHub
commit ac2e45c60e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -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);
});

View file

@ -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 {