Weapon Classes

Hey There

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 :) )

- Simon
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(); */


Inside those functions isn't to tricky

1
2
3
4
//get_damage
get_damage()
{
return (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.


- Simon
Also thanks for the quick reply, I really appreciate this :)
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.
Not sure if I follow you. Is the value of the variables the only difference between the weapons?
In that case you could do something like
1
2
3
weapon BFG(/*values*/), MMM( /**/), stick(/**/);
//then to choose your weapon
choose = stick; //hardcore player 


Ah So I would just use ALOT of setters and getters? Sounds like fun...
I hope that was sarcasm. Use the constructor to set, and instead of asking for information provide methods that will update them.
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
that will update them.


Thanks everybody for the patience

- Simon
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".
closed account (3hM2Nwbp)
I can't compile at work and can't guarantee it's free of logic / syntax errors, so just use it as a little food for thought.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Item
{
  public:
    Item(const std::string& name, int id)
     : name(name), itemID(id) { }
    const string& getName(void) const { return this->name; }
    int getID(void) const { return this->itemID;j }
  private:
   string name;
   int itemID;
};

class Weapon
 : public Item
{
  private:
   int ammo;
  public:
   Weapon(const std::string& name, int id, int ammo)
    : Item(name, id), ammo(ammo) { }
   int getAmmo(void) const;
   virtual int getAccuracy(void) const = 0;
   virtual int getDamage(void) const = 0;
   virtual int getMaxAmmo(void) const = 0;
   virtual float getRange(void) const = 0;
};

class BFG9000
 : public Weapon
{
  public:
   BFG9000(int ammo)
    : Weapon("BFG9000", 0/*id*/, 9000/*ammo*/) { }
   virtual int getAccuracy(void) const { return 9000; }
   virtual int getDamage(void) const { return 9000; }
   virtual int getMaxAmmo(void) const { return 9000; }
   virtual float getRange(void) const { return 9000.0; }
};

class Chainsaw
 : public Weapon
{
  public:
   Chainsaw()
    : Weapon("Chainsaw", 0/*id*/, -1/*ammo*/) { }
   virtual int getAccuracy(void) const { return numeric_limits<int>::max(); }
   virtual int getDamage(void) const { return numeric_limits<int>::max(); }
   virtual int getMaxAmmo(void) const { return -1; }
   virtual float getRange(void) const { return 3.0; }
};

class Player
{
  private:
   Weapon* weapon;
  public:
   Player(void) : weapon(0) { }
   void setWeapon(Weapon* weapon) { delete this->weapon; this->weapon = weapon; }
   Weapon* getWeapon(void) const { return this->weapon; }
   ~Player(void) { delete this->weapon; }
};

int main()
{
  Player player;
  player.setWeapon(new BFG9000(4423));
  cout << player.getWeapon()->getName() << "\t" << player.getWeapon()->getAccuracy() << endl;
  player.setWeapon(new Chainsaw());
  cout << "Let's find some meat..." << endl;
  return 0;
}


Edit - Let's take this time to look back on ID software's finest creation, DOOM 95 :D

Fixed the bugs pointed out by the human compiler below :P Thanks firedraco.
Last edited on
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
1
2
if( rifle.getammo() != 0 ) 
  rifle.setammo( rifle.getammo()-1 );
_Just tell what to do rifle.shoot();
Topic archived. No new replies allowed.