8 Replies - 304 Views - Last Post: 09 February 2013 - 08:19 PM Rate Topic: -----

#1 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Implementing a reload function

Posted 09 February 2013 - 01:19 AM

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

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.

Is This A Good Question/Topic? 1
  • +

Replies To: Implementing a reload function

#2 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Re: Implementing a reload function

Posted 09 February 2013 - 03:57 AM

I fixed it. I took a look at how I was doing my cooldown, and came up with this:
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;
	}
}

Was This Post Helpful? 1
  • +
  • -

#3 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Re: Implementing a reload function

Posted 09 February 2013 - 05:12 PM

Took it one step farther, this is why I don't code at 4 AM.
void Turret::shoot()
{
	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;
	}
}
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(TimeSinceLastFired> 2.5f)
	{
		shotLimit = 5;
	}
}

Shoot didn't need deltaTime because reload didn't need to exist. TimeSinceLastFired kept a count of how long it had been, so I could check it to see if it was time to reload yet.
Was This Post Helpful? 1
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Implementing a reload function

Posted 09 February 2013 - 05:54 PM

I see you like talking to yourself on threads! :D

Not bad code at all. I'm very weary of using the STL is time-critical sections though, because it's slow.
Was This Post Helpful? 1
  • +
  • -

#5 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Re: Implementing a reload function

Posted 09 February 2013 - 06:05 PM

View PostButchDean, on 09 February 2013 - 05:54 PM, said:

I see you like talking to yourself on threads! :D/>

Not bad code at all. I'm very weary of using the STL is time-critical sections though, because it's slow.

Well, you know. Nobody else is replying x) I thought maybe my thought process might help someone.
I've just started coding a couple months ago, so I will not have the cleanest code, but for the small overhead tank game I am making right now, STL works just fine for it.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Implementing a reload function

Posted 09 February 2013 - 07:19 PM

That's very good for two months. Keep it up!
Was This Post Helpful? 1
  • +
  • -

#7 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Re: Implementing a reload function

Posted 09 February 2013 - 08:09 PM

View PostButchDean, on 09 February 2013 - 07:19 PM, said:

That's very good for two months. Keep it up!

Thanks very much! I have a great teacher, so I am learning a lot.
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Implementing a reload function

Posted 09 February 2013 - 08:15 PM

Lovely -- threads that answer themselves. ;)

Very nice effort on that game, Enchant!
Was This Post Helpful? 1
  • +
  • -

#9 Enchant  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 09-February 13

Re: Implementing a reload function

Posted 09 February 2013 - 08:19 PM

View PostAdak, on 09 February 2013 - 08:15 PM, said:

Lovely -- threads that answer themselves. ;)/>

Very nice effort on that game, Enchant!

Thanks, Adak! I'm working on it right now. I keep getting stumped, but I'm having a lot of fun.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1