the other playes now run away from the player on contact
This commit is contained in:
parent
97a55ab66c
commit
05379c8001
@ -13,4 +13,20 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
|||||||
this.setImmovable(true);
|
this.setImmovable(true);
|
||||||
this.setCollideWorldBounds(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {GameManagerInterface} from "./GameManager";
|
import {GameManagerInterface} from "./GameManager";
|
||||||
import {UserInputManager} from "../UserInput/UserInputManager";
|
import {UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
|
||||||
import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
|
import {getPlayerAnimations, PlayerAnimationNames} from "../Player/Animation";
|
||||||
import {Player} from "../Player/Player";
|
import {Player} from "../Player/Player";
|
||||||
import {NonPlayer} from "../NonPlayer/NonPlayer";
|
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, 200, 600));
|
||||||
this.otherPlayers.add(new NonPlayer(this, 400, 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
|
//create map
|
||||||
let currentMap = this.add.tilemap(Textures.Map);
|
let currentMap = this.add.tilemap(Textures.Map);
|
||||||
@ -101,13 +104,31 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
|||||||
|
|
||||||
//hook update
|
//hook update
|
||||||
update(dt: number): void {
|
update(dt: number): void {
|
||||||
let eventList = this.userInputManager.getEventListForGameTick();
|
//user inputs
|
||||||
|
let activeEvents = this.userInputManager.getEventListForGameTick();
|
||||||
|
let speed = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100;
|
||||||
|
|
||||||
this.player.move(eventList);
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//updates other players
|
||||||
this.otherPlayers.getChildren().forEach((otherPlayer: NonPlayer) => {
|
this.otherPlayers.getChildren().forEach((otherPlayer: NonPlayer) => {
|
||||||
//this.physics.accelerateToObject(otherPlayer, this.player); //this line make the models chase the player
|
//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);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,30 @@
|
|||||||
import {PlayableCaracter} from "../Entity/PlayableCaracter";
|
import {PlayableCaracter} from "../Entity/PlayableCaracter";
|
||||||
import {Textures} from "../Game/GameScene";
|
import {Textures} from "../Game/GameScene";
|
||||||
|
import {UserInputEvent} from "../UserInput/UserInputManager";
|
||||||
|
import {Player} from "../Player/Player";
|
||||||
|
|
||||||
export class NonPlayer extends PlayableCaracter {
|
export class NonPlayer extends PlayableCaracter {
|
||||||
|
|
||||||
|
isFleeing: boolean = false;
|
||||||
|
fleeingDirection:any = null //todo create a vector class
|
||||||
|
|
||||||
constructor(scene: Phaser.Scene, x: number, y: number) {
|
constructor(scene: Phaser.Scene, x: number, y: number) {
|
||||||
super(scene, x, y, Textures.Player, 1);
|
super(scene, x, y, Textures.Player, 1);
|
||||||
this.setSize(32, 32); //edit the hitbox to better match the caracter model
|
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}
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,34 +12,6 @@ export class Player extends PlayableCaracter{
|
|||||||
this.setSize(32, 32); //edit the hitbox to better match the caracter model
|
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() {
|
stop() {
|
||||||
this.setVelocity(0, 0)
|
this.setVelocity(0, 0)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user