Skip to main content
gamedev.net
🔒 Locked

grouping classes

Started by TheOne1 Apr 30, 2004 at 8:49 PM 3 replies 350+ views
Original Post
TheOne1
TheOne1
if i were to start on a game that had an assortment of weapons would it be a good programming practice to make individual header files for each weapon? for example, if i had different types of rifles, should i put them under a header file name weapRifles and in that header file define classes like class M4A1, class AK-47...and so on. or is it better to make an individual header file for each weapon like weapRifle_AK-47? thanks in advance for replies.
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Vincent Wolf
Vincent Wolf
Neither. You should make one class for riffles, and then bring in their specific parameters through scripts.
Samith
Samith
I would make one class for rifles, and then specify other details in a file on the harddrive. In the file you would specify things like the range and accuracy and firing rate, bullets per magazine, all that stuff, and things like model names and texture files and sounds.
TheOne1
TheOne1
acutally...i meant a text-rpg. sorry...maybe i should go post this in the beginners forum....or have it moved.
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Permafried-
Permafried-
This might not be exactly what you are looking for...but you could give this a try.

Do a search on the internet for "Class factory pattern", and learn how to implement that for weapons. Essentially, you would have one base class (iWeapon) which is inherited by all other weapons created after that (CRifle, CShotgun, etc.). Then define a WeaponFactory class which returns an iWeapon*. Within each of the classes you could define their behaviour, so since it''s text based, just as an example:

class CRifle : public iWeapon
{
public:
void FireWeapon( void ); // pure virtual in iweapon
public:
};

void CRifle::FireWeapon( void )
{
cout << "Rifle firing" << endl;
}

class CWeaponFactory
{
public:
iWeapon* CreateWeapon( int iType );
private:
};

#include "iWeapon.h"
#include "Rifle.h"

iWeapon* CreateWeapon( int iType )
{
switch( iType )
{
case 0:
return new CRifle();
case 1:
return new CAnotherWeaponHere();
}

int main( int argc, char* argv[] )
{
CWeaponFactory pWeaponFact = new WeaponFactory();

iWeapon* pTheWeapon = pWeaponFact->CreateWeapon( 0 );

// this is now a rifle
pTheWeapon->FireWeapon();

return 0;
}

Output would be:
Rifle firing!

This may be a little excessive for what you''re trying to do, but what I wanted to do was give you a look at how you could use a factory, w/o having to always recompile your main, etc. as you add more weapons as your RPG grows. Personally, I''d put it in it''s own seperate class (hpp/cpp), but really you could have one file called Weapons and inside the header define each weapon as a seperate class, and implement it in Weapons.cpp so all your different weapons would be in one central location.

Permafried-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.