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