From 5d463d097a45766735d7c1dd99209d7e56fc35e4 Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 19:23:21 +0200 Subject: [PATCH 1/7] Refactor Class - Add MapManager permit to create map, camera and player. - Add CameraManager permit to move and update camera. - Add player Call extended of Phaser.GameObjects.Sprite. Permit to manager player data and moving in the map. - Add Animation class permit to manage the player animations. --- front/src/GameScene.ts | 185 --------------------------- front/src/Phaser/CameraManager.ts | 95 ++++++++++++++ front/src/Phaser/GameScene.ts | 35 +++++ front/src/Phaser/MapManager.ts | 85 ++++++++++++ front/src/Phaser/Player.ts | 90 +++++++++++++ front/src/Phaser/Player/Animation.ts | 60 +++++++++ front/src/index.ts | 2 +- 7 files changed, 366 insertions(+), 186 deletions(-) delete mode 100644 front/src/GameScene.ts create mode 100644 front/src/Phaser/CameraManager.ts create mode 100644 front/src/Phaser/GameScene.ts create mode 100644 front/src/Phaser/MapManager.ts create mode 100644 front/src/Phaser/Player.ts create mode 100644 front/src/Phaser/Player/Animation.ts diff --git a/front/src/GameScene.ts b/front/src/GameScene.ts deleted file mode 100644 index 2cb36131..00000000 --- a/front/src/GameScene.ts +++ /dev/null @@ -1,185 +0,0 @@ -import {RESOLUTION} from "./Enum/EnvironmentVariable"; - -export class GameScene extends Phaser.Scene { - private player: Phaser.GameObjects.Sprite; - - private keyZ: Phaser.Input.Keyboard.Key; - private keyQ: Phaser.Input.Keyboard.Key; - private keyS: Phaser.Input.Keyboard.Key; - private keyD: Phaser.Input.Keyboard.Key; - private keyRight: Phaser.Input.Keyboard.Key; - private keyLeft: Phaser.Input.Keyboard.Key; - private keyUp: Phaser.Input.Keyboard.Key; - private keyDown: Phaser.Input.Keyboard.Key; - private keyShift: Phaser.Input.Keyboard.Key; - - private Mappy : Phaser.Tilemaps.Tilemap; - - private startX = ((window.innerWidth / 2) / RESOLUTION); - private startY = ((window.innerHeight / 2) / RESOLUTION); - - constructor() { - super({ - key: "GameScene" - }); - } - - preload(): void { - this.load.image('tiles', 'maps/tiles.png'); - this.load.tilemapTiledJSON('map', 'maps/map2.json'); - this.load.spritesheet('player', - 'resources/characters/pipoya/Male 01-1.png', - { frameWidth: 32, frameHeight: 32 } - ); - } - - init(): void { - this.keyShift = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT); - - this.keyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z); - this.keyQ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q); - this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S); - this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D); - - this.keyUp = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP); - this.keyLeft = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT); - this.keyDown = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN); - this.keyRight = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT); - } - - private moveCamera(x:number, y:number, speedMultiplier: number): void { - this.cameras.main.scrollX += speedMultiplier * 2 * x; - this.cameras.main.scrollY += speedMultiplier * 2 * y; - } - - create(): void { - this.Mappy = this.add.tilemap("map"); - let terrain = this.Mappy.addTilesetImage("tiles", "tiles"); - - let bottomLayer = this.Mappy.createStaticLayer("Calque 1", [terrain], 0, 0); - let topLayer = this.Mappy.createStaticLayer("Calque 2", [terrain], 0, 0); - - // Let's manage animations of the player - this.anims.create({ - key: 'down', - frames: this.anims.generateFrameNumbers('player', { start: 0, end: 2 }), - frameRate: 10, - repeat: -1 - }); - - this.anims.create({ - key: 'left', - frames: this.anims.generateFrameNumbers('player', { start: 3, end: 5 }), - frameRate: 10, - repeat: -1 - }); - - this.anims.create({ - key: 'right', - frames: this.anims.generateFrameNumbers('player', { start: 6, end: 8 }), - frameRate: 10, - repeat: -1 - }); - - this.anims.create({ - key: 'up', - frames: this.anims.generateFrameNumbers('player', { start: 9, end: 11 }), - frameRate: 10, - repeat: -1 - }); - - //let player = this.add.sprite(450, 450, 'player'); - //player.anims.play('down'); - //player.setBounce(0.2); - //player.setCollideWorldBounds(true); - this.player = this.add.sprite(this.startX, this.startY, 'player'); - } - - private angle: number = 0; - - update(dt: number): void { - let xCameraPosition = this.cameras.main.scrollX; - let yCameraPosition = this.cameras.main.scrollY; - - let speedMultiplier = this.keyShift.isDown ? 5 : 1; - - if (this.keyZ.isDown || this.keyUp.isDown) { - this.managePlayerAnimation('up'); - if (this.player.y > 0) { - this.player.setY(this.player.y - 2); - } else { - this.player.setY(0); - } - - if (yCameraPosition > 0) { - if (this.player.y < (this.Mappy.widthInPixels - this.startY)) { - this.moveCamera(0, -1, speedMultiplier); - } - } else { - this.cameras.main.scrollY = 0; - } - } else if (this.keyQ.isDown || this.keyLeft.isDown) { - - this.managePlayerAnimation('left'); - if (this.player.x > 0) { - this.player.setX(this.player.x - 2); - } else { - this.player.setX(0); - } - - if (xCameraPosition > 0) { - if (this.player.x < (this.Mappy.heightInPixels - this.startX)) { - this.moveCamera(-1, 0, speedMultiplier); - } - } else { - this.cameras.main.scrollX = 0; - } - } else if (this.keyS.isDown || this.keyDown.isDown) { - - this.managePlayerAnimation('down'); - if (this.Mappy.heightInPixels > this.player.y) { - this.player.setY(this.player.y + 2); - } else { - this.player.setY(this.Mappy.heightInPixels); - } - - if (this.Mappy.heightInPixels > (yCameraPosition + (window.innerHeight / RESOLUTION))) { - if (this.player.y > this.startY) { - this.moveCamera(0, 1, speedMultiplier); - } - } else { - this.cameras.main.scrollY = (this.Mappy.heightInPixels - (window.innerHeight / RESOLUTION)); - } - } else if (this.keyD.isDown || this.keyRight.isDown) { - - this.managePlayerAnimation('right'); - if (this.Mappy.widthInPixels > this.player.x) { - this.player.setX(this.player.x + 2) - } else { - this.player.setX(this.Mappy.widthInPixels) - } - - if (this.Mappy.widthInPixels > (xCameraPosition + (window.innerWidth / RESOLUTION))) { - if (this.player.x > this.startX) { - this.moveCamera(1, 0, speedMultiplier); - } - } else { - this.cameras.main.scrollX = (this.Mappy.widthInPixels - (window.innerWidth / RESOLUTION)); - } - } else { - this.managePlayerAnimation('none'); - } - /*this.cameras.main.scrollX = Math.floor(300 + 300 * Math.cos(this.angle)); - this.cameras.main.scrollY = Math.floor(300 + 300 * Math.sin(this.angle)); - - this.angle = dt * 0.0001;*/ - } - - managePlayerAnimation(direction: string) { - if (!this.player.anims.currentAnim || this.player.anims.currentAnim.key !== direction) { - this.player.anims.play(direction); - } else if (direction === 'none' && this.player.anims.currentAnim) { - this.player.anims.currentAnim.destroy(); - } - } -} diff --git a/front/src/Phaser/CameraManager.ts b/front/src/Phaser/CameraManager.ts new file mode 100644 index 00000000..528e06ca --- /dev/null +++ b/front/src/Phaser/CameraManager.ts @@ -0,0 +1,95 @@ +import {RESOLUTION} from "../Enum/EnvironmentVariable"; +import {Player} from "./Player"; +import {MapManagerInterface} from "./MapManager"; + +export interface CameraManagerInterface { + CurrentPlayer : Player; + MapManager : MapManagerInterface; + moveCamera() : 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 { + //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); + } + } + 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)) + } +} \ No newline at end of file diff --git a/front/src/Phaser/GameScene.ts b/front/src/Phaser/GameScene.ts new file mode 100644 index 00000000..4f2eb9cc --- /dev/null +++ b/front/src/Phaser/GameScene.ts @@ -0,0 +1,35 @@ +import {MapManagerInterface, MapManager} from "./MapManager"; + +export class GameScene extends Phaser.Scene { + private MapManager : MapManagerInterface; + + constructor() { + super({ + key: "GameScene" + }); + } + + //hook preload scene + preload(): void { + this.load.image('tiles', 'maps/tiles.png'); + this.load.tilemapTiledJSON('map', 'maps/map2.json'); + this.load.spritesheet('player', + 'resources/characters/pipoya/Male 01-1.png', + { frameWidth: 32, frameHeight: 32 } + ); + } + + //hook initialisation + init(){}; + + //hook create scene + create(): void { + //create map manager + this.MapManager = new MapManager(this); + } + + //hook update + update(dt: number): void { + this.MapManager.update(); + } +} diff --git a/front/src/Phaser/MapManager.ts b/front/src/Phaser/MapManager.ts new file mode 100644 index 00000000..ecd86210 --- /dev/null +++ b/front/src/Phaser/MapManager.ts @@ -0,0 +1,85 @@ +import {CameraManager, CameraManagerInterface} from "./CameraManager"; +import {RESOLUTION} from "../Enum/EnvironmentVariable"; +import {Player} from "./Player"; + +export interface MapManagerInterface { + keyZ: Phaser.Input.Keyboard.Key; + keyQ: Phaser.Input.Keyboard.Key; + keyS: Phaser.Input.Keyboard.Key; + keyD: Phaser.Input.Keyboard.Key; + keyRight: Phaser.Input.Keyboard.Key; + keyLeft: Phaser.Input.Keyboard.Key; + keyUp: Phaser.Input.Keyboard.Key; + keyDown: Phaser.Input.Keyboard.Key; + keyShift: Phaser.Input.Keyboard.Key; + + Map: Phaser.Tilemaps.Tilemap; + Terrain: Phaser.Tilemaps.Tileset; + Camera: CameraManagerInterface; + update(): void; +} +export class MapManager implements MapManagerInterface{ + keyZ: Phaser.Input.Keyboard.Key; + keyQ: Phaser.Input.Keyboard.Key; + keyS: Phaser.Input.Keyboard.Key; + keyD: Phaser.Input.Keyboard.Key; + keyRight: Phaser.Input.Keyboard.Key; + keyLeft: Phaser.Input.Keyboard.Key; + keyUp: Phaser.Input.Keyboard.Key; + keyDown: Phaser.Input.Keyboard.Key; + keyShift: Phaser.Input.Keyboard.Key; + + Terrain : Phaser.Tilemaps.Tileset; + Camera: CameraManagerInterface; + CurrentPlayer: Player; + Scene: Phaser.Scene; + Map: Phaser.Tilemaps.Tilemap; + startX = (window.innerWidth / 2) / RESOLUTION; + startY = (window.innerHeight / 2) / RESOLUTION; + + constructor(scene: Phaser.Scene){ + 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 keyboard + this.initKeyBoard(); + + //initialise player + this.CurrentPlayer = new Player( + this.Scene, + this.startX, + this.startY, + this + ); + this.CurrentPlayer.initAnimation(); + + //initialise camera + this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this, this.CurrentPlayer); + } + + + initKeyBoard() { + this.keyShift = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT); + + this.keyZ = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z); + this.keyQ = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q); + this.keyS = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S); + this.keyD = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D); + + this.keyUp = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP); + this.keyLeft = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT); + this.keyDown = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN); + this.keyRight = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT); + } + + update() : void { + this.CurrentPlayer.move(); + this.Camera.moveCamera() + } +} \ No newline at end of file diff --git a/front/src/Phaser/Player.ts b/front/src/Phaser/Player.ts new file mode 100644 index 00000000..f76778a4 --- /dev/null +++ b/front/src/Phaser/Player.ts @@ -0,0 +1,90 @@ +import {MapManagerInterface} from "./MapManager"; +import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Player/Animation"; + +export class Player extends Phaser.GameObjects.Sprite{ + MapManager : MapManagerInterface; + PlayerValue : string; + + constructor( + Scene : Phaser.Scene, + x : number, + y : number, + MapManager: MapManagerInterface, + PlayerValue : string = "player" + ) { + super(Scene, x, y, PlayerValue); + this.PlayerValue = PlayerValue; + Scene.add.existing(this); + this.MapManager = MapManager; + } + + + initAnimation(){ + getPlayerAnimations(this.PlayerValue).forEach(d => { + this.scene.anims.create({ + key: d.key, + frames: this.scene.anims.generateFrameNumbers(d.frameModel, { start: d.frameStart, end: d.frameEnd }), + frameRate: d.frameRate, + repeat: d.repeat + }); + }) + } + + move(){ + //if user client on shift, camera and player speed + let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1; + let haveMove = false; + + if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ + if(!this.CanToMoveUp()){ + return; + } + playAnimation(this, PlayerAnimationNames.WalkUp); + this.setY(this.y - (2 * speedMultiplier)); + haveMove = true; + } + if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ + if(!this.CanToMoveLeft()){ + return; + } + playAnimation(this, PlayerAnimationNames.WalkLeft); + this.setX(this.x - (2 * speedMultiplier)); + haveMove = true; + } + if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ + if(!this.CanToMoveDown()){ + return; + } + playAnimation(this, PlayerAnimationNames.WalkDown); + this.setY(this.y + (2 * speedMultiplier)); + haveMove = true; + } + if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ + if(!this.CanToMoveRight()){ + return; + } + playAnimation(this, PlayerAnimationNames.WalkRight); + this.setX(this.x + (2 * speedMultiplier)); + haveMove = true; + } + if(!haveMove){ + playAnimation(this, PlayerAnimationNames.None); + } + } + + private CanToMoveUp(){ + return this.y > 0; + } + + private CanToMoveLeft(){ + return this.x > 0; + } + + private CanToMoveDown(){ + return this.MapManager.Map.heightInPixels > this.y; + } + + private CanToMoveRight(){ + return this.MapManager.Map.widthInPixels > this.x; + } +} \ No newline at end of file diff --git a/front/src/Phaser/Player/Animation.ts b/front/src/Phaser/Player/Animation.ts new file mode 100644 index 00000000..3652a8d1 --- /dev/null +++ b/front/src/Phaser/Player/Animation.ts @@ -0,0 +1,60 @@ +interface AnimationData { + key: string; + frameRate: number; + repeat: number; + frameModel: string; //todo use an enum + frameStart: number; + frameEnd: number; +} + +export enum PlayerAnimationNames { + WalkDown = 'down', + WalkLeft = 'left', + WalkUp = 'up', + WalkRight = 'right', + None = 'none', +}; + +export const getPlayerAnimations = (PlayerValue : string): AnimationData[] => { + return [{ + key: PlayerAnimationNames.WalkDown, + frameModel: PlayerValue, + frameStart: 0, + frameEnd: 2, + frameRate: 10, + repeat: -1 + }, { + key: PlayerAnimationNames.WalkLeft, + frameModel: PlayerValue, + frameStart: 3, + frameEnd: 5, + frameRate: 10, + repeat: -1 + }, { + key: PlayerAnimationNames.WalkRight, + frameModel: PlayerValue, + frameStart: 6, + frameEnd: 8, + frameRate: 10, + repeat: -1 + }, { + key: PlayerAnimationNames.WalkUp, + frameModel: PlayerValue, + frameStart: 9, + frameEnd: 11, + frameRate: 10, + repeat: -1 + }]; +}; + +export const playAnimation = (Player : Phaser.GameObjects.Sprite, direction : string) => { + if (!Player.anims.currentAnim || Player.anims.currentAnim.key !== direction) { + if (direction !== PlayerAnimationNames.None) { + Player.anims.play(direction); + } else if (Player.anims.isPlaying) { + Player.anims.stop(); + } + } else if (direction === PlayerAnimationNames.None && Player.anims.currentAnim) { + Player.anims.currentAnim.destroy(); + } +}; diff --git a/front/src/index.ts b/front/src/index.ts index cf100627..f0f5107d 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -1,6 +1,6 @@ import 'phaser'; import GameConfig = Phaser.Types.Core.GameConfig; -import {GameScene} from "./GameScene"; +import {GameScene} from "./Phaser/GameScene"; import {Connexion} from "./Connexion"; import {RESOLUTION} from "./Enum/EnvironmentVariable"; From bac1e804adc9f10e79ff4cbbbff4aed92f84634a Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 20:41:35 +0200 Subject: [PATCH 2/7] Refactor to include connexion --- front/src/Connexion.ts | 20 ++++++++----- front/src/Enum/EnvironmentVariable.ts | 4 ++- front/src/Phaser/{ => Game}/CameraManager.ts | 4 +-- front/src/Phaser/Game/GameManager.ts | 31 ++++++++++++++++++++ front/src/Phaser/{ => Game}/GameScene.ts | 16 ++++++++-- front/src/Phaser/{ => Game}/MapManager.ts | 10 ++++--- front/src/Phaser/{ => Player}/Player.ts | 18 ++++++++++-- front/src/index.ts | 9 +++--- 8 files changed, 88 insertions(+), 24 deletions(-) rename front/src/Phaser/{ => Game}/CameraManager.ts (96%) create mode 100644 front/src/Phaser/Game/GameManager.ts rename front/src/Phaser/{ => Game}/GameScene.ts (59%) rename front/src/Phaser/{ => Game}/MapManager.ts (91%) rename front/src/Phaser/{ => Player}/Player.ts (83%) diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 4f094152..b27be366 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -1,3 +1,5 @@ +import {GameManagerInterface} from "./Phaser/Game/GameManager"; + const SocketIo = require('socket.io-client'); import Axios from "axios"; import {API_URL} from "./Enum/EnvironmentVariable"; @@ -70,8 +72,11 @@ export class Connexion { email : string; startedRoom : string; - constructor(email : string) { + GameManager: GameManagerInterface; + + constructor(email : string, GameManager: GameManagerInterface) { this.email = email; + this.GameManager = GameManager; Axios.post(`${API_URL}/login`, {email: email}) .then((res) => { this.token = res.data.token; @@ -87,9 +92,8 @@ export class Connexion { this.joinARoom(this.startedRoom); //share your first position - this.sharePosition(0, 0); + this.sharePosition(this.startedRoom, 0, 0); - //create listen event to get all data user shared by the back this.positionOfAllUser(); this.errorMessage(); @@ -114,8 +118,8 @@ export class Connexion { * @param x * @param y */ - sharePosition(x : number, y : number){ - let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(x, y)); + sharePosition(roomId : string, x : number, y : number){ + let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y)); this.socket.emit('user-position', messageUserPosition.toString()); } @@ -135,8 +139,10 @@ export class Connexion { **/ positionOfAllUser(){ this.socket.on("user-position", (message : string) => { - //TODO show all user in map - console.info("user-position", message); + let data = JSON.parse(message); + data.forEach((UserPositions : any) => { + this.GameManager.sharedUserPosition(UserPositions); + }); }); } diff --git a/front/src/Enum/EnvironmentVariable.ts b/front/src/Enum/EnvironmentVariable.ts index 71934e6c..3cf52b85 100644 --- a/front/src/Enum/EnvironmentVariable.ts +++ b/front/src/Enum/EnvironmentVariable.ts @@ -1,7 +1,9 @@ const API_URL = process.env.API_URL || "http://api.workadventure.localhost"; +const ROOM = [process.env.ROOM || "THECODINGMACHINE"]; const RESOLUTION = 2; export { API_URL, - RESOLUTION + RESOLUTION, + ROOM } \ No newline at end of file diff --git a/front/src/Phaser/CameraManager.ts b/front/src/Phaser/Game/CameraManager.ts similarity index 96% rename from front/src/Phaser/CameraManager.ts rename to front/src/Phaser/Game/CameraManager.ts index 528e06ca..829bedf4 100644 --- a/front/src/Phaser/CameraManager.ts +++ b/front/src/Phaser/Game/CameraManager.ts @@ -1,5 +1,5 @@ -import {RESOLUTION} from "../Enum/EnvironmentVariable"; -import {Player} from "./Player"; +import {RESOLUTION} from "../../Enum/EnvironmentVariable"; +import {Player} from "../Player/Player"; import {MapManagerInterface} from "./MapManager"; export interface CameraManagerInterface { diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts new file mode 100644 index 00000000..ff9f5b5f --- /dev/null +++ b/front/src/Phaser/Game/GameManager.ts @@ -0,0 +1,31 @@ +import {GameSceneInterface, GameScene} from "./GameScene"; +import {ROOM} from "../../Enum/EnvironmentVariable" +import {Connexion} from "../../Connexion"; + +export let ConnexionInstance : Connexion; + +export interface GameManagerInterface { + GameScenes: Array; + + sharedUserPosition(UserPositions: any): void; +} +export class GameManager implements GameManagerInterface { + GameScenes: Array = []; + + constructor() { + this.configureGame(); + ConnexionInstance = new Connexion("test@gmail.com", this); + } + + configureGame() { + ROOM.forEach((roomId) => { + let newGame = new GameScene(roomId, this); + this.GameScenes.push(newGame); + }); + } + + sharedUserPosition(UserPositions: any) { + let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === UserPositions.roomId); + Game.sharedUserPosition(UserPositions) + } +} \ No newline at end of file diff --git a/front/src/Phaser/GameScene.ts b/front/src/Phaser/Game/GameScene.ts similarity index 59% rename from front/src/Phaser/GameScene.ts rename to front/src/Phaser/Game/GameScene.ts index 4f2eb9cc..947da7bd 100644 --- a/front/src/Phaser/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1,12 +1,19 @@ import {MapManagerInterface, MapManager} from "./MapManager"; +import {GameManagerInterface} from "./GameManager"; -export class GameScene extends Phaser.Scene { +export interface GameSceneInterface extends Phaser.Scene { + RoomId : string; + sharedUserPosition(data : []): void; +} +export class GameScene extends Phaser.Scene implements GameSceneInterface{ private MapManager : MapManagerInterface; + RoomId : string; - constructor() { + constructor(RoomId : string, GameManager : GameManagerInterface) { super({ key: "GameScene" }); + this.RoomId = RoomId; } //hook preload scene @@ -32,4 +39,9 @@ export class GameScene extends Phaser.Scene { update(dt: number): void { this.MapManager.update(); } + + sharedUserPosition(data: []): void { + //TODO share position of all user + //console.log("sharedUserPosition", data); + } } diff --git a/front/src/Phaser/MapManager.ts b/front/src/Phaser/Game/MapManager.ts similarity index 91% rename from front/src/Phaser/MapManager.ts rename to front/src/Phaser/Game/MapManager.ts index ecd86210..f12a32d1 100644 --- a/front/src/Phaser/MapManager.ts +++ b/front/src/Phaser/Game/MapManager.ts @@ -1,6 +1,7 @@ import {CameraManager, CameraManagerInterface} from "./CameraManager"; -import {RESOLUTION} from "../Enum/EnvironmentVariable"; -import {Player} from "./Player"; +import {RESOLUTION} from "../../Enum/EnvironmentVariable"; +import {Player} from "../Player/Player"; +import {GameScene, GameSceneInterface} from "./GameScene"; export interface MapManagerInterface { keyZ: Phaser.Input.Keyboard.Key; @@ -16,6 +17,7 @@ export interface MapManagerInterface { Map: Phaser.Tilemaps.Tilemap; Terrain: Phaser.Tilemaps.Tileset; Camera: CameraManagerInterface; + Scene: GameSceneInterface; update(): void; } export class MapManager implements MapManagerInterface{ @@ -32,12 +34,12 @@ export class MapManager implements MapManagerInterface{ Terrain : Phaser.Tilemaps.Tileset; Camera: CameraManagerInterface; CurrentPlayer: Player; - Scene: Phaser.Scene; + Scene: GameSceneInterface; Map: Phaser.Tilemaps.Tilemap; startX = (window.innerWidth / 2) / RESOLUTION; startY = (window.innerHeight / 2) / RESOLUTION; - constructor(scene: Phaser.Scene){ + constructor(scene: GameSceneInterface){ this.Scene = scene; //initalise map diff --git a/front/src/Phaser/Player.ts b/front/src/Phaser/Player/Player.ts similarity index 83% rename from front/src/Phaser/Player.ts rename to front/src/Phaser/Player/Player.ts index f76778a4..ebfc19b0 100644 --- a/front/src/Phaser/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -1,12 +1,16 @@ -import {MapManagerInterface} from "./MapManager"; -import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Player/Animation"; +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"; export class Player extends Phaser.GameObjects.Sprite{ MapManager : MapManagerInterface; PlayerValue : string; + Connexion: Connexion; constructor( - Scene : Phaser.Scene, + Scene : GameSceneInterface, x : number, y : number, MapManager: MapManagerInterface, @@ -69,6 +73,14 @@ export class Player extends Phaser.GameObjects.Sprite{ } if(!haveMove){ playAnimation(this, PlayerAnimationNames.None); + }else{ + this.sharePosition(); + } + } + + private sharePosition(){ + if(ConnexionInstance) { + ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y); } } diff --git a/front/src/index.ts b/front/src/index.ts index f0f5107d..650fd52c 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -1,15 +1,16 @@ import 'phaser'; import GameConfig = Phaser.Types.Core.GameConfig; -import {GameScene} from "./Phaser/GameScene"; -import {Connexion} from "./Connexion"; +import {GameManager} from "./Phaser/Game/GameManager"; import {RESOLUTION} from "./Enum/EnvironmentVariable"; +let gameManager = new GameManager(); + const config: GameConfig = { title: "Office game", width: window.innerWidth / RESOLUTION, height: window.innerHeight / RESOLUTION, parent: "game", - scene: [GameScene], + scene: gameManager.GameScenes, zoom: RESOLUTION, }; @@ -18,5 +19,3 @@ let game = new Phaser.Game(config); window.addEventListener('resize', function (event) { game.scale.resize(window.innerWidth / RESOLUTION, window.innerHeight / RESOLUTION); }); - -const connexion = new Connexion("test@gmail.com"); From aba332218881a86eac79250a6669e912d3e0db3f Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 20:46:30 +0200 Subject: [PATCH 3/7] Fix CI --- front/src/Connexion.ts | 3 +++ front/src/Phaser/Game/GameScene.ts | 2 +- front/src/Phaser/Player/Animation.ts | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index b27be366..18bcd6dd 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -119,6 +119,9 @@ export class Connexion { * @param y */ sharePosition(roomId : string, x : number, y : number){ + if(!this.socket){ + return; + } let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y)); this.socket.emit('user-position', messageUserPosition.toString()); } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 947da7bd..d3abfd15 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -27,7 +27,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{ } //hook initialisation - init(){}; + init(){} //hook create scene create(): void { diff --git a/front/src/Phaser/Player/Animation.ts b/front/src/Phaser/Player/Animation.ts index 3652a8d1..38f2afd3 100644 --- a/front/src/Phaser/Player/Animation.ts +++ b/front/src/Phaser/Player/Animation.ts @@ -13,7 +13,7 @@ export enum PlayerAnimationNames { WalkUp = 'up', WalkRight = 'right', None = 'none', -}; +} export const getPlayerAnimations = (PlayerValue : string): AnimationData[] => { return [{ From 67c3eaa7f48533e32c5d46e348a66a4aaa08eb09 Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 21:02:23 +0200 Subject: [PATCH 4/7] Fix Message send to add direction --- back/src/Model/Websocket/MessageUserPosition.ts | 9 ++++++--- back/src/Model/Websocket/PointInterface.ts | 1 + front/src/Connexion.ts | 15 ++++++++++----- front/src/Phaser/Player/Player.ts | 11 ++++++++--- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/back/src/Model/Websocket/MessageUserPosition.ts b/back/src/Model/Websocket/MessageUserPosition.ts index 493f1457..117c99cb 100644 --- a/back/src/Model/Websocket/MessageUserPosition.ts +++ b/back/src/Model/Websocket/MessageUserPosition.ts @@ -4,19 +4,22 @@ import {PointInterface} from "./PointInterface"; export class Point implements PointInterface{ x: number; y: number; + direction: string; - constructor(x : number, y : number) { + constructor(x : number, y : number, direction : string = "none") { if(x === null || y === null){ throw Error("position x and y cannot be null"); } this.x = x; this.y = y; + this.direction = direction; } toJson(){ return { x : this.x, - y: this.y + y: this.y, + direction: this.direction } } } @@ -27,7 +30,7 @@ export class MessageUserPosition extends Message{ constructor(message: string) { super(message); let data = JSON.parse(message); - this.position = new Point(data.position.x, data.position.y); + this.position = new Point(data.position.x, data.position.y, data.position.direction); } toString() { diff --git a/back/src/Model/Websocket/PointInterface.ts b/back/src/Model/Websocket/PointInterface.ts index 7f2ab39a..7b464a5d 100644 --- a/back/src/Model/Websocket/PointInterface.ts +++ b/back/src/Model/Websocket/PointInterface.ts @@ -1,5 +1,6 @@ export interface PointInterface { x: number; y: number; + direction: string; toJson() : object; } \ No newline at end of file diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 18bcd6dd..9c6bde05 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -30,19 +30,22 @@ export class Message { export class Point implements PointInterface{ x: number; y: number; + direction : string; - constructor(x : number, y : number) { + constructor(x : number, y : number, direction : string = "none") { if(x === null || y === null){ throw Error("position x and y cannot be null"); } this.x = x; this.y = y; + this.direction = direction; } toJson(){ return { x : this.x, - y: this.y + y: this.y, + direction: this.direction } } } @@ -114,15 +117,17 @@ export class Connexion { } /** - * Permit to share your position in map + * + * @param roomId * @param x * @param y + * @param direction */ - sharePosition(roomId : string, x : number, y : number){ + sharePosition(roomId : string, x : number, y : number, direction : string = "none"){ if(!this.socket){ return; } - let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y)); + let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y, direction)); this.socket.emit('user-position', messageUserPosition.toString()); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ebfc19b0..f3911041 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -38,6 +38,7 @@ export class Player extends Phaser.GameObjects.Sprite{ //if user client on shift, camera and player speed let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1; let haveMove = false; + let direction = null; if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ if(!this.CanToMoveUp()){ @@ -46,6 +47,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkUp); this.setY(this.y - (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkUp; } if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ if(!this.CanToMoveLeft()){ @@ -54,6 +56,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkLeft); this.setX(this.x - (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkLeft; } if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ if(!this.CanToMoveDown()){ @@ -62,6 +65,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkDown); this.setY(this.y + (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkDown; } if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ if(!this.CanToMoveRight()){ @@ -70,17 +74,18 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkRight); this.setX(this.x + (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkRight; } if(!haveMove){ playAnimation(this, PlayerAnimationNames.None); }else{ - this.sharePosition(); + this.sharePosition(direction); } } - private sharePosition(){ + private sharePosition(direction : string){ if(ConnexionInstance) { - ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y); + ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y, direction); } } From 77780bd27be0c6891fe47b707f79d5c4e3926f4e Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 21:03:33 +0200 Subject: [PATCH 5/7] Change comment with new message strategy --- back/src/Controller/IoSocketController.ts | 3 ++- front/src/Connexion.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index aa5dfdc9..24392f09 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -103,7 +103,8 @@ export class IoSocketController{ roomId: , position: { x : , - y : + y : , + direction: } }, ... diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 9c6bde05..d6f09110 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -139,7 +139,8 @@ export class Connexion { * roomId: , * position: { * x : , - * y : + * y : , + * direction: * } * }, * ... From 25895e51f72b73c89135768adf705eb5dfa56abb Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 22:38:53 +0200 Subject: [PATCH 6/7] Fix and refactor with comments of @moumoug --- front/src/Phaser/Game/CameraManager.ts | 85 +++++++------------------- front/src/Phaser/Game/MapManager.ts | 7 +-- front/src/Phaser/Player/Player.ts | 24 +++++--- 3 files changed, 39 insertions(+), 77 deletions(-) diff --git a/front/src/Phaser/Game/CameraManager.ts b/front/src/Phaser/Game/CameraManager.ts index 829bedf4..b1585542 100644 --- a/front/src/Phaser/Game/CameraManager.ts +++ b/front/src/Phaser/Game/CameraManager.ts @@ -1,95 +1,54 @@ 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; + let limit = { + 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 (!this.CanToMoveUp()) { - this.Camera.scrollY = 0; - }else if (this.CurrentPlayer.y < (this.MapManager.Map.widthInPixels - startY)) { - this.moveCameraPosition(0, -1, speedMultiplier); - } + 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.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(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); } - 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(){ - 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)) } } \ No newline at end of file diff --git a/front/src/Phaser/Game/MapManager.ts b/front/src/Phaser/Game/MapManager.ts index f12a32d1..8b3d9231 100644 --- a/front/src/Phaser/Game/MapManager.ts +++ b/front/src/Phaser/Game/MapManager.ts @@ -52,17 +52,17 @@ export class MapManager implements MapManagerInterface{ //initialise keyboard this.initKeyBoard(); + //initialise camera + this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this); //initialise player this.CurrentPlayer = new Player( this.Scene, this.startX, this.startY, + this.Camera, this ); 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 { this.CurrentPlayer.move(); - this.Camera.moveCamera() } } \ No newline at end of file diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index f3911041..563843fd 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -1,18 +1,19 @@ 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" ) { @@ -20,6 +21,7 @@ export class Player extends Phaser.GameObjects.Sprite{ this.PlayerValue = PlayerValue; Scene.add.existing(this); this.MapManager = MapManager; + this.CameraManager = CameraManager; } @@ -41,7 +43,7 @@ export class Player extends Phaser.GameObjects.Sprite{ let direction = null; if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ - if(!this.CanToMoveUp()){ + if(!this.CanMoveUp()){ return; } playAnimation(this, PlayerAnimationNames.WalkUp); @@ -50,7 +52,7 @@ export class Player extends Phaser.GameObjects.Sprite{ direction = PlayerAnimationNames.WalkUp; } if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ - if(!this.CanToMoveLeft()){ + if(!this.CanMoveLeft()){ return; } playAnimation(this, PlayerAnimationNames.WalkLeft); @@ -59,7 +61,7 @@ export class Player extends Phaser.GameObjects.Sprite{ direction = PlayerAnimationNames.WalkLeft; } if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ - if(!this.CanToMoveDown()){ + if(!this.CanMoveDown()){ return; } playAnimation(this, PlayerAnimationNames.WalkDown); @@ -68,7 +70,7 @@ export class Player extends Phaser.GameObjects.Sprite{ direction = PlayerAnimationNames.WalkDown; } if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ - if(!this.CanToMoveRight()){ + if(!this.CanMoveRight()){ return; } playAnimation(this, PlayerAnimationNames.WalkRight); @@ -81,6 +83,8 @@ export class Player extends Phaser.GameObjects.Sprite{ }else{ this.sharePosition(direction); } + + this.CameraManager.moveCamera(this); } private sharePosition(direction : string){ @@ -89,19 +93,19 @@ export class Player extends Phaser.GameObjects.Sprite{ } } - 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 From 9d83ba22d5feb17a888ab6f201eeae5ffeed3333 Mon Sep 17 00:00:00 2001 From: gparant Date: Tue, 7 Apr 2020 23:56:16 +0200 Subject: [PATCH 7/7] Fix play anim --- front/src/Phaser/Player/Animation.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/front/src/Phaser/Player/Animation.ts b/front/src/Phaser/Player/Animation.ts index 38f2afd3..eb8298f4 100644 --- a/front/src/Phaser/Player/Animation.ts +++ b/front/src/Phaser/Player/Animation.ts @@ -49,11 +49,7 @@ export const getPlayerAnimations = (PlayerValue : string): AnimationData[] => { export const playAnimation = (Player : Phaser.GameObjects.Sprite, direction : string) => { if (!Player.anims.currentAnim || Player.anims.currentAnim.key !== direction) { - if (direction !== PlayerAnimationNames.None) { - Player.anims.play(direction); - } else if (Player.anims.isPlaying) { - Player.anims.stop(); - } + Player.anims.play(direction); } else if (direction === PlayerAnimationNames.None && Player.anims.currentAnim) { Player.anims.currentAnim.destroy(); }