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:
parent
1e6f9bb8d9
commit
125a4d11af
12 changed files with 238 additions and 101 deletions
8
front/src/Phaser/Game/AddPlayerInterface.ts
Normal file
8
front/src/Phaser/Game/AddPlayerInterface.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import {PointInterface} from "../../Connexion";
|
||||
|
||||
export interface AddPlayerInterface {
|
||||
userId: string;
|
||||
name: string;
|
||||
character: string;
|
||||
position: PointInterface;
|
||||
}
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
import {GameManager, gameManager, HasMovedEvent, MapObject, StatusGameManagerEnum} from "./GameManager";
|
||||
import {GroupCreatedUpdatedMessageInterface, MessageUserPositionInterface} from "../../Connexion";
|
||||
import {
|
||||
GroupCreatedUpdatedMessageInterface,
|
||||
MessageUserJoined,
|
||||
MessageUserMovedInterface,
|
||||
MessageUserPositionInterface
|
||||
} from "../../Connexion";
|
||||
import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
|
||||
import { DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
|
||||
import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap";
|
||||
|
@ -7,25 +12,18 @@ import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
|
|||
import Texture = Phaser.Textures.Texture;
|
||||
import Sprite = Phaser.GameObjects.Sprite;
|
||||
import CanvasTexture = Phaser.Textures.CanvasTexture;
|
||||
import CreateSceneFromObjectConfig = Phaser.Types.Scenes.CreateSceneFromObjectConfig;
|
||||
import {AddPlayerInterface} from "./AddPlayerInterface";
|
||||
|
||||
export enum Textures {
|
||||
Player = "male1"
|
||||
}
|
||||
|
||||
export interface GameSceneInterface extends Phaser.Scene {
|
||||
Map: Phaser.Tilemaps.Tilemap;
|
||||
createCurrentPlayer() : void;
|
||||
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void;
|
||||
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface): void;
|
||||
updateOrCreateMapPlayer(UsersPosition : Array<MessageUserPositionInterface>): void;
|
||||
deleteGroup(groupId: string): void;
|
||||
}
|
||||
export class GameScene extends Phaser.Scene implements GameSceneInterface, CreateSceneFromObjectConfig{
|
||||
export class GameScene extends Phaser.Scene {
|
||||
GameManager : GameManager;
|
||||
Terrains : Array<Phaser.Tilemaps.Tileset>;
|
||||
CurrentPlayer: CurrentGamerInterface;
|
||||
MapPlayers : Phaser.Physics.Arcade.Group;
|
||||
MapPlayersByKey : Map<string, GamerInterface> = new Map<string, GamerInterface>();
|
||||
Map: Phaser.Tilemaps.Tilemap;
|
||||
Layers : Array<Phaser.Tilemaps.StaticTilemapLayer>;
|
||||
Objects : Array<Phaser.Physics.Arcade.Sprite>;
|
||||
|
@ -333,6 +331,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
|
|||
/**
|
||||
* Share position in scene
|
||||
* @param UsersPosition
|
||||
* @deprecated
|
||||
*/
|
||||
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void {
|
||||
this.updateOrCreateMapPlayer(UsersPosition);
|
||||
|
@ -358,7 +357,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
|
|||
if(!player){
|
||||
this.addPlayer(userPosition);
|
||||
}else{
|
||||
player.updatePosition(userPosition);
|
||||
player.updatePosition(userPosition.position);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -372,31 +371,59 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
|
|||
});
|
||||
}
|
||||
|
||||
public initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
|
||||
if(!this.CurrentPlayer){
|
||||
console.error('Cannot initiate users list because map is not loaded yet')
|
||||
return;
|
||||
}
|
||||
|
||||
let currentPlayerId = this.GameManager.getPlayerId();
|
||||
|
||||
// clean map
|
||||
this.MapPlayersByKey.forEach((player: GamerInterface) => {
|
||||
player.destroy();
|
||||
this.MapPlayers.remove(player);
|
||||
});
|
||||
this.MapPlayersByKey = new Map<string, GamerInterface>();
|
||||
|
||||
// load map
|
||||
usersPosition.forEach((userPosition : MessageUserPositionInterface) => {
|
||||
if(userPosition.userId === currentPlayerId){
|
||||
return;
|
||||
}
|
||||
this.addPlayer(userPosition);
|
||||
console.log("Added player ", userPosition)
|
||||
});
|
||||
|
||||
console.log("Initialized with ", usersPosition);
|
||||
}
|
||||
|
||||
private findPlayerInMap(UserId : string) : GamerInterface | null{
|
||||
let player = this.MapPlayers.getChildren().find((player: Player) => UserId === player.userId);
|
||||
return this.MapPlayersByKey.get(UserId);
|
||||
/*let player = this.MapPlayers.getChildren().find((player: Player) => UserId === player.userId);
|
||||
if(!player){
|
||||
return null;
|
||||
}
|
||||
return (player as GamerInterface);
|
||||
return (player as GamerInterface);*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new player
|
||||
* @param MessageUserPosition
|
||||
*/
|
||||
addPlayer(MessageUserPosition : MessageUserPositionInterface) : void{
|
||||
public addPlayer(addPlayerData : AddPlayerInterface) : void{
|
||||
//initialise player
|
||||
let player = new Player(
|
||||
MessageUserPosition.userId,
|
||||
addPlayerData.userId,
|
||||
this,
|
||||
MessageUserPosition.position.x,
|
||||
MessageUserPosition.position.y,
|
||||
MessageUserPosition.name,
|
||||
MessageUserPosition.character
|
||||
addPlayerData.position.x,
|
||||
addPlayerData.position.y,
|
||||
addPlayerData.name,
|
||||
addPlayerData.character
|
||||
);
|
||||
player.initAnimation();
|
||||
this.MapPlayers.add(player);
|
||||
player.updatePosition(MessageUserPosition);
|
||||
this.MapPlayersByKey.set(player.userId, player);
|
||||
player.updatePosition(addPlayerData.position);
|
||||
|
||||
//init collision
|
||||
/*this.physics.add.collider(this.CurrentPlayer, player, (CurrentPlayer: CurrentGamerInterface, MapPlayer: GamerInterface) => {
|
||||
|
@ -404,6 +431,24 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
|
|||
});*/
|
||||
}
|
||||
|
||||
public removePlayer(userId: string) {
|
||||
let player = this.MapPlayersByKey.get(userId);
|
||||
if (player === undefined) {
|
||||
console.error('Cannot find user with id ', userId);
|
||||
}
|
||||
player.destroy();
|
||||
this.MapPlayers.remove(player);
|
||||
this.MapPlayersByKey.delete(userId);
|
||||
}
|
||||
|
||||
updatePlayerPosition(message: MessageUserMovedInterface): void {
|
||||
let player : GamerInterface | undefined = this.MapPlayersByKey.get(message.userId);
|
||||
if (player === undefined) {
|
||||
throw new Error('Cannot find player with ID "' + message.userId +'"');
|
||||
}
|
||||
player.updatePosition(message.position);
|
||||
}
|
||||
|
||||
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
|
||||
let groupId = groupPositionMessage.groupId;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue