Fix to move the camera only in the map. The camera stops on the border map.
authorgparant <g.parant@thecodingmachine.com>
Mon, 6 Apr 2020 20:55:09 +0000 (22:55 +0200)
committergparant <g.parant@thecodingmachine.com>
Mon, 6 Apr 2020 20:55:09 +0000 (22:55 +0200)
front/src/Enum/EnvironmentVariable.ts
front/src/GameScene.ts
front/src/index.ts

index 0e10e78918f94a3ba9ad9aa406b333c07bd3f477..71934e6c887ef4998892900225a5fe3dd871acc6 100644 (file)
@@ -1,5 +1,7 @@
 const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
+const RESOLUTION = 2;
 
 export {
-    API_URL
+    API_URL,
+    RESOLUTION
 }
\ No newline at end of file
index 2567a1fb2cbba07614958b579d056642c723d35d..59c06cac01b650a50bd9685a299d63cbbbaac4c6 100644 (file)
@@ -1,3 +1,4 @@
+import {RESOLUTION} from "./Enum/EnvironmentVariable";
 
 export class GameScene extends Phaser.Scene {
     private player: Phaser.GameObjects.Sprite;
@@ -12,6 +13,8 @@ export class GameScene extends Phaser.Scene {
     private keyDown: Phaser.Input.Keyboard.Key;
     private keyShift: Phaser.Input.Keyboard.Key;
 
+    private Mappy : Phaser.Tilemaps.Tilemap;
+
     constructor() {
         super({
             key: "GameScene"
@@ -47,11 +50,11 @@ export class GameScene extends Phaser.Scene {
     }
 
     create(): void {
-        let mappy = this.add.tilemap("map");
-        let terrain = mappy.addTilesetImage("tiles", "tiles");
+        this.Mappy = this.add.tilemap("map");
+        let terrain = this.Mappy.addTilesetImage("tiles", "tiles");
 
-        let bottomLayer = mappy.createStaticLayer("Calque 1", [terrain], 0, 0);
-        let topLayer = mappy.createStaticLayer("Calque 2", [terrain], 0, 0);
+        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({
@@ -87,26 +90,43 @@ export class GameScene extends Phaser.Scene {
         //player.setBounce(0.2);
         //player.setCollideWorldBounds(true);
         this.player = this.add.sprite(450, 450, '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.keyUp.isDown) {
-            this.moveCamera(0, -1, speedMultiplier);
+            if(yCameraPosition > 0) {
+                this.moveCamera(0, -1, speedMultiplier);
+            }else {
+                this.cameras.main.scrollY = 0;
+            }
         }
         if (this.keyLeft.isDown) {
-            this.moveCamera(-1, 0, speedMultiplier);
+            if(xCameraPosition > 0) {
+                this.moveCamera(-1, 0, speedMultiplier);
+            }else{
+                this.cameras.main.scrollX = 0;
+            }
         }
         if (this.keyDown.isDown) {
-            this.moveCamera(0, 1, speedMultiplier);
+            if(this.Mappy.heightInPixels > (yCameraPosition + (window.innerHeight / RESOLUTION))) {
+                this.moveCamera(0, 1, speedMultiplier);
+            }else{
+                this.cameras.main.scrollY = (this.Mappy.heightInPixels - (window.innerHeight / RESOLUTION));
+            }
         }
         if (this.keyRight.isDown) {
-            this.moveCamera(1, 0, speedMultiplier);
+            if(this.Mappy.widthInPixels > (xCameraPosition + (window.innerWidth / RESOLUTION))) {
+                this.moveCamera(1, 0, speedMultiplier);
+            }else{
+                this.cameras.main.scrollX = (this.Mappy.widthInPixels - (window.innerWidth / RESOLUTION));
+            }
         }
         
         if (this.keyZ.isDown) {
index 9c7c0e8ae7126c472510c7e0720d4c22f9d998a8..cf1006278176500f243fa4400b771bf2f42275bc 100644 (file)
@@ -2,22 +2,21 @@ import 'phaser';
 import GameConfig = Phaser.Types.Core.GameConfig;
 import {GameScene} from "./GameScene";
 import {Connexion} from "./Connexion";
-
-const resolution = 2;
+import {RESOLUTION} from "./Enum/EnvironmentVariable";
 
 const config: GameConfig = {
     title: "Office game",
-    width: window.innerWidth / resolution,
-    height: window.innerHeight / resolution,
+    width: window.innerWidth / RESOLUTION,
+    height: window.innerHeight / RESOLUTION,
     parent: "game",
     scene: [GameScene],
-    zoom: resolution,
+    zoom: RESOLUTION,
 };
 
 let game = new Phaser.Game(config);
 
 window.addEventListener('resize', function (event) {
-    game.scale.resize(window.innerWidth / resolution, window.innerHeight / resolution);
+    game.scale.resize(window.innerWidth / RESOLUTION, window.innerHeight / RESOLUTION);
 });
 
 const connexion = new Connexion("test@gmail.com");