I recently started on my new top down shooter using c++ and SFML and I'm just setting up the class hierarchy as before I begin the serious coding ;). Now I read a thread recently on here and decided the best way for my weapon classes to go would be:
Item >> Weapon >> Heavy Machine Gun
Item >> Weapon >> Flash Bang
But my question is this, If my classes have these sort of values:
1 2 3
Item:
string name;
int itemID;
Then
1 2 3 4 5 6
Weapon:
int accuracy;
int damage;
int ammo;
int max_Ammo;
float range;
then when it gets to the actual specific weapon such like Heavy Machine Gun, all I can't really see any actual specific traits I need to add. So then what? How do I actually add the values to these variables to make the gun specific? I know this would probably be an object and I would need a function for this, I'd just like to see that function.
Sorry if this didn't make sense (sometimes things don't come out of my head the way they should :) )
Instead of inheiriting you could make a "Heavy_Machine_Gun" variable of Class Weapon.
then when it gets to the actual specific weapon such like Heavy Machine Gun, all I can't really see any actual specific traits I need to add. So then what? How do I actually add the values to these variables to make the gun specific? I know this would probably be an object and I would need a function for this, I'd just like to see that function.
Well if your variables are Private or Protected(the latter is the best, in my opinion) you will indeed need to use get and set functions.
1 2 3 4
Weapon Knife //declares instance of Weapon
Knife.set_ammo(/*OVER*\ 9000) // ha ha ha....
Knife.get_damage(); */
Ah So I would just use ALOT of setters and getters? Sounds like fun...
But seriously when would I place all that information? I'm thinking of having a system where the player selects a player class such as Recon or Infantry and then that class has a set primary weapon. So could I make a class called 'Recon' and then set the values there? would you mind telling me practically where I would call the setters and getters?
Also I didn't quit understand what you meant by
Instead of inheiriting you could make a "Heavy_Machine_Gun" variable of Class Weapon.
A simple solution would be a variable inside the weapon class called type. It could be an int, char, string, or just about anything else. If I were you, I'd make it an int. Then you would set the value of this variable based on the type that the player picked. (heavy_machine_gun could be represented by a 1, etc). Finally, which weapon that gets drawn and what certain stats get set to will all be controlled by the value of the type variable.
Ok Thanks a ton everybody for the quick and helpful responses, But I'm still getting a bit of a mixed message in how I should do this. So, and correct me if I'm getting the wrong idea here, I should Create a base class called Item with the Name and Id for each item. I then narrow it down to stats such as accuracy and damage. Then With the weapons class I just make the constructor add values to the gun like ne555 said. Then with that constructor I would call it in each player class (?) like a sniper for recon, and Heavy machine gun for The Heavy Class. If i'm still not getting it though, just slap me across the head and scream it down the internet XD
@ModShop - Now I could give each weapon a type using an array(?), and then just assign the weapon the index number of the array(I'm just guessing?)
@ne555 Thanks a ton for the idea, It helps a lot. And yes, that quote was me <trying> to be funny... Also do you think you could type up a quick example of the constructor for me, just to get the picture (I suck a constructors BIG time).
Also I didn't understand what you meant by
You could use an array. I was thinking something like this though:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class weapon {
public:
weapon(int t); //where t is the "type"
int type;
int attack;
};
//in a function...
weapon wep(1); //lets pretend "1" represents a machine gun
//constructor
weapon::weapon(int t){
type = t;
if (type==1)
attack = 50;}
This isn't pretty, but it works. The value of "type" represents the type of weapon the object is. Other vars get set by the constructor based on the value of "type".
1) I think you forgot to inherit from Weapon in the Chainsaw etc classes.
2) You will be deleting garbage memory the first time you call Player::setWeapon().
The idea was that the only difference between the weapons is their properties. But they all act the same, they don't overwrite behaviour or add anything. (this could be changed)
Also all the rifles are the same at the start, or late in the game, they all do the same damage (this could be changed, too)
So you've got one instance of weapon of every kind. When you want to assign a weapon you copy the appropriate object.
Let the methods to do the job.
_Don't ask for information