I am developing a top down shooter. The character is already shooting fireballs but in my opionion the game is to easy so I want to add a cooldown time .
How can I do that?
Below I leave my projectile class
public class Projectile : Basic2D
{
protected bool done;
protected float speed;
protected Vector2 direction;
protected Unit owner;
protected _Timer timer;
public float distanceTravelled = 250.0f;
public Projectile(string _path, Vector2 _position, Vector2 _dimensions, Unit _owner, Vector2 _target) : base(_path, _position, _dimensions)
{
done = false;
owner = _owner;
direction = _target - owner.GetPosition();
direction.Normalize(); //makes lenght 1
SetRotation(Globals.RotateTowards(GetPosition(), new Vector2(_target.X, _target.Y)));
timer = new _Timer(1200);
}
public bool IsDone()
{
return done;
}
public virtual void Update(Vector2 _offset, List<Unit> _units)
{
SetPosition(GetPosition() + (direction * speed));
timer.UpdateTimer();
if(timer.Test())
{
done = true;
}
if(CheckCollision(_units) || OutOfRange())
{
done = true;
//owner.SetProjectilesOnScreen(owner.GetProjectilesOnScreen() - 1);
}
}
public virtual bool CheckCollision(List<Unit> _units)
{
for(int i = 0; i < _units.Count; i++)
{
if (Vector2.Distance(GetPosition(), _units[i].GetPosition())< owner.GetAttackArea())
{
_units[i].GetHit(owner.GetDamage());
return true;
}
}
return false;
}
public virtual bool OutOfRange()
{
if(Vector2.Distance(GetPosition(), owner.GetPosition()) > owner.GetRange())
{
return true;
}
return false;
}
}
public class _Timer
{
public bool goodToGo;
protected int mSec;
protected TimeSpan timer = new TimeSpan();
public _Timer(int m)
{
goodToGo = false;
mSec = m;
}
public void UpdateTimer()
{
timer += Globals.gameTime.ElapsedGameTime;
}
public bool Test()
{
if(timer.TotalMilliseconds >= mSec || goodToGo)
{
return true;
}
else
{
return false;
}
}
public void ResetToZero()
{
timer = TimeSpan.Zero;
goodToGo = false;
}
public virtual void AddToTimer(int MSEC)
{
timer += TimeSpan.FromMilliseconds((long)(MSEC));
}
}