Refactored and optimized messages

Now, when a user moves, only his/her position is sent back to the other users. The position of all users is not sent each time.

The messages sent to the browser are now:

- the list of all users as a return to the join_room event (you can send responses to events in socket.io)
- a "join_room" event sent when a new user joins the room
- a "user_moved" event when a user moved
- a "user_left" event when a user left the room

The GameScene tracks all these events and reacts accordingly.

Also, I made a number of refactoring in the classes and removed the GameSceneInterface that was useless (it was implemented by the LogincScene for no reason at all)
This commit is contained in:
David Négrier 2020-05-19 19:11:12 +02:00
parent 1e6f9bb8d9
commit 125a4d11af
12 changed files with 238 additions and 101 deletions

View file

@ -1,12 +1,13 @@
import {GameScene, GameSceneInterface} from "./GameScene";
import {GameScene} from "./GameScene";
import {
Connexion,
GroupCreatedUpdatedMessageInterface,
ListMessageUserPositionInterface
ListMessageUserPositionInterface, MessageUserJoined, MessageUserMovedInterface, MessageUserPositionInterface, Point
} from "../../Connexion";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
import {getMapKeyByUrl} from "../Login/LogincScene";
import ScenePlugin = Phaser.Scenes.ScenePlugin;
import {AddPlayerInterface} from "./AddPlayerInterface";
export enum StatusGameManagerEnum {
IN_PROGRESS = 1,
@ -27,7 +28,7 @@ export interface MapObject {
export class GameManager {
status: number;
private ConnexionInstance: Connexion;
private currentGameScene: GameSceneInterface;
private currentGameScene: GameScene;
private playerName: string;
SimplePeer : SimplePeerInterface;
private characterUserSelected: string;
@ -56,7 +57,7 @@ export class GameManager {
});
}
setCurrentGameScene(gameScene: GameSceneInterface) {
setCurrentGameScene(gameScene: GameScene) {
this.currentGameScene = gameScene;
}
@ -74,9 +75,28 @@ export class GameManager {
this.ConnexionInstance.joinARoom(sceneKey);
}
onUserJoins(message: MessageUserJoined): void {
let userMessage: AddPlayerInterface = {
userId: message.userId,
character: message.character,
name: message.name,
position: new Point(-1000, -1000)
}
this.currentGameScene.addPlayer(userMessage);
}
onUserMoved(message: MessageUserMovedInterface): void {
this.currentGameScene.updatePlayerPosition(message);
}
onUserLeft(userId: string): void {
this.currentGameScene.removePlayer(userId);
}
/**
* Share position in game
* @param ListMessageUserPosition
* @deprecated
*/
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
@ -89,6 +109,18 @@ export class GameManager {
}
}
initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
// Shall we wait for room to be loaded?
/*if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
return;
}*/
try {
this.currentGameScene.initUsersPosition(usersPosition)
} catch (e) {
console.error(e);
}
}
/**
* Share group position in game
*/