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
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";
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);
//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);
+ }
})
}
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
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)
}