the other playes now run away from the player on contact
authorkharhamel <oognic@gmail.com>
Sun, 12 Apr 2020 17:06:31 +0000 (19:06 +0200)
committerkharhamel <oognic@gmail.com>
Sun, 12 Apr 2020 17:06:31 +0000 (19:06 +0200)
front/src/Phaser/Entity/PlayableCaracter.ts
front/src/Phaser/Game/GameScene.ts
front/src/Phaser/NonPlayer/NonPlayer.ts
front/src/Phaser/Player/Player.ts

index 8456ed44a535643d1ee552ab6bc850dd70f93296..789b247583122ffc493e5452e9244dbc9b00b702 100644 (file)
@@ -13,4 +13,20 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
         this.setImmovable(true);
         this.setCollideWorldBounds(true)
     }
+
+    move(x: number, y: number){
+
+        this.setVelocity(x, y);
+
+        //todo improve animations to better account for diagonal movement
+        if (this.body.velocity.x > 0) { //moving right
+            this.play(PlayerAnimationNames.WalkRight, true);
+        } else if (this.body.velocity.x < 0) { //moving left
+            this.anims.playReverse(PlayerAnimationNames.WalkLeft, true);
+        } else if (this.body.velocity.y < 0) { //moving up
+            this.play(PlayerAnimationNames.WalkUp, true);
+        } else if (this.body.velocity.y > 0) { //moving down
+            this.play(PlayerAnimationNames.WalkDown, true);
+        }
+    }
 }
\ No newline at end of file
index 648e6579664e35bff318fa6a124ea279dc9fc2ef..112e452c6f365091a833fb294ff3fab68d5fbf97 100644 (file)
@@ -1,5 +1,5 @@
 import {GameManagerInterface} from "./GameManager";
-import {UserInputManager} from "../UserInput/UserInputManager";
+import {UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
 import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
 import {Player} from "../Player/Player";
 import {NonPlayer} from "../NonPlayer/NonPlayer";
@@ -64,7 +64,10 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
         this.otherPlayers.add(new NonPlayer(this, 200, 600));
         this.otherPlayers.add(new NonPlayer(this, 400, 600));
 
-        this.physics.add.collider(this.player, this.otherPlayers);
+        this.physics.add.collider(this.player, this.otherPlayers, (player: Player, otherPlayer: NonPlayer) => {
+            console.log("Don't touch me!");
+            otherPlayer.fleeFrom(player)
+        });
         
         //create map
         let currentMap = this.add.tilemap(Textures.Map);
@@ -101,13 +104,31 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
 
     //hook update
     update(dt: number): void {
-        let eventList = this.userInputManager.getEventListForGameTick();
+        //user inputs
+        let activeEvents = this.userInputManager.getEventListForGameTick();
+        let speed = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100;
+
+        if(activeEvents.get(UserInputEvent.MoveUp)){
+            this.player.move(0, -speed)
+        } else if(activeEvents.get(UserInputEvent.MoveLeft)){
+            this.player.move(-speed, 0)
+        } else if(activeEvents.get(UserInputEvent.MoveDown)){
+            this.player.move(0, speed)
+        } else if(activeEvents.get(UserInputEvent.MoveRight)){
+            this.player.move(speed, 0)
+        } else {
+            this.player.move(0, 0)
+        }
         
-        this.player.move(eventList);
         
+        //updates other players
         this.otherPlayers.getChildren().forEach((otherPlayer: NonPlayer) => {
             //this.physics.accelerateToObject(otherPlayer, this.player); //this line make the models chase the player
-            otherPlayer.setVelocity(20, 5);
+            if (otherPlayer.isFleeing) {
+                otherPlayer.move(otherPlayer.fleeingDirection.x, otherPlayer.fleeingDirection.y);
+            } else {
+                otherPlayer.move(0, 0);
+            }
         })
     }
 
index 46fa645083c62db188a546cd926497c39ee6345e..91b83ba37a3c4bf1aea968b80fce1e6b5d1b0675 100644 (file)
@@ -1,10 +1,30 @@
 import {PlayableCaracter} from "../Entity/PlayableCaracter";
 import {Textures} from "../Game/GameScene";
+import {UserInputEvent} from "../UserInput/UserInputManager";
+import {Player} from "../Player/Player";
 
 export class NonPlayer extends PlayableCaracter {
     
+    isFleeing: boolean = false;
+    fleeingDirection:any = null //todo create a vector class
+    
     constructor(scene: Phaser.Scene, x: number, y: number) {
         super(scene, x, y, Textures.Player, 1);
         this.setSize(32, 32); //edit the hitbox to better match the caracter model
     }
+
+    fleeFrom(player:Player) {
+        if (this.isFleeing) return;
+        this.isFleeing = true;
+        
+        setTimeout(() => {
+            console.log("I escaped");
+            this.isFleeing = false
+            this.fleeingDirection = null
+        }, 3000);
+        
+        let vectorX = this.x - player.x; 
+        let vectorY = this.y - player.y;
+        this.fleeingDirection = {x: vectorX, y: vectorY}
+    }
 }
\ No newline at end of file
index ff2278f5b19c83ab5ec06c3c67992b4d8fc57a4c..8c9b462bb63ccfd683c940712d1bb6fa5f1d5ebe 100644 (file)
@@ -12,34 +12,6 @@ export class Player extends PlayableCaracter{
         this.setSize(32, 32); //edit the hitbox to better match the caracter model
     }
 
-    move(activeEvents: ActiveEventList){
-        let speed = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100;
-        let haveMove = false;
-        let direction = null;
-
-        if(activeEvents.get(UserInputEvent.MoveUp)){
-            this.setVelocity(0, -speed)
-        } else if(activeEvents.get(UserInputEvent.MoveLeft)){
-            this.setVelocity(-speed, 0)
-        } else if(activeEvents.get(UserInputEvent.MoveDown)){
-            this.setVelocity(0, speed)
-        } else if(activeEvents.get(UserInputEvent.MoveRight)){
-            this.setVelocity(speed, 0)
-        } else {
-            this.setVelocity(0, 0)
-        }
-
-        if (this.body.velocity.x > 0) { //moving right
-            this.play("right", true);
-        } else if (this.body.velocity.x < 0) { //moving left
-            this.anims.playReverse("left", true);
-        } else if (this.body.velocity.y < 0) { //moving up
-            this.play("up", true);
-        } else if (this.body.velocity.y > 0) { //moving down
-            this.play("down", true);
-        }
-    }
-
     stop() {
         this.setVelocity(0, 0)
     }