So, I’ve been trying to figure out aggro, pathing and such the past few days, and I finally figured out how to get the enemy to break patrol, chase the player if they get to close and return to their patrolling pattern if they player escapes the aggro radius with tweens!
The only problem is that I feel like I could have better code for this, and was wanting some critique on what I am using. I’d appreciate any and all feedback / suggestions
let distanceFromPlayer = Math.sqrt(Math.pow((scene.player.y-this.y) , 2) + Math.pow((scene.player.x-this.x), 2));
if ((distanceFromPlayer < scene.player.aggroRadius))
{
//slime is aggroed
scene.tweens.add(
{
targets: this,
props:
{
y: { value: scene.player.y},
x: { value: scene.player.x}
},
yoyo: true,
repeat: -1
});
this._aggroed = true;
this.patrolling = false;
}else{
//slime is not aggroed
if (this.x == this.xSpawnCoordinate && this.y == this.ySpawnCoordinate && this.returningHome == true)
{
//slime has returned home
scene.tweens.killTweensOf(this);
scene.tweens.add(
{
targets: this,
props: this.tweenXY,
yoyo: true,
repeat: -1
});
this.returningHome = false;
this.patrolling = true;
}else{
//send slime home after losing aggro
if (this.patrolling == false && this._aggroed == true)
{
scene.tweens.add(
{
targets: this,
props:
{
y: { value: this.ySpawnCoordinate, duration: 2000},
x: { value: this.xSpawnCoordinate, duration: 2000}
},
yoyo: true,
repeat: -1
});
this._aggroed = false;
this.returningHome = true;
}
}