Only sending move events if the player actually moved
authorDavid Négrier <d.negrier@thecodingmachine.com>
Mon, 4 May 2020 21:11:59 +0000 (23:11 +0200)
committerDavid Négrier <d.negrier@thecodingmachine.com>
Mon, 4 May 2020 21:11:59 +0000 (23:11 +0200)
If the player did not move a pixel (and if it did not change direction), then do not send an event to save bandwidth and processing.

front/src/Phaser/Entity/PlayableCaracter.ts
front/src/Phaser/Player/Player.ts

index 02e5a08bd3e5ab0be9371997598e783fa1d5ffe1..f905d33f2b5470b9bec84362b15b467de9f05382 100644 (file)
@@ -50,7 +50,7 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
 
     stop(){
         this.setVelocity(0, 0);
-        this.play(PlayerAnimationNames.None, true);
+        this.anims.stop();
     }
 
     say(text: string) {
index d13feed4d677e98597e0719ea5822c2161ab079c..80eeb275bec9228f6927c7a08d81d7773ca60bad 100644 (file)
@@ -62,7 +62,6 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
 
     moveUser(delta: number): void {
         //if user client on shift, camera and player speed
-        let haveMove = false;
         let direction = null;
 
         let activeEvents = this.userInputManager.getEventListForGameTick();
@@ -87,12 +86,18 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
         }
         if (x !== 0 || y !== 0) {
             this.move(x, y);
+            this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
         } else {
-            direction = PlayerAnimationNames.None;
-            this.stop();
+            if (this.previousMove !== PlayerAnimationNames.None) {
+                direction = PlayerAnimationNames.None;
+                this.stop();
+                this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
+            }
         }
 
-        this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
+        if (direction !== null) {
+            this.previousMove = direction;
+        }
     }
 
     //todo: put this method into the NonPlayer class instead