rewrote the app code to more easily allow for collisions

This commit is contained in:
kharhamel 2020-04-12 16:12:08 +02:00
parent 241cbd720a
commit 6e27377b07
10 changed files with 139 additions and 268 deletions

View file

@ -1,54 +0,0 @@
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {MapManagerInterface} from "./MapManager";
import {PlayerAnimationNames} from "../Player/Animation";
export interface CameraManagerInterface {
MapManager : MapManagerInterface;
moveCamera(CurrentPlayer : Player) : void;
}
export class CameraManager implements CameraManagerInterface{
Scene : Phaser.Scene;
Camera : Phaser.Cameras.Scene2D.Camera;
MapManager : MapManagerInterface;
constructor(
Scene: Phaser.Scene,
Camera : Phaser.Cameras.Scene2D.Camera,
MapManager: MapManagerInterface,
) {
this.Scene = Scene;
this.MapManager = MapManager;
this.Camera = Camera;
}
moveCamera(CurrentPlayer : Player): void {
//center of camera
let startX = ((window.innerWidth / 2) / RESOLUTION);
let startY = ((window.innerHeight / 2) / RESOLUTION);
let limit = {
top: startY,
left: startX,
bottom : this.MapManager.Map.heightInPixels - startY,
right: this.MapManager.Map.widthInPixels - startX,
};
if(CurrentPlayer.x < limit.left){
this.Camera.scrollX = 0;
}else if(CurrentPlayer.x > limit.right){
this.Camera.scrollX = (limit.right - startX);
}else {
this.Camera.scrollX = (CurrentPlayer.x - startX);
}
if(CurrentPlayer.y < limit.top){
this.Camera.scrollY = 0;
}else if(CurrentPlayer.y > limit.bottom){
this.Camera.scrollY = (limit.bottom - startY);
}else {
this.Camera.scrollY = (CurrentPlayer.y - startY);
}
}
}

View file

@ -1,8 +1,13 @@
import {MapManagerInterface, MapManager} from "./MapManager";
import {GameManagerInterface} from "./GameManager";
import {UserInputManager} from "../UserInput/UserInputManager";
import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
import {Player} from "../Player/Player";
export enum Textures {
Rock = 'rock',
Player = 'player',
Map = 'map',
Tiles = 'tiles'
}
export interface GameSceneInterface extends Phaser.Scene {
@ -10,8 +15,11 @@ export interface GameSceneInterface extends Phaser.Scene {
sharedUserPosition(data : []): void;
}
export class GameScene extends Phaser.Scene implements GameSceneInterface{
private MapManager : MapManagerInterface;
//private MapManager : MapManagerInterface;
RoomId : string;
private player: Player;
private rock: Phaser.Physics.Arcade.Sprite;
private userInputManager: UserInputManager;
constructor(RoomId : string, GameManager : GameManagerInterface) {
super({
@ -23,26 +31,72 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
//hook preload scene
preload(): void {
this.load.image(Textures.Rock, 'resources/objects/rockSprite.png');
this.load.image('tiles', 'maps/tiles.png');
this.load.tilemapTiledJSON('map', 'maps/map2.json');
this.load.spritesheet('player',
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 }
);
}
//hook initialisation
init(){}
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 {
//create map manager
this.MapManager = new MapManager(this);
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 {
this.MapManager.update();
let eventList = this.userInputManager.getEventListForGameTick();
this.player.move(eventList);
}
sharedUserPosition(data: []): void {

View file

@ -1,66 +0,0 @@
import {CameraManager, CameraManagerInterface} from "./CameraManager";
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {Rock} from "../Rock/Rock";
import {GameSceneInterface} from "./GameScene";
import {UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
export interface MapManagerInterface {
Map: Phaser.Tilemaps.Tilemap;
Terrain: Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
Scene: GameSceneInterface;
userInputManager: UserInputManager;
update(): void;
}
export class MapManager implements MapManagerInterface{
Terrain : Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
CurrentPlayer: Player;
Scene: GameSceneInterface;
Map: Phaser.Tilemaps.Tilemap;
startX = (window.innerWidth / 2) / RESOLUTION;
startY = (window.innerHeight / 2) / RESOLUTION;
userInputManager: UserInputManager;
private rock: Rock;
constructor(scene: GameSceneInterface){
this.Scene = scene;
//initalise map
this.Map = this.Scene.add.tilemap("map");
this.Terrain = this.Map.addTilesetImage("tiles", "tiles");
this.Map.createStaticLayer("tiles", "tiles");
this.Map.createStaticLayer("Calque 1", [this.Terrain], 0, 0);
this.Map.createStaticLayer("Calque 2", [this.Terrain], 0, 0);
//initialise camera
this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this);
this.userInputManager = new UserInputManager(this.Scene);
//initialise player
this.CurrentPlayer = new Player(
this.Scene,
this.startX,
this.startY,
this.Camera,
this
);
this.CurrentPlayer.initAnimation();
this.rock = new Rock(
this.Scene,
100,
300,
);
//this.rock.set()
}
update() : void {
let activeEvents = this.userInputManager.getEventListForGameTick();
this.CurrentPlayer.move(activeEvents);
/*if (activeEvents.get(UserInputEvent.Interact)) {
}*/
}
}