From: kharhamel Date: Sun, 12 Apr 2020 17:06:31 +0000 (+0200) Subject: the other playes now run away from the player on contact X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=05379c800190adabddda3003ba65630a8c21cdf3;p=libreadventure.git the other playes now run away from the player on contact --- diff --git a/front/src/Phaser/Entity/PlayableCaracter.ts b/front/src/Phaser/Entity/PlayableCaracter.ts index 8456ed4..789b247 100644 --- a/front/src/Phaser/Entity/PlayableCaracter.ts +++ b/front/src/Phaser/Entity/PlayableCaracter.ts @@ -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 diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 648e657..112e452 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -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); + } }) } diff --git a/front/src/Phaser/NonPlayer/NonPlayer.ts b/front/src/Phaser/NonPlayer/NonPlayer.ts index 46fa645..91b83ba 100644 --- a/front/src/Phaser/NonPlayer/NonPlayer.ts +++ b/front/src/Phaser/NonPlayer/NonPlayer.ts @@ -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 diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ff2278f..8c9b462 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -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) }