Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Advice on bullets? [SOLVED]  (Read 2090 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Advice on bullets? [SOLVED]
« on: October 31, 2011, 03:58:22 pm »
Ok folks, I am having a hard time coming up with an efficient bullet system.

My problem isn't with the creation, display, or movement of the bullets--those are easy. However, I am having a hard time with destroying them when their life is over (i.e. they have gone out of screen or they have hit an object and must disappear).

I have been using a vector to contain my bullet sprites, but removing a specific bullet sprite from wherever it happens to reside in that vector has become a problem.

Does anyone have a recommendation for a good container and/or system to use for managing bullets?

If another container is suggested, I would also appreciate any simple code examples you are willing to give. Though, I am also willing to research it myself with whatever info is provided!

Thank you!
Brock

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Advice on bullets? [SOLVED]
« Reply #1 on: October 31, 2011, 04:49:27 pm »
To avoid time consuming creation-insertion(copying to a container)-removal of big number of objects you may use a pool of pre-created objects (bullets in your case). Instead of removal a bullet when its life expires you just make it invisible and place its index (in case of using vector) in a stack of available bullets. When you need a new bullet you get the first available index from the stack, initialize the bullet with appropriate arguments (position, velosity, etc.) and make the bullet visible. I gave you just a bare idea. I think you can find a lot of information and tutorials on using pools in the Internet.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Advice on bullets? [SOLVED]
« Reply #2 on: October 31, 2011, 11:54:38 pm »
Relic, I definitely appreciate that suggestion, and I took it into consideration.

However, I had been trying so hard to figure out a create/destroy method for my bullets, that I wasn't going to be satisfied until I at least figured out a good way to do it. And I finally have.

Here is the solution I came up with, and hopefully this can be helpful to others in their games since bullets are such a prevalent part of gaming.

First I have a vector to store all of my Laser sprites called "laserbox".

Within the Laser class constructor, each laser sets its position to be the front tip of the space ship. As soon as the laser is created it begins moving upward based on its speed and movement functionality which is a different topic.

When the left mouse button is clicked, a new Laser sprite is create and added to the laserbox vector.
Code: [Select]
case sf::Event::MouseButtonPressed:
    if(event.MouseButton.Button == sf::Mouse::Left){
        laserbox.push_back(Laser(this));
    } break;


Then, inside the game's main loop, this for loop is called. It calls each laser's Update function first, which moves the sprite and checks to see if the laser is still Alive, then it calls Window.Draw() to draw each laser to the screen.

Secondly, and this has been the tricky part for me, it destroys the lasers if they are no longer "Alive". Each laser has a bool member called Alive, and if gets switched to false by its Update() function as soon as its position is out of the screen and/or it comes into contact with a space-rock.

Code: [Select]
for(std::vector<Laser>::iterator iter = laserbox.begin(); iter != laserbox.end(); ){
    iter->Update();
    Window.Draw(*iter);
    if(iter->Alive == false)
        iter = laserbox.erase(iter);
    else
        ++iter;
}


I hope this code can be helpful if anyone else comes across this need. It seems so simple now, but it solves a big problem for me.

 

anything