partey_workadventure/front/src/Phaser/Game/GameScene.ts

107 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-04-07 20:41:35 +02:00
import {GameManagerInterface} from "./GameManager";
import {UserInputManager} from "../UserInput/UserInputManager";
import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
import {Player} from "../Player/Player";
2020-04-11 18:17:36 +02:00
export enum Textures {
Rock = 'rock',
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{
//private MapManager : MapManagerInterface;
2020-04-07 20:41:35 +02:00
RoomId : string;
private player: Player;
private rock: Phaser.Physics.Arcade.Sprite;
private userInputManager: UserInputManager;
2020-04-07 20:41:35 +02:00
constructor(RoomId : string, GameManager : GameManagerInterface) {
super({
key: "GameScene"
});
2020-04-07 20:41:35 +02:00
this.RoomId = RoomId;
}
//hook preload scene
preload(): void {
2020-04-11 18:17:36 +02:00
this.load.image(Textures.Rock, 'resources/objects/rockSprite.png');
this.load.image(Textures.Tiles, 'maps/tiles.png');
this.load.tilemapTiledJSON(Textures.Map, 'maps/map2.json');
this.load.spritesheet(Textures.Player,
'resources/characters/pipoya/Male 01-1.png',
{ frameWidth: 32, frameHeight: 32 }
);
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
});
});
}
//hook create scene
create(): void {
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);
this.physics.world.addCollider(this.player, this.rock, (player: Player, rock) => {
rock.destroy();
});
//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);
}
});
}
//hook update
update(dt: number): void {
let eventList = this.userInputManager.getEventListForGameTick();
this.player.move(eventList);
}
2020-04-07 20:41:35 +02:00
sharedUserPosition(data: []): void {
//TODO share position of all user
//console.log("sharedUserPosition", data);
}
}