game class question

Hi, I am about to try to implement a simple combat system for my SDL game. I am currently writing a Weapon class. My Player class will have an inventory(Array) of weapons.

My question comes for the bullets, each weapon should have a differet type of bullet associated with it. I'm not sure how best to go about this. I decided to make my Bullet as a nested class of my Weapon. This is how my class looks so far:
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
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Item.h"
class Weapon : public Item
{
	class Bullet{
	public:
		Bullet(WeaponType);
		//set
		void setBulletICO();
	private:
		float xPos;
		float yPos;
	};
public:
	Weapon(WeaponType);
public:
	enum WeaponType{
		WPN_Default=0,WPN_Laser,WPN_Rocket
	};

private:
	int magazineSize;
	int currentAmmo;
private:
	SDL_Surface* weaponICO;
	WeaponType _type;
	Bullet bullet;

};


#endif 


As you can see, Inside my weapon class I create a Bullet object. Should I make an array of bullets that is the size of the magazine of whichever weapon is currently in use? Or, Is it possible to create one Bullet object per weapon, and re-use it more than once on the "same screen"?

I need some input on the best way to go about this please, thanks
I recommend not creating an array of bullets that is the size of the magazine. It will take up needless space in memory. Rather just decrement currentAmmo every time you shoot a bullet in order to keep track of bullets remaining etc. What you could do is that you could have an

vector<Bullet*> activeBullets;

of which you push bullets into everytime you fire.


1
2
3
4
5
6
Bullet* newBullet;

// Minime, fire ze LAZER !
newBullet = new Bullet(WPN_Laser);
activeBullets.push_back(newBullet);


Then when you update your game state you can iterate through that vector and update whatever you need to e.g. xPos, yPox and whether it has hit something.
Last edited on
So, you saying that when I fire, I add a bullet to my "list" and when it hits something/goes off screen, I remove it from the list?

Would that mean If I had another variables such as rate of fire, would it be good to push "rate of fire" number of bullets into the list? So you can only fire that many bullets per second or something, is that a good way to do that?
Yes remove the bullet from the data structure once it has been destroyed or out of bounds of your screen.

Rate of fire, you can do in several ways depending on your game mechanics. I would recommend creating a timestamp timeFired when you shoot. Then when the player wants to shoot again you can check that against current system time while taking into account the rate of fire of the current weapon. If the difference is less or equal to 0 then the player can shoot again. :P

Oh, unless you want to shoot 3 bullets at a time of which all you need to do is the same thing but create 3 Bullet objects instead of one and throw them into the vector.
Last edited on
Thanks
You're welcome! :D Good luck with your game!
Topic archived. No new replies allowed.