Implementing a reload function

I am making a tank game, and I am trying to implement a reload function. What I have now is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void Turret::shoot(double deltaTime)
{
	if(timeSinceLastFired > cooldown && shotLimit > 0)
	{
		Shot* shot = new Shot();
		shots.push_front(shot);
		shot->setPos(sprite.GetPos() + direction * bulletOffset);
		shot->SetDirection(direction);
		timeSinceLastFired = 0;
		shotLimit -=1;
		reload = deltaTime + 2.0f;
	}
}
void Turret::Update(double deltaTime)
{
	timeSinceLastFired += deltaTime;
	ShotList::iterator s = shots.begin();
	while(s!= shots.end())
	{
		(*s)->Update(deltaTime);
		if((*s)->GetAge() >= bulletTimeLimit)
		{
			delete (*s);
			s = shots.erase(s);
		}
		else ++s;
	}
	if(deltaTime > reload && shotLimit < 5)
	{
		shotLimit = 5;
	}
}


Before, I had a logic check that allowed it to check if the list was under 5, and if it was, it would allow me to fire. This became a problem when I started colliding the bullets with other tanks and deleting them, it allowed me to rapid fire into the other tank. I redid the code like this, following this line of logic:

When a bullet is fired, 1 is subtracted from the bullet allowance and deltaTime + 2 is set as the value for reload. When the next bullet is going to be fired, check if the bullet allowance is greater than zero and if it is, fire. When deltaTime is greater than reload, set the bullet allowance back to 5, and allow firing again.

If I set a break point on the reload part of the code, it hits it after the elapsed time has passed, and if I hit continue, it will allow me to shoot again, so I really don't know what is going wrong here.

The problem is that my tank stops firing after 5 shots are fired, no matter how. I can post any more code that you would like to see, but I need some help fairly urgently.
I fixed it by looking at how I did my cooldown and implementing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void Turret::shoot(double deltaTime)
{
	if(timeSinceLastFired > cooldown && shotLimit > 0)
	{
		Shot* shot = new Shot();
		shots.push_front(shot);
		shot->setPos(sprite.GetPos() + direction * bulletOffset);
		shot->SetDirection(direction);
		timeSinceLastFired = 0;
		shotLimit -=1;
		reload = 0;
	}
}
void Turret::Update(double deltaTime)
{
	timeSinceLastFired += deltaTime;
	reload +=deltaTime;
	ShotList::iterator s = shots.begin();
	while(s!= shots.end())
	{
		(*s)->Update(deltaTime);
		if((*s)->GetAge() >= bulletTimeLimit)
		{
			delete (*s);
			s = shots.erase(s);
		}
		else ++s;
	}
	if(reload > 2.5f)
	{
		shotLimit = 5;
		reload = 0;
	}
}
Topic archived. No new replies allowed.