2020-04-10 12:54:05 +02:00
|
|
|
import {GameManagerInterface, StatusGameManagerEnum} from "./GameManager";
|
|
|
|
import {MessageUserPositionInterface} from "../../Connexion";
|
2020-04-13 15:34:09 +02:00
|
|
|
import {CameraManager, CameraManagerInterface} from "./CameraManager";
|
|
|
|
import {CurrentGamerInterface, GamerInterface, Player} from "../Player/Player";
|
|
|
|
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
|
|
|
|
import Tile = Phaser.Tilemaps.Tile;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-11 18:17:36 +02:00
|
|
|
export enum Textures {
|
|
|
|
Rock = 'rock',
|
2020-04-12 18:28:05 +02:00
|
|
|
Player = 'playerModel',
|
2020-04-12 16:12:08 +02:00
|
|
|
Map = 'map',
|
|
|
|
Tiles = 'tiles'
|
2020-04-11 18:17:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:41:35 +02:00
|
|
|
export interface GameSceneInterface extends Phaser.Scene {
|
|
|
|
RoomId : string;
|
2020-04-13 15:34:09 +02:00
|
|
|
Map: Phaser.Tilemaps.Tilemap;
|
2020-04-10 12:54:05 +02:00
|
|
|
createCurrentPlayer(UserId : string) : void;
|
|
|
|
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
2020-04-10 12:54:05 +02:00
|
|
|
GameManager : GameManagerInterface;
|
2020-04-07 20:41:35 +02:00
|
|
|
RoomId : string;
|
2020-04-13 15:34:09 +02:00
|
|
|
Terrain : Phaser.Tilemaps.Tileset;
|
|
|
|
Camera: CameraManagerInterface;
|
|
|
|
CurrentPlayer: CurrentGamerInterface;
|
|
|
|
MapPlayers : Phaser.Physics.Arcade.Group;
|
|
|
|
Map: Phaser.Tilemaps.Tilemap;
|
|
|
|
Layers : Array<Phaser.Tilemaps.StaticTilemapLayer>;
|
|
|
|
Objects : Array<Phaser.Physics.Arcade.Sprite>;
|
|
|
|
startX = (window.innerWidth / 2) / RESOLUTION;
|
|
|
|
startY = (window.innerHeight / 2) / RESOLUTION;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-07 20:41:35 +02:00
|
|
|
constructor(RoomId : string, GameManager : GameManagerInterface) {
|
2020-04-07 19:23:21 +02:00
|
|
|
super({
|
|
|
|
key: "GameScene"
|
|
|
|
});
|
2020-04-07 20:41:35 +02:00
|
|
|
this.RoomId = RoomId;
|
2020-04-10 12:54:05 +02:00
|
|
|
this.GameManager = GameManager;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook preload scene
|
|
|
|
preload(): void {
|
2020-04-12 16:12:08 +02:00
|
|
|
this.load.image(Textures.Tiles, 'maps/tiles.png');
|
|
|
|
this.load.tilemapTiledJSON(Textures.Map, 'maps/map2.json');
|
2020-04-13 13:42:21 +02:00
|
|
|
this.load.image(Textures.Rock, 'resources/objects/rockSprite.png');
|
2020-04-12 16:12:08 +02:00
|
|
|
this.load.spritesheet(Textures.Player,
|
2020-04-07 19:23:21 +02:00
|
|
|
'resources/characters/pipoya/Male 01-1.png',
|
|
|
|
{ frameWidth: 32, frameHeight: 32 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//hook initialisation
|
2020-04-07 20:46:30 +02:00
|
|
|
init(){}
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
//hook create scene
|
|
|
|
create(): void {
|
2020-04-13 15:34:09 +02:00
|
|
|
|
|
|
|
//initalise map
|
|
|
|
this.Map = this.add.tilemap("map");
|
|
|
|
this.Terrain = this.Map.addTilesetImage("tiles", "tiles");
|
|
|
|
this.Map.createStaticLayer("tiles", "tiles");
|
|
|
|
|
|
|
|
//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>();
|
|
|
|
this.addLayer( this.Map.createStaticLayer("Calque 1", [this.Terrain], 0, 0).setDepth(-2) );
|
|
|
|
this.addLayer( this.Map.createStaticLayer("Calque 2", [this.Terrain], 0, 0).setDepth(-1) );
|
|
|
|
|
|
|
|
//add entities
|
|
|
|
this.Objects = new Array<Phaser.Physics.Arcade.Sprite>();
|
|
|
|
this.addSpite(this.physics.add.sprite(200, 400, Textures.Rock, 26));
|
|
|
|
|
|
|
|
//init event click
|
|
|
|
this.EventToClickOnTile();
|
|
|
|
|
|
|
|
//initialise camera
|
|
|
|
this.Camera = new CameraManager(this, this.cameras.main);
|
|
|
|
|
|
|
|
//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 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) => {
|
|
|
|
this.CurrentPlayer.say("Collision with layer : "+ (object2 as Tile).layer.name)
|
|
|
|
});
|
|
|
|
Layer.setCollisionByProperty({collides: true});
|
|
|
|
//debug code
|
|
|
|
//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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addSpite(Object : Phaser.Physics.Arcade.Sprite){
|
|
|
|
Object.setImmovable(true);
|
|
|
|
this.Objects.push(Object);
|
|
|
|
}
|
|
|
|
|
|
|
|
createCollisionObject(){
|
|
|
|
this.Objects.forEach((Object : Phaser.Physics.Arcade.Sprite) => {
|
|
|
|
this.physics.add.collider(this.CurrentPlayer, Object, (object1: any, object2: any) => {
|
|
|
|
this.CurrentPlayer.say("Collision with object : " + (object2 as Phaser.Physics.Arcade.Sprite).texture.key)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
createCurrentPlayer(UserId : string){
|
|
|
|
//initialise player
|
|
|
|
this.CurrentPlayer = new Player(
|
|
|
|
UserId,
|
|
|
|
this,
|
|
|
|
this.startX,
|
|
|
|
this.startY,
|
|
|
|
this.Camera,
|
|
|
|
);
|
|
|
|
this.CurrentPlayer.initAnimation();
|
|
|
|
|
|
|
|
//create collision
|
|
|
|
this.createCollisionWithPlayer();
|
|
|
|
this.createCollisionObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
update() : void {
|
|
|
|
this.CurrentPlayer.moveUser();
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
2020-04-13 15:34:09 +02:00
|
|
|
* Share position in scene
|
|
|
|
* @param UsersPosition
|
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
|
|
|
|
|
|
|
//add or create new user
|
|
|
|
UsersPosition.forEach((userPosition : MessageUserPositionInterface) => {
|
|
|
|
if(userPosition.userId === this.CurrentPlayer.userId){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let player = this.findPlayerInMap(userPosition.userId);
|
|
|
|
if(!player){
|
|
|
|
this.addPlayer(userPosition);
|
|
|
|
}else{
|
|
|
|
player.updatePosition(userPosition);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//clean map
|
|
|
|
this.MapPlayers.getChildren().forEach((player: GamerInterface) => {
|
|
|
|
if(UsersPosition.find((message : MessageUserPositionInterface) => message.userId === player.userId)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.destroy();
|
|
|
|
this.MapPlayers.remove(player);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private findPlayerInMap(UserId : string) : GamerInterface | null{
|
|
|
|
let player = this.MapPlayers.getChildren().find((player: Player) => UserId === player.userId);
|
|
|
|
if(!player){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* @param MessageUserPosition
|
2020-04-10 12:54:05 +02:00
|
|
|
*/
|
2020-04-13 15:34:09 +02:00
|
|
|
addPlayer(MessageUserPosition : MessageUserPositionInterface){
|
|
|
|
//initialise player
|
|
|
|
let player = new Player(
|
|
|
|
MessageUserPosition.userId,
|
|
|
|
this,
|
|
|
|
MessageUserPosition.position.x,
|
|
|
|
MessageUserPosition.position.y,
|
|
|
|
this.Camera,
|
|
|
|
);
|
|
|
|
player.initAnimation();
|
|
|
|
this.MapPlayers.add(player);
|
|
|
|
player.updatePosition(MessageUserPosition);
|
|
|
|
|
|
|
|
//init colision
|
|
|
|
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-04-13 15:34:09 +02:00
|
|
|
});
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|