2020-05-23 16:50:17 +02:00
|
|
|
import {GameManager, gameManager, HasMovedEvent} from "./GameManager";
|
2020-05-19 19:11:12 +02:00
|
|
|
import {
|
|
|
|
GroupCreatedUpdatedMessageInterface,
|
|
|
|
MessageUserMovedInterface,
|
2020-05-23 15:43:26 +02:00
|
|
|
MessageUserPositionInterface, PointInterface, PositionInterface
|
2020-05-19 19:11:12 +02:00
|
|
|
} from "../../Connexion";
|
2020-05-02 16:54:52 +02:00
|
|
|
import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
|
2020-05-23 16:50:17 +02:00
|
|
|
import { DEBUG_MODE, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
|
2020-05-09 21:28:50 +02:00
|
|
|
import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap";
|
2020-05-06 01:50:01 +02:00
|
|
|
import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
|
2020-05-08 16:09:50 +02:00
|
|
|
import Texture = Phaser.Textures.Texture;
|
|
|
|
import Sprite = Phaser.GameObjects.Sprite;
|
|
|
|
import CanvasTexture = Phaser.Textures.CanvasTexture;
|
2020-05-19 19:11:12 +02:00
|
|
|
import {AddPlayerInterface} from "./AddPlayerInterface";
|
2020-05-22 22:59:43 +02:00
|
|
|
import {PlayerAnimationNames} from "../Player/Animation";
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-11 18:17:36 +02:00
|
|
|
export enum Textures {
|
2020-05-09 19:41:21 +02:00
|
|
|
Player = "male1"
|
2020-04-11 18:17:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 15:43:26 +02:00
|
|
|
interface GameSceneInitInterface {
|
|
|
|
initPosition: PointInterface|null
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export class GameScene extends Phaser.Scene {
|
2020-04-27 15:03:05 +02:00
|
|
|
GameManager : GameManager;
|
2020-04-15 19:39:26 +02:00
|
|
|
Terrains : Array<Phaser.Tilemaps.Tileset>;
|
2020-04-13 15:34:09 +02:00
|
|
|
CurrentPlayer: CurrentGamerInterface;
|
|
|
|
MapPlayers : Phaser.Physics.Arcade.Group;
|
2020-05-19 19:11:12 +02:00
|
|
|
MapPlayersByKey : Map<string, GamerInterface> = new Map<string, GamerInterface>();
|
2020-04-13 15:34:09 +02:00
|
|
|
Map: Phaser.Tilemaps.Tilemap;
|
|
|
|
Layers : Array<Phaser.Tilemaps.StaticTilemapLayer>;
|
|
|
|
Objects : Array<Phaser.Physics.Arcade.Sprite>;
|
2020-04-15 23:10:12 +02:00
|
|
|
map: ITiledMap;
|
2020-05-09 19:41:21 +02:00
|
|
|
groups: Map<string, Sprite>;
|
2020-05-03 17:19:42 +02:00
|
|
|
startX = 704;// 22 case
|
|
|
|
startY = 32; // 1 case
|
2020-05-08 16:09:50 +02:00
|
|
|
circleTexture: CanvasTexture;
|
2020-05-23 15:43:26 +02:00
|
|
|
initPosition: PositionInterface;
|
2020-04-15 19:39:26 +02:00
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
MapKey: string;
|
|
|
|
MapUrlFile: string;
|
2020-05-23 17:27:49 +02:00
|
|
|
RoomId: string;
|
|
|
|
instance: string;
|
2020-05-09 19:41:21 +02:00
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
PositionNextScene: Array<any> = new Array<any>();
|
|
|
|
|
2020-05-23 17:27:49 +02:00
|
|
|
static createFromUrl(mapUrlFile: string, instance: string): GameScene {
|
|
|
|
let key = GameScene.getMapKeyByUrl(mapUrlFile);
|
|
|
|
return new GameScene(key, mapUrlFile, instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(MapKey : string, MapUrlFile: string, instance: string) {
|
2020-04-07 19:23:21 +02:00
|
|
|
super({
|
2020-05-09 19:41:21 +02:00
|
|
|
key: MapKey
|
2020-04-07 19:23:21 +02:00
|
|
|
});
|
2020-05-09 19:41:21 +02:00
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
this.GameManager = gameManager;
|
2020-04-15 19:39:26 +02:00
|
|
|
this.Terrains = [];
|
2020-05-08 16:09:50 +02:00
|
|
|
this.groups = new Map<string, Sprite>();
|
2020-05-23 17:27:49 +02:00
|
|
|
this.instance = instance;
|
2020-05-09 19:41:21 +02:00
|
|
|
|
2020-05-23 17:27:49 +02:00
|
|
|
this.MapKey = MapKey;
|
2020-05-09 19:41:21 +02:00
|
|
|
this.MapUrlFile = MapUrlFile;
|
2020-05-23 17:27:49 +02:00
|
|
|
this.RoomId = this.instance + '__' + this.MapKey;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook preload scene
|
|
|
|
preload(): void {
|
2020-04-27 15:03:05 +02:00
|
|
|
this.GameManager.setCurrentGameScene(this);
|
2020-05-09 19:41:21 +02:00
|
|
|
this.load.on('filecomplete-tilemapJSON-'+this.MapKey, (key: string, type: string, data: any) => {
|
2020-04-15 19:23:06 +02:00
|
|
|
// Triggered when the map is loaded
|
|
|
|
// Load tiles attached to the map recursively
|
2020-04-15 23:10:12 +02:00
|
|
|
this.map = data.data;
|
2020-05-13 00:02:39 +02:00
|
|
|
let url = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
|
2020-04-15 23:10:12 +02:00
|
|
|
this.map.tilesets.forEach((tileset) => {
|
2020-04-15 23:31:39 +02:00
|
|
|
if (typeof tileset.name === 'undefined' || typeof tileset.image === 'undefined') {
|
|
|
|
console.warn("Don't know how to handle tileset ", tileset)
|
|
|
|
return;
|
|
|
|
}
|
2020-05-10 17:55:30 +02:00
|
|
|
//TODO strategy to add access token
|
2020-05-10 18:34:55 +02:00
|
|
|
this.load.image(tileset.name, `${url}/${tileset.image}`);
|
2020-04-15 19:23:06 +02:00
|
|
|
})
|
|
|
|
});
|
2020-05-10 17:55:30 +02:00
|
|
|
//TODO strategy to add access token
|
2020-05-10 18:34:55 +02:00
|
|
|
this.load.tilemapTiledJSON(this.MapKey, this.MapUrlFile);
|
2020-05-06 01:50:01 +02:00
|
|
|
|
|
|
|
//add player png
|
|
|
|
PLAYER_RESOURCES.forEach((playerResource: any) => {
|
|
|
|
this.load.spritesheet(
|
|
|
|
playerResource.name,
|
|
|
|
playerResource.img,
|
|
|
|
{frameWidth: 32, frameHeight: 32}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-05-01 23:38:09 +02:00
|
|
|
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook initialisation
|
2020-05-23 15:43:26 +02:00
|
|
|
init(initData : GameSceneInitInterface) {
|
|
|
|
this.initPosition = initData.initPosition;
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
//hook create scene
|
|
|
|
create(): void {
|
2020-04-13 15:34:09 +02:00
|
|
|
//initalise map
|
2020-05-09 19:41:21 +02:00
|
|
|
this.Map = this.add.tilemap(this.MapKey);
|
2020-04-15 23:10:12 +02:00
|
|
|
this.map.tilesets.forEach((tileset: ITiledTileSet) => {
|
|
|
|
this.Terrains.push(this.Map.addTilesetImage(tileset.name, tileset.name));
|
2020-04-15 19:39:26 +02:00
|
|
|
});
|
2020-04-13 15:34:09 +02:00
|
|
|
|
|
|
|
//permit to set bound collision
|
|
|
|
this.physics.world.setBounds(0,0, this.Map.widthInPixels, this.Map.heightInPixels);
|
|
|
|
|
|
|
|
//add layer on map
|
|
|
|
this.Layers = new Array<Phaser.Tilemaps.StaticTilemapLayer>();
|
2020-04-15 23:10:12 +02:00
|
|
|
let depth = -2;
|
2020-05-09 21:28:50 +02:00
|
|
|
this.map.layers.forEach((layer : ITiledMapLayer) => {
|
2020-05-08 17:35:40 +02:00
|
|
|
if (layer.type === 'tilelayer') {
|
|
|
|
this.addLayer(this.Map.createStaticLayer(layer.name, this.Terrains, 0, 0).setDepth(depth));
|
2020-05-08 13:18:34 +02:00
|
|
|
}
|
2020-05-11 18:49:20 +02:00
|
|
|
if (layer.type === 'tilelayer' && this.getExitSceneUrl(layer) !== undefined) {
|
2020-05-10 14:49:49 +02:00
|
|
|
this.loadNextGame(layer, this.map.width, this.map.tilewidth, this.map.tileheight);
|
2020-05-09 21:28:50 +02:00
|
|
|
}
|
|
|
|
if (layer.type === 'tilelayer' && layer.name === "start") {
|
|
|
|
this.startUser(layer);
|
|
|
|
}
|
2020-05-08 17:35:40 +02:00
|
|
|
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
|
|
|
|
depth = 10000;
|
2020-04-15 23:10:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (depth === -2) {
|
|
|
|
throw new Error('Your map MUST contain a layer of type "objectgroup" whose name is "floorLayer" that represents the layer characters are drawn at.');
|
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
|
|
|
|
//add entities
|
|
|
|
this.Objects = new Array<Phaser.Physics.Arcade.Sprite>();
|
|
|
|
|
|
|
|
//init event click
|
|
|
|
this.EventToClickOnTile();
|
|
|
|
|
|
|
|
//initialise list of other player
|
|
|
|
this.MapPlayers = this.physics.add.group({ immovable: true });
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
//notify game manager can to create currentUser in map
|
|
|
|
this.GameManager.createCurrentPlayer();
|
2020-04-13 19:56:41 +02:00
|
|
|
|
|
|
|
//initialise camera
|
|
|
|
this.initCamera();
|
2020-05-08 00:35:36 +02:00
|
|
|
|
2020-05-08 16:09:50 +02:00
|
|
|
|
|
|
|
// Let's generate the circle for the group delimiter
|
2020-05-10 17:55:30 +02:00
|
|
|
let circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite');
|
|
|
|
if(circleElement) {
|
|
|
|
this.textures.remove('circleSprite');
|
2020-05-09 19:41:21 +02:00
|
|
|
}
|
2020-05-10 17:55:30 +02:00
|
|
|
this.circleTexture = this.textures.createCanvas('circleSprite', 96, 96);
|
2020-05-08 16:09:50 +02:00
|
|
|
let context = this.circleTexture.context;
|
|
|
|
context.beginPath();
|
|
|
|
context.arc(48, 48, 48, 0, 2 * Math.PI, false);
|
|
|
|
// context.lineWidth = 5;
|
|
|
|
context.strokeStyle = '#ffffff';
|
|
|
|
context.stroke();
|
|
|
|
this.circleTexture.refresh();
|
2020-05-12 00:07:50 +02:00
|
|
|
|
|
|
|
// Let's alter browser history
|
|
|
|
let url = new URL(this.MapUrlFile);
|
2020-05-23 17:27:49 +02:00
|
|
|
let path = '/_/'+this.instance+'/'+url.host+url.pathname;
|
2020-05-12 00:07:50 +02:00
|
|
|
if (url.hash) {
|
|
|
|
// FIXME: entry should be dictated by a property passed to init()
|
|
|
|
path += '#'+url.hash;
|
|
|
|
}
|
|
|
|
window.history.pushState({}, null, path);
|
2020-04-13 19:56:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 18:49:20 +02:00
|
|
|
private getExitSceneUrl(layer: ITiledMapLayer): string|undefined {
|
|
|
|
let properties : any = layer.properties;
|
|
|
|
if (!properties) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let obj = properties.find((property:any) => property.name === "exitSceneUrl");
|
|
|
|
if (obj === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return obj.value;
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:27:49 +02:00
|
|
|
private getExitSceneInstance(layer: ITiledMapLayer): string|undefined {
|
|
|
|
let properties : any = layer.properties;
|
|
|
|
if (!properties) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let obj = properties.find((property:any) => property.name === "exitInstance");
|
|
|
|
if (obj === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return obj.value;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
/**
|
2020-05-10 14:49:49 +02:00
|
|
|
*
|
|
|
|
* @param layer
|
|
|
|
* @param mapWidth
|
|
|
|
* @param tileWidth
|
|
|
|
* @param tileHeight
|
2020-05-09 21:28:50 +02:00
|
|
|
*/
|
2020-05-10 14:49:49 +02:00
|
|
|
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
|
2020-05-11 18:49:20 +02:00
|
|
|
let exitSceneUrl = this.getExitSceneUrl(layer);
|
2020-05-23 17:27:49 +02:00
|
|
|
let instance = this.getExitSceneInstance(layer);
|
|
|
|
if (instance === undefined) {
|
|
|
|
instance = this.instance;
|
|
|
|
}
|
2020-05-09 21:28:50 +02:00
|
|
|
|
2020-05-13 00:02:39 +02:00
|
|
|
// TODO: eventually compute a relative URL
|
|
|
|
let absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href;
|
2020-05-23 17:27:49 +02:00
|
|
|
let exitSceneKey = gameManager.loadMap(absoluteExitSceneUrl, this.scene, instance);
|
2020-05-10 18:34:55 +02:00
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
let tiles : any = layer.data;
|
|
|
|
tiles.forEach((objectKey : number, key: number) => {
|
|
|
|
if(objectKey === 0){
|
|
|
|
return;
|
|
|
|
}
|
2020-05-10 14:49:49 +02:00
|
|
|
//key + 1 because the start x = 0;
|
|
|
|
let y : number = parseInt(((key + 1) / mapWidth).toString());
|
|
|
|
let x : number = key - (y * mapWidth);
|
|
|
|
//push and save switching case
|
2020-05-11 18:49:20 +02:00
|
|
|
// TODO: this is not efficient. We should refactor that to enable a search by key. For instance: this.PositionNextScene[y][x] = exitSceneKey
|
2020-05-09 21:28:50 +02:00
|
|
|
this.PositionNextScene.push({
|
2020-05-10 14:49:49 +02:00
|
|
|
xStart: (x * tileWidth),
|
|
|
|
yStart: (y * tileWidth),
|
|
|
|
xEnd: ((x +1) * tileHeight),
|
|
|
|
yEnd: ((y + 1) * tileHeight),
|
2020-05-10 18:34:55 +02:00
|
|
|
key: exitSceneKey
|
2020-05-09 21:28:50 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param layer
|
|
|
|
*/
|
2020-05-23 15:43:26 +02:00
|
|
|
private startUser(layer: ITiledMapLayer): void {
|
|
|
|
if (this.initPosition !== undefined) {
|
|
|
|
this.startX = this.initPosition.x;
|
|
|
|
this.startY = this.initPosition.y;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
let tiles : any = layer.data;
|
|
|
|
tiles.forEach((objectKey : number, key: number) => {
|
|
|
|
if(objectKey === 0){
|
|
|
|
return;
|
|
|
|
}
|
2020-05-11 18:49:20 +02:00
|
|
|
let y = Math.floor(key / layer.width);
|
|
|
|
let x = key % layer.width;
|
2020-05-09 21:28:50 +02:00
|
|
|
|
|
|
|
this.startX = (x * 32);
|
|
|
|
this.startY = (y * 32);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:56:41 +02:00
|
|
|
//todo: in a dedicated class/function?
|
|
|
|
initCamera() {
|
|
|
|
this.cameras.main.setBounds(0,0, this.Map.widthInPixels, this.Map.heightInPixels);
|
|
|
|
this.cameras.main.startFollow(this.CurrentPlayer);
|
|
|
|
this.cameras.main.setZoom(ZOOM_LEVEL);
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
addLayer(Layer : Phaser.Tilemaps.StaticTilemapLayer){
|
|
|
|
this.Layers.push(Layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
createCollisionWithPlayer() {
|
|
|
|
//add collision layer
|
|
|
|
this.Layers.forEach((Layer: Phaser.Tilemaps.StaticTilemapLayer) => {
|
|
|
|
this.physics.add.collider(this.CurrentPlayer, Layer, (object1: any, object2: any) => {
|
2020-04-13 16:56:06 +02:00
|
|
|
//this.CurrentPlayer.say("Collision with layer : "+ (object2 as Tile).layer.name)
|
2020-04-13 15:34:09 +02:00
|
|
|
});
|
|
|
|
Layer.setCollisionByProperty({collides: true});
|
2020-04-13 19:40:10 +02:00
|
|
|
if (DEBUG_MODE) {
|
|
|
|
//debug code to see the collision hitbox of the object in the top layer
|
|
|
|
Layer.renderDebug(this.add.graphics(), {
|
|
|
|
tileColor: null, //non-colliding tiles
|
|
|
|
collidingTileColor: new Phaser.Display.Color(243, 134, 48, 200), // Colliding tiles,
|
|
|
|
faceColor: new Phaser.Display.Color(40, 39, 37, 255) // Colliding face edges
|
|
|
|
});
|
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createCollisionObject(){
|
|
|
|
this.Objects.forEach((Object : Phaser.Physics.Arcade.Sprite) => {
|
|
|
|
this.physics.add.collider(this.CurrentPlayer, Object, (object1: any, object2: any) => {
|
2020-04-13 16:56:06 +02:00
|
|
|
//this.CurrentPlayer.say("Collision with object : " + (object2 as Phaser.Physics.Arcade.Sprite).texture.key)
|
2020-04-13 15:34:09 +02:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
createCurrentPlayer(){
|
2020-04-13 15:34:09 +02:00
|
|
|
//initialise player
|
2020-05-23 17:27:49 +02:00
|
|
|
//TODO create animation moving between exit and start
|
2020-04-13 15:34:09 +02:00
|
|
|
this.CurrentPlayer = new Player(
|
2020-05-14 23:19:48 +02:00
|
|
|
null, // The current player is not has no id (because the id can change if connexion is lost and we should check that id using the GameManager.
|
2020-04-13 15:34:09 +02:00
|
|
|
this,
|
|
|
|
this.startX,
|
|
|
|
this.startY,
|
2020-05-06 01:50:01 +02:00
|
|
|
this.GameManager.getPlayerName(),
|
2020-05-22 22:59:43 +02:00
|
|
|
this.GameManager.getCharacterSelected(),
|
|
|
|
PlayerAnimationNames.WalkDown,
|
|
|
|
false
|
2020-04-13 15:34:09 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//create collision
|
|
|
|
this.createCollisionWithPlayer();
|
|
|
|
this.createCollisionObject();
|
2020-05-10 13:58:32 +02:00
|
|
|
|
|
|
|
//join room
|
2020-05-23 17:27:49 +02:00
|
|
|
this.GameManager.joinRoom(this.RoomId, this.startX, this.startY, PlayerAnimationNames.WalkDown, false);
|
2020-05-10 13:58:32 +02:00
|
|
|
|
|
|
|
//listen event to share position of user
|
2020-05-02 16:54:52 +02:00
|
|
|
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
|
|
|
|
}
|
2020-05-04 18:38:04 +02:00
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
pushPlayerPosition(event: HasMovedEvent) {
|
|
|
|
this.GameManager.pushPlayerPosition(event);
|
2020-04-13 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EventToClickOnTile(){
|
|
|
|
// debug code to get a tile properties by clicking on it
|
|
|
|
this.input.on("pointerdown", (pointer: Phaser.Input.Pointer)=>{
|
|
|
|
//pixel position toz tile position
|
|
|
|
let tile = this.Map.getTileAt(this.Map.worldToTileX(pointer.worldX), this.Map.worldToTileY(pointer.worldY));
|
|
|
|
if(tile){
|
|
|
|
this.CurrentPlayer.say("Your touch " + tile.layer.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-18 17:16:39 +02:00
|
|
|
/**
|
|
|
|
* @param time
|
|
|
|
* @param delta The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
|
|
|
*/
|
|
|
|
update(time: number, delta: number) : void {
|
|
|
|
this.CurrentPlayer.moveUser(delta);
|
2020-05-09 21:28:50 +02:00
|
|
|
let nextSceneKey = this.checkToExit();
|
|
|
|
if(nextSceneKey){
|
|
|
|
this.scene.start(nextSceneKey.key);
|
2020-05-09 19:41:21 +02:00
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
checkToExit(){
|
|
|
|
if(this.PositionNextScene.length === 0){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.PositionNextScene.find((position : any) => {
|
|
|
|
return position.xStart <= this.CurrentPlayer.x && this.CurrentPlayer.x <= position.xEnd
|
|
|
|
&& position.yStart <= this.CurrentPlayer.y && this.CurrentPlayer.y <= position.yEnd
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
2020-04-13 15:34:09 +02:00
|
|
|
* Share position in scene
|
|
|
|
* @param UsersPosition
|
2020-05-19 19:11:12 +02:00
|
|
|
* @deprecated
|
2020-04-10 12:54:05 +02:00
|
|
|
*/
|
2020-04-13 15:34:09 +02:00
|
|
|
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void {
|
|
|
|
this.updateOrCreateMapPlayer(UsersPosition);
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
/**
|
|
|
|
* Create new player and clean the player on the map
|
|
|
|
* @param UsersPosition
|
|
|
|
*/
|
|
|
|
updateOrCreateMapPlayer(UsersPosition : Array<MessageUserPositionInterface>){
|
|
|
|
if(!this.CurrentPlayer){
|
2020-04-10 12:54:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
let currentPlayerId = this.GameManager.getPlayerId();
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
//add or create new user
|
|
|
|
UsersPosition.forEach((userPosition : MessageUserPositionInterface) => {
|
2020-05-14 23:19:48 +02:00
|
|
|
if(userPosition.userId === currentPlayerId){
|
2020-04-13 15:34:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let player = this.findPlayerInMap(userPosition.userId);
|
|
|
|
if(!player){
|
|
|
|
this.addPlayer(userPosition);
|
|
|
|
}else{
|
2020-05-19 19:11:12 +02:00
|
|
|
player.updatePosition(userPosition.position);
|
2020-04-13 15:34:09 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//clean map
|
|
|
|
this.MapPlayers.getChildren().forEach((player: GamerInterface) => {
|
|
|
|
if(UsersPosition.find((message : MessageUserPositionInterface) => message.userId === player.userId)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.destroy();
|
|
|
|
this.MapPlayers.remove(player);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
private findPlayerInMap(UserId : string) : GamerInterface | null{
|
2020-05-19 19:11:12 +02:00
|
|
|
return this.MapPlayersByKey.get(UserId);
|
|
|
|
/*let player = this.MapPlayers.getChildren().find((player: Player) => UserId === player.userId);
|
2020-04-13 15:34:09 +02:00
|
|
|
if(!player){
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-19 19:11:12 +02:00
|
|
|
return (player as GamerInterface);*/
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
2020-04-13 15:34:09 +02:00
|
|
|
* Create new player
|
2020-04-10 12:54:05 +02:00
|
|
|
*/
|
2020-05-19 19:11:12 +02:00
|
|
|
public addPlayer(addPlayerData : AddPlayerInterface) : void{
|
2020-05-23 14:00:36 +02:00
|
|
|
//check if exist player, if exist, move position
|
|
|
|
if(this.MapPlayersByKey.has(addPlayerData.userId)){
|
|
|
|
this.updatePlayerPosition({
|
|
|
|
userId: addPlayerData.userId,
|
|
|
|
position: addPlayerData.position
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
//initialise player
|
|
|
|
let player = new Player(
|
2020-05-19 19:11:12 +02:00
|
|
|
addPlayerData.userId,
|
2020-04-13 15:34:09 +02:00
|
|
|
this,
|
2020-05-19 19:11:12 +02:00
|
|
|
addPlayerData.position.x,
|
|
|
|
addPlayerData.position.y,
|
|
|
|
addPlayerData.name,
|
2020-05-22 22:59:43 +02:00
|
|
|
addPlayerData.character,
|
|
|
|
addPlayerData.position.direction,
|
|
|
|
addPlayerData.position.moving
|
2020-04-13 15:34:09 +02:00
|
|
|
);
|
|
|
|
this.MapPlayers.add(player);
|
2020-05-19 19:11:12 +02:00
|
|
|
this.MapPlayersByKey.set(player.userId, player);
|
|
|
|
player.updatePosition(addPlayerData.position);
|
2020-04-13 15:34:09 +02:00
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
//init collision
|
|
|
|
/*this.physics.add.collider(this.CurrentPlayer, player, (CurrentPlayer: CurrentGamerInterface, MapPlayer: GamerInterface) => {
|
2020-04-13 15:35:38 +02:00
|
|
|
CurrentPlayer.say("Hello, how are you ? ");
|
2020-05-08 15:18:22 +02:00
|
|
|
});*/
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
public removePlayer(userId: string) {
|
2020-05-22 22:59:43 +02:00
|
|
|
console.log('Removing player ', userId)
|
2020-05-19 19:11:12 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
|
|
|
|
let groupId = groupPositionMessage.groupId;
|
|
|
|
|
|
|
|
if (this.groups.has(groupId)) {
|
2020-05-08 16:09:50 +02:00
|
|
|
this.groups.get(groupId).setPosition(Math.round(groupPositionMessage.position.x), Math.round(groupPositionMessage.position.y));
|
2020-05-08 00:35:36 +02:00
|
|
|
} else {
|
|
|
|
// TODO: circle radius should not be hard stored
|
2020-05-10 17:55:30 +02:00
|
|
|
let sprite = new Sprite(
|
|
|
|
this,
|
|
|
|
Math.round(groupPositionMessage.position.x),
|
|
|
|
Math.round(groupPositionMessage.position.y),
|
|
|
|
'circleSprite');
|
2020-05-08 16:09:50 +02:00
|
|
|
sprite.setDisplayOrigin(48, 48);
|
|
|
|
this.add.existing(sprite);
|
|
|
|
this.groups.set(groupId, sprite);
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteGroup(groupId: string): void {
|
2020-05-11 13:17:02 +02:00
|
|
|
if(!this.groups.get(groupId)){
|
|
|
|
return;
|
|
|
|
}
|
2020-05-08 16:09:50 +02:00
|
|
|
this.groups.get(groupId).destroy();
|
2020-05-08 00:35:36 +02:00
|
|
|
this.groups.delete(groupId);
|
|
|
|
}
|
2020-05-23 17:27:49 +02:00
|
|
|
|
|
|
|
public static getMapKeyByUrl(mapUrlStart: string) : string {
|
|
|
|
// FIXME: the key should be computed from the full URL of the map.
|
|
|
|
let startPos = mapUrlStart.indexOf('://')+3;
|
|
|
|
let endPos = mapUrlStart.indexOf(".json");
|
|
|
|
return mapUrlStart.substring(startPos, endPos);
|
|
|
|
|
|
|
|
|
|
|
|
let tab = mapUrlStart.split("/");
|
|
|
|
return tab[tab.length -1].substr(0, tab[tab.length -1].indexOf(".json"));
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|