Merge pull request #174 from thecodingmachine/fixstartpos

Fixing start position on disconnect
This commit is contained in:
David Négrier 2020-06-10 16:08:59 +02:00 committed by GitHub
commit 1e362a5eb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 33 deletions

View File

@ -117,6 +117,10 @@ export abstract class Character extends Phaser.Physics.Arcade.Sprite {
} }
protected playAnimation(direction : string, moving: boolean): void { 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)) { if (moving && (!this.anims.currentAnim || this.anims.currentAnim.key !== direction)) {
this.play(this.PlayerTexture+'-'+direction, true); this.play(this.PlayerTexture+'-'+direction, true);
} else if (!moving) { } else if (!moving) {

View File

@ -11,7 +11,9 @@ import {
} from "../../Connection"; } from "../../Connection";
import {SimplePeer} from "../../WebRtc/SimplePeer"; import {SimplePeer} from "../../WebRtc/SimplePeer";
import {AddPlayerInterface} from "./AddPlayerInterface"; 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 { /*export enum StatusGameManagerEnum {
IN_PROGRESS = 1, IN_PROGRESS = 1,
@ -33,7 +35,7 @@ export interface MapObject {
export class GameManager { export class GameManager {
//status: number; //status: number;
private ConnectionInstance: Connection; private ConnectionInstance: Connection;
private currentGameScene: GameScene; private currentGameScene: GameScene|null;
private playerName: string; private playerName: string;
SimplePeer : SimplePeer; SimplePeer : SimplePeer;
private characterUserSelected: string; private characterUserSelected: string;
@ -87,15 +89,15 @@ export class GameManager {
name: message.name, name: message.name,
position: message.position position: message.position
} }
this.currentGameScene.addPlayer(userMessage); this.getCurrentGameScene().addPlayer(userMessage);
} }
onUserMoved(message: MessageUserMovedInterface): void { onUserMoved(message: MessageUserMovedInterface): void {
this.currentGameScene.updatePlayerPosition(message); this.getCurrentGameScene().updatePlayerPosition(message);
} }
onUserLeft(userId: string): void { onUserLeft(userId: string): void {
this.currentGameScene.removePlayer(userId); this.getCurrentGameScene().removePlayer(userId);
} }
initUsersPosition(usersPosition: MessageUserPositionInterface[]): void { initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
@ -104,7 +106,7 @@ export class GameManager {
return; return;
}*/ }*/
try { try {
this.currentGameScene.initUsersPosition(usersPosition) this.getCurrentGameScene().initUsersPosition(usersPosition)
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
@ -118,7 +120,7 @@ export class GameManager {
return; return;
}*/ }*/
try { try {
this.currentGameScene.shareGroupPosition(groupPositionMessage) this.getCurrentGameScene().shareGroupPosition(groupPositionMessage)
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
@ -129,7 +131,7 @@ export class GameManager {
return; return;
}*/ }*/
try { try {
this.currentGameScene.deleteGroup(groupId) this.getCurrentGameScene().deleteGroup(groupId)
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
@ -163,13 +165,36 @@ export class GameManager {
} }
private oldSceneKey : string; private oldSceneKey : string;
private oldMapUrlFile : string;
private oldInstance : string;
private scenePlugin: ScenePlugin;
private reconnectScene: Scene;
switchToDisconnectedScene(): void { switchToDisconnectedScene(): void {
if (this.currentGameScene === null) {
return;
}
console.log('Switching to disconnected scene');
this.oldSceneKey = this.currentGameScene.scene.key; this.oldSceneKey = this.currentGameScene.scene.key;
this.oldMapUrlFile = this.currentGameScene.MapUrlFile;
this.oldInstance = this.currentGameScene.instance;
this.currentGameScene.scene.start(ReconnectingSceneName); 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) { 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, true, { initPosition: lastPositionShared });
}
private getCurrentGameScene(): GameScene {
if (this.currentGameScene === null) {
throw new Error('No current game scene enabled');
}
return this.currentGameScene;
} }
} }

View File

@ -160,6 +160,11 @@ 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.'); throw new Error('Your map MUST contain a layer of type "objectgroup" whose name is "floorLayer" that represents the layer characters are drawn at.');
} }
// 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 // Now, let's find the start layer
if (this.startLayerName) { if (this.startLayerName) {
for (let layer of this.mapFile.layers) { for (let layer of this.mapFile.layers) {
@ -180,6 +185,7 @@ export class GameScene extends Phaser.Scene {
} }
} }
} }
}
// 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.
if (this.startX === undefined) { if (this.startX === undefined) {
console.warn('This map is missing a layer named "start" that contains the available default start positions.'); console.warn('This map is missing a layer named "start" that contains the available default start positions.');
@ -304,15 +310,6 @@ export class GameScene extends Phaser.Scene {
* @param layer * @param layer
*/ */
private startUser(layer: ITiledMapLayer): PositionInterface { 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 tiles : any = layer.data;
let possibleStartPositions : PositionInterface[] = []; let possibleStartPositions : PositionInterface[] = [];
tiles.forEach((objectKey : number, key: number) => { tiles.forEach((objectKey : number, key: number) => {