Multi players on the map

- Fix share user position
 - Fix initialise map
 - Create function to add user on the map with back end data
This commit is contained in:
gparant 2020-04-10 12:54:05 +02:00
parent 6bec8b3703
commit d257b2b944
10 changed files with 253 additions and 48 deletions

View file

@ -1,7 +1,8 @@
import {CameraManager, CameraManagerInterface} from "./CameraManager";
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {GameScene, GameSceneInterface} from "./GameScene";
import {GameSceneInterface} from "./GameScene";
import {MessageUserPositionInterface} from "../../Connexion";
export interface MapManagerInterface {
keyZ: Phaser.Input.Keyboard.Key;
@ -18,7 +19,10 @@ export interface MapManagerInterface {
Terrain: Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
Scene: GameSceneInterface;
createCurrentPlayer(UserId : string): void;
update(): void;
updateOrCreateMapPlayer(UsersPosition : Array<MessageUserPositionInterface>): void;
}
export class MapManager implements MapManagerInterface{
keyZ: Phaser.Input.Keyboard.Key;
@ -34,6 +38,7 @@ export class MapManager implements MapManagerInterface{
Terrain : Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
CurrentPlayer: Player;
MapPlayers : Player[];
Scene: GameSceneInterface;
Map: Phaser.Tilemaps.Tilemap;
startX = (window.innerWidth / 2) / RESOLUTION;
@ -51,11 +56,16 @@ export class MapManager implements MapManagerInterface{
//initialise keyboard
this.initKeyBoard();
//initialise camera
this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this);
//initialise list of other player
this.MapPlayers = new Array<Player>();
}
createCurrentPlayer(UserId : string){
//initialise player
this.CurrentPlayer = new Player(
UserId,
this.Scene,
this.startX,
this.startY,
@ -65,7 +75,6 @@ export class MapManager implements MapManagerInterface{
this.CurrentPlayer.initAnimation();
}
initKeyBoard() {
this.keyShift = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT);
@ -83,4 +92,57 @@ export class MapManager implements MapManagerInterface{
update() : void {
this.CurrentPlayer.move();
}
/**
* Create new player and clean the player on the map
* @param UsersPosition
*/
updateOrCreateMapPlayer(UsersPosition : Array<MessageUserPositionInterface>){
if(!this.CurrentPlayer){
return;
}
//add or create new user
UsersPosition.forEach((userPosition : MessageUserPositionInterface) => {
if(userPosition.userId === this.CurrentPlayer.userId){
return;
}
let player = this.MapPlayers.find((player: Player) => userPosition.userId === player.userId);
if(!player){
this.addPlayer(userPosition);
}else{
player.updatePosition(userPosition);
}
});
//clean map
let mapPlayers = new Array<Player>();
this.MapPlayers.forEach((player: Player) => {
if(UsersPosition.find((message : MessageUserPositionInterface) => message.userId === player.userId)){
mapPlayers.push(player);
return;
}
player.destroy();
});
this.MapPlayers = mapPlayers;
}
/**
* Create new player
* @param MessageUserPosition
*/
addPlayer(MessageUserPosition : MessageUserPositionInterface){
//initialise player
let player = new Player(
MessageUserPosition.userId,
this.Scene,
MessageUserPosition.position.x,
MessageUserPosition.position.y,
this.Camera,
this
);
player.initAnimation();
this.MapPlayers.push(player);
player.updatePosition(MessageUserPosition)
}
}