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:
parent
6bec8b3703
commit
d257b2b944
10 changed files with 253 additions and 48 deletions
|
@ -1,31 +1,76 @@
|
|||
import {GameSceneInterface, GameScene} from "./GameScene";
|
||||
import {ROOM} from "../../Enum/EnvironmentVariable"
|
||||
import {Connexion} from "../../Connexion";
|
||||
import {Connexion, ListMessageUserPositionInterface} from "../../Connexion";
|
||||
|
||||
export enum StatusGameManagerEnum {
|
||||
IN_PROGRESS = 1,
|
||||
CURRENT_USER_CREATED = 2
|
||||
}
|
||||
|
||||
export let ConnexionInstance : Connexion;
|
||||
|
||||
export interface GameManagerInterface {
|
||||
GameScenes: Array<GameSceneInterface>;
|
||||
|
||||
sharedUserPosition(UserPositions: any): void;
|
||||
status : number;
|
||||
createCurrentPlayer() : void;
|
||||
shareUserPosition(ListMessageUserPosition : ListMessageUserPositionInterface): void;
|
||||
}
|
||||
export class GameManager implements GameManagerInterface {
|
||||
GameScenes: Array<GameSceneInterface> = [];
|
||||
status: number;
|
||||
|
||||
constructor() {
|
||||
this.configureGame();
|
||||
this.status = StatusGameManagerEnum.IN_PROGRESS;
|
||||
ConnexionInstance = new Connexion("test@gmail.com", this);
|
||||
}
|
||||
|
||||
configureGame() {
|
||||
ROOM.forEach((roomId) => {
|
||||
let newGame = new GameScene(roomId, this);
|
||||
this.GameScenes.push(newGame);
|
||||
createGame(){
|
||||
return ConnexionInstance.createConnexion().then((data: any) => {
|
||||
this.configureGame();
|
||||
/** TODO add loader in the page **/
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
sharedUserPosition(UserPositions: any) {
|
||||
let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === UserPositions.roomId);
|
||||
Game.sharedUserPosition(UserPositions)
|
||||
/**
|
||||
* permit to config rooms
|
||||
*/
|
||||
configureGame() {
|
||||
ROOM.forEach((roomId) => {
|
||||
let newGame = new GameScene(roomId, this);
|
||||
this.GameScenes.push((newGame as GameSceneInterface));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Permit to create player in started room
|
||||
* @param RoomId
|
||||
* @param UserId
|
||||
*/
|
||||
createCurrentPlayer(): void {
|
||||
let game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ConnexionInstance.startedRoom);
|
||||
game.createCurrentPlayer(ConnexionInstance.userId);
|
||||
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Share position in game
|
||||
* @param ListMessageUserPosition
|
||||
*/
|
||||
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
|
||||
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ListMessageUserPosition.roomId);
|
||||
if (!Game) {
|
||||
return;
|
||||
}
|
||||
Game.shareUserPosition(ListMessageUserPosition.listUsersPosition)
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,15 @@
|
|||
import {MapManagerInterface, MapManager} from "./MapManager";
|
||||
import {GameManagerInterface} from "./GameManager";
|
||||
import {GameManagerInterface, StatusGameManagerEnum} from "./GameManager";
|
||||
import {MessageUserPositionInterface} from "../../Connexion";
|
||||
|
||||
export interface GameSceneInterface extends Phaser.Scene {
|
||||
RoomId : string;
|
||||
sharedUserPosition(data : []): void;
|
||||
createCurrentPlayer(UserId : string) : void;
|
||||
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void;
|
||||
}
|
||||
export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
||||
private MapManager : MapManagerInterface;
|
||||
GameManager : GameManagerInterface;
|
||||
RoomId : string;
|
||||
|
||||
constructor(RoomId : string, GameManager : GameManagerInterface) {
|
||||
|
@ -14,6 +17,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
|||
key: "GameScene"
|
||||
});
|
||||
this.RoomId = RoomId;
|
||||
this.GameManager = GameManager;
|
||||
}
|
||||
|
||||
//hook preload scene
|
||||
|
@ -33,15 +37,31 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
|||
create(): void {
|
||||
//create map manager
|
||||
this.MapManager = new MapManager(this);
|
||||
//notify game manager can to create currentUser in map
|
||||
this.GameManager.createCurrentPlayer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create current player
|
||||
* @param UserId
|
||||
*/
|
||||
createCurrentPlayer(UserId : string): void {
|
||||
this.MapManager.createCurrentPlayer(UserId)
|
||||
}
|
||||
|
||||
//hook update
|
||||
update(dt: number): void {
|
||||
if(this.GameManager.status === StatusGameManagerEnum.IN_PROGRESS){
|
||||
return;
|
||||
}
|
||||
this.MapManager.update();
|
||||
}
|
||||
|
||||
sharedUserPosition(data: []): void {
|
||||
//TODO share position of all user
|
||||
//console.log("sharedUserPosition", data);
|
||||
/**
|
||||
* Share position in scene
|
||||
* @param UsersPosition
|
||||
*/
|
||||
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void {
|
||||
this.MapManager.updateOrCreateMapPlayer(UsersPosition);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue