I created an enemy class for my game. It works well, except for one dilemma. When I spawn more than one enemy, eventually they begin to "overlap" and look like one enemy. I was wondering if anyone had any ideas on how to create more differences and spontaneity between the enemies. All help would be greatly appreciated.
Here is the class:
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Defender
{
public class Enemy
{
Vector2 Location;
public static Texture2D texture;
float rotation;
Rectangle rectangle;
int shootTimer = 50;
public void Draw(SpriteBatch spriteBatch)
{
rectangle = new Rectangle((int)Location.X, (int)Location.Y, 32, 32);
spriteBatch.Draw(texture, Location, null, Color.White, rotation, new Vector2(16, 16), 1, SpriteEffects.None, 0);
}
public void Update(GameTime gameTime)
{
Rotate();
Shoot();
Move(gameTime);
}
public void Move(GameTime gameTime)
{
if (Vector2.Distance(Location, Game.player.Location) > 75)
{
Location.X += findDirection(Game.player.Location).X * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 20);
Location.Y += findDirection(Game.player.Location).Y * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 20);
}
}
private void Rotate()
{
float XDistance = Location.X - Game.player.Location.X;
float YDistance = Location.Y - Game.player.Location.Y;
rotation = (float)Math.Atan2(YDistance, XDistance);
}
private Vector2 findDirection(Vector2 target)
{
Vector2 direction = target - Location;
direction.Normalize();
return direction;
}
public void Shoot()
{
if (shootTimer > 0)
{
shootTimer--;
}
if (shootTimer == 0)
{
//find slope
//create bullet
Bullet bullet = new Bullet(Location, findDirection(new Vector2(Game.player.Location.X, Game.player.Location.Y)));
Game.bulletList.Add(bullet);
//reset timer
shootTimer = 50;
}
}
public Enemy(Vector2 loc)
{
Location = loc;
}
}
}