I want to have two type of projectiles in my videogame, one that only hits one enemy and another one that hits all enemies that are within the attack area of the character. But I have a problem. If for example I set the attack area of my as 1000f the enemies are simply killed and the projectiles never drawn. Bellow I leave my classes
public class Projectile : Basic2D
{
protected bool done; //the projectile travelled all the character range or hitted an enemy
protected float speed; //speed in which the projectile will travel
protected Vector2 direction;
protected Unit owner; //Unit that shot the projectile
protected Vector2 originalPosition; //position where the projectile was shot
public Projectile(string _path, Vector2 _position, Vector2 _dimensions, Unit _owner, Vector2 _target) : base(_path, _position, _dimensions)
{
done = false;
owner = _owner;
direction = _target - owner.GetPosition();
originalPosition = _position;
direction.Normalize(); //makes lenght 1
SetRotation(RotateTowards(GetPosition(), new Vector2(_target.X, _target.Y)));
}
public bool IsDone()
{
return done;
}
public virtual void Update(Vector2 _offset, List<Enemy> _enemies, Character _character)
{
SetPosition(GetPosition() + (direction * speed));
if (CheckCollision(_enemies, _character) || OutOfRange())
{
done = true;
}
}
public virtual bool CheckCollision(List<Enemy> _enemies, Character _character)
{
for (int i = 0; i < _enemies.Count; i++) //will go trough the list of enemies
{
//if the Unit who shot the projectile and the target are foes
//AND if the target is in range of the owner of the projectile attack area
if (Vector2.Distance(GetPosition(), _enemies[i].GetPosition()) < owner.GetAttackArea() && owner.GetOwnerId() != _enemies[i].GetOwnerId() )
{
//if an enemy is hit there was a collision .This implies that the variable done will be set as true
//The fact that the return is inside the if condition implies that this projectile will only affect one enemy
_enemies[i].GetHit(owner.GetDamage());
return true;
}
}
if (owner.GetOwnerId() != _character.GetOwnerId() && Vector2.Distance(GetPosition(), _character.GetPosition()) < owner.GetAttackArea())
{
_character.GetHit(owner.GetDamage());
return true;
}
return false;
}
public virtual bool OutOfRange()
{
if (Vector2.Distance(GetPosition(), originalPosition) > owner.GetRange())
{
return true;
}
return false;
}
public override void Draw(Vector2 _offset)
{
base.Draw(_offset);
}
}
public class Fireball : Projectile
{
public Fireball(Vector2 _position, Unit _owner, Vector2 _target) : base("2D\\Projectiles\\fireball", _position, new Vector2(40, 40), _owner, _target)
{
speed = 5.00f;
}
public override void Update(Vector2 _offset, List<Enemy> _enemies, Character _character)
{
base.Update(_offset, _enemies, _character);
}
//We intend that a fireball has a slightly different behavior than the other projectiles.
//In other words , we want the fireball to affect all enemies within range
public override bool CheckCollision(List<Enemy> _enemies, Character _character)
{
bool hitted = false;
int count = 0;
for (int i = 0; i < _enemies.Count; i++)
{
//if the Unit who shot the projectile and the target are foes and if the target is in range of the owner of the projectile attack area
if (owner.GetOwnerId() != _enemies[i].GetOwnerId() && Vector2.Distance(GetPosition(), _enemies[i].GetPosition()) < owner.GetAttackArea())
{
_enemies[i].GetHit(owner.GetDamage());
hitted = true;
count++;
}
}
if (count > 0)
{
Debug.WriteLine("THIS PROJECTILE HITTED : " + count);
}
if (owner.GetOwnerId() != _character.GetOwnerId() && Vector2.Distance(GetPosition(), _character.GetPosition()) < owner.GetAttackArea())
{
_character.GetHit(owner.GetDamage());
hitted = true;
}
return hitted;
}
public override void Draw(Vector2 _offset)
{
base.Draw(_offset);
}
}
This is where I update and draw my projectiles
public void Update()
{
//update projectiles
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Update(offset, enemies,character);
if (projectiles[i].IsDone())
{
projectiles.RemoveAt(i);
}
}
public void Draw()
{
//draw the projectiles
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(offset);
}
}