2020-04-07 20:41:35 +02:00
|
|
|
import {GameManagerInterface} from "./GameManager";
|
2020-04-12 16:12:08 +02:00
|
|
|
import {UserInputManager} from "../UserInput/UserInputManager";
|
|
|
|
import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
|
|
|
|
import {Player} from "../Player/Player";
|
2020-04-12 17:08:28 +02:00
|
|
|
import {NonPlayer} from "../NonPlayer/NonPlayer";
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-11 18:17:36 +02:00
|
|
|
export enum Textures {
|
|
|
|
Rock = 'rock',
|
2020-04-12 16:12:08 +02:00
|
|
|
Player = 'player',
|
|
|
|
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;
|
|
|
|
sharedUserPosition(data : []): void;
|
|
|
|
}
|
|
|
|
export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
2020-04-12 16:12:08 +02:00
|
|
|
//private MapManager : MapManagerInterface;
|
2020-04-07 20:41:35 +02:00
|
|
|
RoomId : string;
|
2020-04-12 16:12:08 +02:00
|
|
|
private player: Player;
|
|
|
|
private rock: Phaser.Physics.Arcade.Sprite;
|
|
|
|
private userInputManager: UserInputManager;
|
2020-04-12 17:08:28 +02:00
|
|
|
private otherPlayers: Phaser.Physics.Arcade.Group;
|
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-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook preload scene
|
|
|
|
preload(): void {
|
2020-04-11 18:17:36 +02:00
|
|
|
this.load.image(Textures.Rock, 'resources/objects/rockSprite.png');
|
2020-04-12 16:12:08 +02:00
|
|
|
this.load.image(Textures.Tiles, 'maps/tiles.png');
|
|
|
|
this.load.tilemapTiledJSON(Textures.Map, 'maps/map2.json');
|
|
|
|
this.load.spritesheet(Textures.Player,
|
2020-04-07 19:23:21 +02:00
|
|
|
'resources/characters/pipoya/Male 01-1.png',
|
|
|
|
{ frameWidth: 32, frameHeight: 32 }
|
|
|
|
);
|
|
|
|
|
2020-04-12 16:12:08 +02:00
|
|
|
getPlayerAnimations().forEach(d => {
|
|
|
|
this.anims.create({
|
|
|
|
key: d.key,
|
|
|
|
frames: this.anims.generateFrameNumbers(d.frameModel, { start: d.frameStart, end: d.frameEnd }),
|
|
|
|
frameRate: d.frameRate,
|
|
|
|
//repeat: d.repeat
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
//hook create scene
|
|
|
|
create(): void {
|
2020-04-12 16:12:08 +02:00
|
|
|
this.userInputManager = new UserInputManager(this);
|
|
|
|
|
|
|
|
//create entities
|
|
|
|
this.player = new Player(this, 400, 400);
|
|
|
|
this.rock = this.physics.add.sprite(200, 400, Textures.Rock, 26).setImmovable(true);
|
2020-04-12 17:08:28 +02:00
|
|
|
this.physics.add.collider(this.player, this.rock);
|
|
|
|
|
|
|
|
this.otherPlayers = this.physics.add.group({ immovable: true });
|
|
|
|
this.otherPlayers.add(new NonPlayer(this, 200, 600));
|
|
|
|
this.otherPlayers.add(new NonPlayer(this, 400, 600));
|
|
|
|
|
|
|
|
this.physics.add.collider(this.player, this.otherPlayers);
|
2020-04-12 16:12:08 +02:00
|
|
|
|
|
|
|
//create map
|
|
|
|
let currentMap = this.add.tilemap(Textures.Map);
|
|
|
|
let terrain = currentMap.addTilesetImage(Textures.Tiles, "tiles");
|
|
|
|
let bottomLayer = currentMap.createStaticLayer("Calque 1", [terrain], 0, 0).setDepth(-1);
|
|
|
|
let topLayer = currentMap.createStaticLayer("Calque 2", [terrain], 0, 0);
|
|
|
|
this.physics.world.setBounds(0,0, currentMap.widthInPixels, currentMap.heightInPixels);
|
|
|
|
|
|
|
|
this.physics.add.collider(this.player, topLayer);
|
|
|
|
topLayer.setCollisionByProperty({collides:true});
|
|
|
|
|
|
|
|
|
|
|
|
this.cameras.main.startFollow(this.player);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//debug code
|
|
|
|
//debug code to see the collision hitbox of the object in the top layer
|
|
|
|
topLayer.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
|
|
|
|
})
|
|
|
|
|
|
|
|
// debug code to get a tile properties by clicking on it
|
|
|
|
this.input.on("pointerdown", (pointer: Phaser.Input.Pointer)=>{
|
|
|
|
//pixel position to tile position
|
|
|
|
let tile = currentMap.getTileAt(currentMap.worldToTileX(pointer.worldX), currentMap.worldToTileY(pointer.worldY));
|
|
|
|
if(tile){
|
|
|
|
console.log(tile);
|
|
|
|
}
|
|
|
|
});
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook update
|
|
|
|
update(dt: number): void {
|
2020-04-12 16:12:08 +02:00
|
|
|
let eventList = this.userInputManager.getEventListForGameTick();
|
|
|
|
|
|
|
|
this.player.move(eventList);
|
2020-04-12 17:13:33 +02:00
|
|
|
|
|
|
|
this.otherPlayers.getChildren().forEach((otherPlayer: NonPlayer) => {
|
|
|
|
otherPlayer.setVelocity(20, 5);
|
|
|
|
})
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
sharedUserPosition(data: []): void {
|
|
|
|
//TODO share position of all user
|
|
|
|
//console.log("sharedUserPosition", data);
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|