Hi Monogamers,
I am looking for some help regarding having enemies follow player when the enemy gets to a certain radius.
what would be the best way to implement a simple AI for the enemy class? or more like when the player is in range of enemy, enemy will follow player for x amount of time/distance?
Here is my enemy class:
`
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace rpg
{
class Enemy
{
private Vector2 position;
protected int health;
protected int speed;
protected int radius;
protected int state;
public static List<Enemy> enemies = new List<Enemy>();
public int Health
{
get { return health; }
set { health = value; }
}
public Vector2 Position
{
get { return position; }
}
public int Radius
{
get { return radius; }
}
public Enemy(Vector2 newPos)
{
position = newPos;
}
public void Update(GameTime gameTime, Vector2 playerPos)
{
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
Vector2 moveDir = playerPos - position;
moveDir.Normalize();
//position += moveDir * speed * dt;
distanceToPlayer = ;
int state = 0;
if (state == 0)
{
// Everything the enemy does during "idle"
if (distanceToPlayer < 600)
{
state = 1;
}
}
else if (state == 1)
{
// Everything the enemy does during "following"
position += moveDir * speed * dt; // move enemy towards player
}
}
}
class Snake : Enemy
{
public Snake(Vector2 newPos) : base(newPos)
{
speed = 160;
radius = 42;
health = 3;
//state = 0;
}
}
class Eye : Enemy
{
public Eye(Vector2 newPos) : base(newPos)
{
speed = 80;
radius = 45;
health = 5;
//state = 0;
}
}
}
`