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

View file

@ -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,36 @@ 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, true, { initPosition: lastPositionShared });
}
private getCurrentGameScene(): GameScene {
if (this.currentGameScene === null) {
throw new Error('No current game scene enabled');
}
return this.currentGameScene;
}
}

View file

@ -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;
}
}
}
}
@ -304,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) => {