Fix and refactor with comments of @moumoug

This commit is contained in:
gparant 2020-04-07 22:38:53 +02:00
parent 77780bd27b
commit 25895e51f7
3 changed files with 39 additions and 77 deletions

View File

@ -1,95 +1,54 @@
import {RESOLUTION} from "../../Enum/EnvironmentVariable"; import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player"; import {Player} from "../Player/Player";
import {MapManagerInterface} from "./MapManager"; import {MapManagerInterface} from "./MapManager";
import {PlayerAnimationNames} from "../Player/Animation";
export interface CameraManagerInterface { export interface CameraManagerInterface {
CurrentPlayer : Player;
MapManager : MapManagerInterface; MapManager : MapManagerInterface;
moveCamera() : void; moveCamera(CurrentPlayer : Player) : void;
} }
export class CameraManager implements CameraManagerInterface{ export class CameraManager implements CameraManagerInterface{
Scene : Phaser.Scene; Scene : Phaser.Scene;
Camera : Phaser.Cameras.Scene2D.Camera; Camera : Phaser.Cameras.Scene2D.Camera;
CurrentPlayer : Player;
MapManager : MapManagerInterface; MapManager : MapManagerInterface;
constructor( constructor(
Scene: Phaser.Scene, Scene: Phaser.Scene,
Camera : Phaser.Cameras.Scene2D.Camera, Camera : Phaser.Cameras.Scene2D.Camera,
MapManager: MapManagerInterface, MapManager: MapManagerInterface,
CurrentPlayer: Player
) { ) {
this.Scene = Scene; this.Scene = Scene;
this.MapManager = MapManager; this.MapManager = MapManager;
this.Camera = Camera; this.Camera = Camera;
this.CurrentPlayer = CurrentPlayer;
}
/**
*
* @param x
* @param y
* @param speedMultiplier
*/
private moveCameraPosition(x:number, y:number, speedMultiplier: number): void {
this.Camera.scrollX += speedMultiplier * 2 * x;
this.Camera.scrollY += speedMultiplier * 2 * y;
} }
/** moveCamera(CurrentPlayer : Player): void {
*
*/
moveCamera(): void {
//center of camera //center of camera
let startX = ((window.innerWidth / 2) / RESOLUTION); let startX = ((window.innerWidth / 2) / RESOLUTION);
let startY = ((window.innerHeight / 2) / RESOLUTION); let startY = ((window.innerHeight / 2) / RESOLUTION);
//if user client on shift, camera and player speed let limit = {
let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1; top: startY,
left: startX,
bottom : this.MapManager.Map.heightInPixels - startY,
right: this.MapManager.Map.widthInPixels - startX,
};
if (this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown) { if(CurrentPlayer.x < limit.left){
if (!this.CanToMoveUp()) {
this.Camera.scrollY = 0;
}else if (this.CurrentPlayer.y < (this.MapManager.Map.widthInPixels - startY)) {
this.moveCameraPosition(0, -1, speedMultiplier);
}
}
if (this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown) {
if (!this.CanToMoveLeft()) {
this.Camera.scrollX = 0; this.Camera.scrollX = 0;
}else if (this.CurrentPlayer.x < (this.MapManager.Map.heightInPixels - startX)) { }else if(CurrentPlayer.x > limit.right){
this.moveCameraPosition(-1, 0, speedMultiplier); this.Camera.scrollX = (limit.right - startX);
} }else {
} this.Camera.scrollX = (CurrentPlayer.x - startX);
if (this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown) {
if (!this.CanToMoveDown()) {
this.Camera.scrollY = (this.MapManager.Map.heightInPixels - (window.innerHeight / RESOLUTION));
} else if (this.CurrentPlayer.y > startY) {
this.moveCameraPosition(0, 1, speedMultiplier);
}
}
if (this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown) {
if (!this.CanToMoveRight()) {
this.Camera.scrollX = (this.MapManager.Map.widthInPixels - (window.innerWidth / RESOLUTION));
} else if (this.CurrentPlayer.x > startX) {
this.moveCameraPosition(1, 0, speedMultiplier);
}
}
} }
private CanToMoveUp(){ if(CurrentPlayer.y < limit.top){
return this.Camera.scrollY > 0; this.Camera.scrollY = 0;
}else if(CurrentPlayer.y > limit.bottom){
this.Camera.scrollY = (limit.bottom - startY);
}else {
this.Camera.scrollY = (CurrentPlayer.y - startY);
} }
private CanToMoveLeft(){
return this.Camera.scrollX > 0;
}
private CanToMoveDown(){
return this.MapManager.Map.heightInPixels > (this.Camera.scrollY + (window.innerHeight / RESOLUTION))
}
private CanToMoveRight(){
return this.MapManager.Map.widthInPixels > (this.Camera.scrollX + (window.innerWidth / RESOLUTION))
} }
} }

View File

@ -52,17 +52,17 @@ export class MapManager implements MapManagerInterface{
//initialise keyboard //initialise keyboard
this.initKeyBoard(); this.initKeyBoard();
//initialise camera
this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this);
//initialise player //initialise player
this.CurrentPlayer = new Player( this.CurrentPlayer = new Player(
this.Scene, this.Scene,
this.startX, this.startX,
this.startY, this.startY,
this.Camera,
this this
); );
this.CurrentPlayer.initAnimation(); this.CurrentPlayer.initAnimation();
//initialise camera
this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this, this.CurrentPlayer);
} }
@ -82,6 +82,5 @@ export class MapManager implements MapManagerInterface{
update() : void { update() : void {
this.CurrentPlayer.move(); this.CurrentPlayer.move();
this.Camera.moveCamera()
} }
} }

View File

@ -1,18 +1,19 @@
import {MapManagerInterface} from "../Game/MapManager"; import {MapManagerInterface} from "../Game/MapManager";
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation"; import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
import {Connexion} from "../../Connexion";
import {GameSceneInterface} from "../Game/GameScene"; import {GameSceneInterface} from "../Game/GameScene";
import {ConnexionInstance} from "../Game/GameManager"; import {ConnexionInstance} from "../Game/GameManager";
import {CameraManagerInterface} from "../Game/CameraManager";
export class Player extends Phaser.GameObjects.Sprite{ export class Player extends Phaser.GameObjects.Sprite{
MapManager : MapManagerInterface; MapManager : MapManagerInterface;
PlayerValue : string; PlayerValue : string;
Connexion: Connexion; CameraManager: CameraManagerInterface;
constructor( constructor(
Scene : GameSceneInterface, Scene : GameSceneInterface,
x : number, x : number,
y : number, y : number,
CameraManager: CameraManagerInterface,
MapManager: MapManagerInterface, MapManager: MapManagerInterface,
PlayerValue : string = "player" PlayerValue : string = "player"
) { ) {
@ -20,6 +21,7 @@ export class Player extends Phaser.GameObjects.Sprite{
this.PlayerValue = PlayerValue; this.PlayerValue = PlayerValue;
Scene.add.existing(this); Scene.add.existing(this);
this.MapManager = MapManager; this.MapManager = MapManager;
this.CameraManager = CameraManager;
} }
@ -41,7 +43,7 @@ export class Player extends Phaser.GameObjects.Sprite{
let direction = null; let direction = null;
if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){
if(!this.CanToMoveUp()){ if(!this.CanMoveUp()){
return; return;
} }
playAnimation(this, PlayerAnimationNames.WalkUp); playAnimation(this, PlayerAnimationNames.WalkUp);
@ -50,7 +52,7 @@ export class Player extends Phaser.GameObjects.Sprite{
direction = PlayerAnimationNames.WalkUp; direction = PlayerAnimationNames.WalkUp;
} }
if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){
if(!this.CanToMoveLeft()){ if(!this.CanMoveLeft()){
return; return;
} }
playAnimation(this, PlayerAnimationNames.WalkLeft); playAnimation(this, PlayerAnimationNames.WalkLeft);
@ -59,7 +61,7 @@ export class Player extends Phaser.GameObjects.Sprite{
direction = PlayerAnimationNames.WalkLeft; direction = PlayerAnimationNames.WalkLeft;
} }
if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){
if(!this.CanToMoveDown()){ if(!this.CanMoveDown()){
return; return;
} }
playAnimation(this, PlayerAnimationNames.WalkDown); playAnimation(this, PlayerAnimationNames.WalkDown);
@ -68,7 +70,7 @@ export class Player extends Phaser.GameObjects.Sprite{
direction = PlayerAnimationNames.WalkDown; direction = PlayerAnimationNames.WalkDown;
} }
if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){
if(!this.CanToMoveRight()){ if(!this.CanMoveRight()){
return; return;
} }
playAnimation(this, PlayerAnimationNames.WalkRight); playAnimation(this, PlayerAnimationNames.WalkRight);
@ -81,6 +83,8 @@ export class Player extends Phaser.GameObjects.Sprite{
}else{ }else{
this.sharePosition(direction); this.sharePosition(direction);
} }
this.CameraManager.moveCamera(this);
} }
private sharePosition(direction : string){ private sharePosition(direction : string){
@ -89,19 +93,19 @@ export class Player extends Phaser.GameObjects.Sprite{
} }
} }
private CanToMoveUp(){ private CanMoveUp(){
return this.y > 0; return this.y > 0;
} }
private CanToMoveLeft(){ private CanMoveLeft(){
return this.x > 0; return this.x > 0;
} }
private CanToMoveDown(){ private CanMoveDown(){
return this.MapManager.Map.heightInPixels > this.y; return this.MapManager.Map.heightInPixels > this.y;
} }
private CanToMoveRight(){ private CanMoveRight(){
return this.MapManager.Map.widthInPixels > this.x; return this.MapManager.Map.widthInPixels > this.x;
} }
} }