import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {MapManagerInterface} from "./MapManager";
+import {PlayerAnimationNames} from "../Player/Animation";
export interface CameraManagerInterface {
- CurrentPlayer : Player;
MapManager : MapManagerInterface;
- moveCamera() : void;
+ moveCamera(CurrentPlayer : Player) : void;
}
export class CameraManager implements CameraManagerInterface{
Scene : Phaser.Scene;
Camera : Phaser.Cameras.Scene2D.Camera;
- CurrentPlayer : Player;
MapManager : MapManagerInterface;
constructor(
Scene: Phaser.Scene,
Camera : Phaser.Cameras.Scene2D.Camera,
MapManager: MapManagerInterface,
- CurrentPlayer: Player
) {
this.Scene = Scene;
this.MapManager = MapManager;
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(): void {
+ moveCamera(CurrentPlayer : Player): void {
//center of camera
let startX = ((window.innerWidth / 2) / RESOLUTION);
let startY = ((window.innerHeight / 2) / RESOLUTION);
- //if user client on shift, camera and player speed
- let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1;
-
- if (this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown) {
- 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;
- }else if (this.CurrentPlayer.x < (this.MapManager.Map.heightInPixels - startX)) {
- this.moveCameraPosition(-1, 0, speedMultiplier);
- }
- }
- 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);
- }
+ 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 (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(){
- return this.Camera.scrollY > 0;
- }
-
- 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))
+ 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);
+ }
}
}
\ No newline at end of file
import {MapManagerInterface} from "../Game/MapManager";
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
-import {Connexion} from "../../Connexion";
import {GameSceneInterface} from "../Game/GameScene";
import {ConnexionInstance} from "../Game/GameManager";
+import {CameraManagerInterface} from "../Game/CameraManager";
export class Player extends Phaser.GameObjects.Sprite{
MapManager : MapManagerInterface;
PlayerValue : string;
- Connexion: Connexion;
+ CameraManager: CameraManagerInterface;
constructor(
Scene : GameSceneInterface,
x : number,
y : number,
+ CameraManager: CameraManagerInterface,
MapManager: MapManagerInterface,
PlayerValue : string = "player"
) {
this.PlayerValue = PlayerValue;
Scene.add.existing(this);
this.MapManager = MapManager;
+ this.CameraManager = CameraManager;
}
let direction = null;
if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){
- if(!this.CanToMoveUp()){
+ if(!this.CanMoveUp()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkUp);
direction = PlayerAnimationNames.WalkUp;
}
if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){
- if(!this.CanToMoveLeft()){
+ if(!this.CanMoveLeft()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkLeft);
direction = PlayerAnimationNames.WalkLeft;
}
if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){
- if(!this.CanToMoveDown()){
+ if(!this.CanMoveDown()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkDown);
direction = PlayerAnimationNames.WalkDown;
}
if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){
- if(!this.CanToMoveRight()){
+ if(!this.CanMoveRight()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkRight);
}else{
this.sharePosition(direction);
}
+
+ this.CameraManager.moveCamera(this);
}
private sharePosition(direction : string){
}
}
- private CanToMoveUp(){
+ private CanMoveUp(){
return this.y > 0;
}
- private CanToMoveLeft(){
+ private CanMoveLeft(){
return this.x > 0;
}
- private CanToMoveDown(){
+ private CanMoveDown(){
return this.MapManager.Map.heightInPixels > this.y;
}
- private CanToMoveRight(){
+ private CanMoveRight(){
return this.MapManager.Map.widthInPixels > this.x;
}
}
\ No newline at end of file