/// <summary>
/// Update to chase the player.
/// </summary>
/// <param name="playerPos">The position of player. Basically the direction it's heading.</param>
public void Update(Vector2 playerPos, GameTime gameTime)
{
distance = new Vector2(playerPos.X - position.X, playerPos.Y - position.Y);
speed.X = (playerPos.X - position.X) * speedLength / distance.Length();
speed.Y = (playerPos.Y - position.Y) * speedLength / distance.Length();
timeSinceLastSwitch += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastSwitch > millisecondPerSwitch)
{
timeSinceLastSwitch -= millisecondPerSwitch;
isChasing = !isChasing;
}
if (isChasing)
{
speed *= 1; // chase
}
else
{
speed *= -0.5f; // evade
}
// Prevent EvadingEnemies from moving out of window
if (position.X < 0) position.X = 0;
if (position.Y < 0) position.Y = 0;
if (position.X > windowWidth)
position.X = windowWidth;
if (position.Y > windowHeight)
position.Y = windowHeight;
base.Update();
}