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

Author Topic: How to go about creating a bullet class in SFML 2.0  (Read 5893 times)

0 Members and 1 Guest are viewing this topic.

jonnyprogrammer123

  • Guest
How to go about creating a bullet class in SFML 2.0
« on: May 28, 2013, 07:15:51 am »
Hi,

This will be my first post on these forums. You see, I am a YOUNG game developer, aged 13, who has been dabbling in C++ for the past 2 years. Only around a month ago was I introduced to SFML; The power of graphics has blown me away. Now that I can utilize graphical elements, I can finally achieve what I've always dreamed of doing: Creating an open-ended, do-whatever-you-want RPG. I currently have a relatively simple engine rolling, with simple sprite animation, 3 items, an inventory, collision, views, character creation, and other generally simple aspects. Now that I have basic movement and items down, I have proceeded to attempt to add in "enemies." But before I should tackle that idea, I would desire the ability to "shoot" first. My question is how I would implement this. If anyone can help me to create a shooting effect with drawing bullets, I would really appreciate it. My game is currently set up so that if you press Space, it will initiate the shootmusket function in the shoot class. The shootmusket function will determine what direction you are facing, and if you are looking at the rectangle that is the enemy, it decreases the enemy's health by 5. The problem with this system is that it takes the enemy as an argument, and thus it will not work if I added in multiple enemies. Plus, if there were a wall, there is no bullet, so there would be no collision. I'm very new to SFML in general, so any help is appreciated. Thank you.

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: How to go about creating a bullet class in SFML 2.0
« Reply #1 on: May 28, 2013, 09:40:25 am »
I think your method is wrong to begin with.. rather then checking if your just facing the enemy you should actually fire a bullet entity instead. what I mean is set up a bullet manager class so when you press SPACE it activates a bullet and every frame update its position with the direction vector its traveling multiplied by the speed you want it to travel, and then also test if its colliding with anything. If it is colliding with something, reduce its health by whatever you want.

I would provide you with an example but I am at work at the moment, I will edit this post when I am at home.
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

mn12sc35th

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: How to go about creating a bullet class in SFML 2.0
« Reply #2 on: May 30, 2013, 04:53:53 am »
I agree with MattY's idea; you'd be better served to create a bullet and check for collision with it. You might do something like:

FireMusket(float elapsedTime){
          if(elapsedTime >= 2.0f) //or however fast you want to be able to fire
                     bullet_manager.Add(currentDirection); //currentDirection being the unit vector for your character's facing
}

Then create a bullet class, and a manager container for it (could use a list or map to store bullets). When you call the .Add function like above, you'd multiply the currentDirection vector by the bullet's speed to get the bullets velocity vector. Move the bullet by the vector, and check for collision in each step.
« Last Edit: May 30, 2013, 04:59:58 am by mn12sc35th »

jonnyprogrammer123

  • Guest
Re: How to go about creating a bullet class in SFML 2.0
« Reply #3 on: May 31, 2013, 05:50:00 am »
Alrighty. I have created the code similarly to that, used an array of sf::RectangleShape's and boolean values, made a for loop in the game engine which checked if the boolean value was true. If it were true, it'd draw the rectangles. Upon collision the boolean values would turn false, and thus it would not draw the bullet. It has a cap of 15 bullets, here's my code so far:



Code: [Select]
//Shoot.h



class shoot {


public:


bool ifw, shooting[51], seebullet[51];


void shootmusket(sf::RectangleShape enemy123, sf::Sprite player, int direction
);
shoot();
void shootbullet();
void deletebullet(int b);
sf::RectangleShape bullet[51];
int damage;


private:


sf::Clock shoottimer;
void makebullet(int direction, int z);
void makebullet(int direction, int z, int x, int y);
int velx[51], vely[51];
bool cb[51];


} shoot;




Code: [Select]


// Shooting.h



void game::shoot::shootmusket(sf::RectangleShape enemy123, sf::Sprite player, int direction) {


sf::Time shoottime = shoottimer.getElapsedTime();


if (shoottime.asSeconds() >= 2.0 && gamer.sprint > 0) {


shoottimer.restart();
gamer.sprint -= 5;
std::cout << gamer.sprint << std::endl;


int d = direction;


if (!cb[1])
makebullet(d, 1);
else if (!cb[2])
makebullet(d, 2);
else if (!cb[3])
makebullet(d, 3);
else if (!cb[4])
makebullet(d, 4);
else if (!cb[5])
makebullet(d, 5);
else if (!cb[6])
makebullet(d, 6);
else if (!cb[7])
makebullet(d, 7);
else if (!cb[8])
makebullet(d, 8);
else if (!cb[9])
makebullet(d, 9);
else if (!cb[10])
makebullet(d, 10);
else if (!cb[11])
makebullet(d, 11);
else if (!cb[12])
makebullet(d, 12);
else if (!cb[13])
makebullet(d, 13);
else if (!cb[14])
makebullet(d, 14);
else if (!cb[15])
makebullet(d, 15);
else
std::cout << "No Free Bullet Slots\n";
damage = 5;


}


}


game::shoot::shoot() {


shoottimer.restart();


}


void game::shoot::makebullet(int direction, int z) {


if (direction == 3 || direction == 4)
bullet[z].setSize(sf::Vector2f(5, 3));
else if (direction == 1 || direction == 2)
bullet[z].setSize(sf::Vector2f(3, 5));
bullet[z].setFillColor(sf::Color(100, 100, 5));
shooting[z] = 1;
bullet[z].setPosition(gamer.player.getPosition().x, gamer.player.getPosition().y);


velx[z] = 0;
vely[z] = 0;


if (direction == 1)
vely[z] = -5;
if (direction == 2)
vely[z] = 5;
if (direction == 3)
velx[z] = -5;
if (direction == 4)
velx[z] = 5;


cb[z] = 1;




}


void game::shoot::shootbullet() {


for (int x = 0; x <= 10; x++) {
bullet[x].move(velx[x], vely[x]);
seebullet[x] = 1;
}


}


void game::shoot::deletebullet(int b) {


bullet[b].setPosition(gamer.player.getPosition().x, gamer.player.getPosition().y);
cb[b] = 0;
velx[b] = 0;
vely[b] = 0;
seebullet[b] = 0;
shooting[b] = 0;


}




Code: [Select]


// Portion of Game Engine


// Shoot Bullet



for (int x = 0; x < 15; x++) {
if (shoot.shooting[x])
shoot.shootbullet();
}


// Bullet Collision



if (enemy1.seen) {
       
        for (int x = 0; x < 15; x++) {
       
        if(shoot.bullet[x].getGlobalBounds().intersects(enemy1.enemyr.getGlobalBounds())) {
   
    enemy1.health -= shoot.damage;
    shoot.deletebullet(x);
    enemy1.ifdead();
    std::cout << "BULLET " << x << " HIT TARGET ENEMY1\n";
       
        }
       
        }
       
        enemy1.updateai();
       
        }
       
        for (int x = 0; x <= 15; x++) {
       
        if(shoot.bullet[x].getGlobalBounds().intersects(wall.getGlobalBounds())) {

    shoot.seebullet[x] = 0;
    shoot.deletebullet(x);
       
        }

if(shoot.bullet[x].getGlobalBounds().intersects(wall2.getGlobalBounds())) {

    shoot.seebullet[x] = 0;
    shoot.deletebullet(x);
       
        }
       
    if(shoot.bullet[x].getGlobalBounds().intersects(wall3.getGlobalBounds())) {

    shoot.seebullet[x] = 0;
    shoot.deletebullet(x);
       
        }
       
    if(shoot.bullet[x].getGlobalBounds().intersects(wall4.getGlobalBounds())) {

    shoot.seebullet[x] = 0;
    shoot.deletebullet(x);
       
        }
       
    }




Code: [Select]


// Draw Bullets to the Screen



for (int x = 0; x < 15; x++) {
   
    if (shoot.seebullet[x])
    window.draw(shoot.bullet[x]);
   
    }




I'd say that that ain't bad for a 13-year-old. Does this look good? Any suggestions? Thanks.

 

anything