Creating a weapon system (c++)

So say I'm making a text based rpg,

I'm writing it as a console project in code::blocks

So I want to include some sort of inventory system, but I have recently begun my path to learning c++. I don't know how to have a library of weapons, and when the player chooses it, it changes the attack of the character, and how to do it effeciantly.

There's no rush to answer, just I'm looking through the tutorials, and human help would be really appreciated
I would probably have a container of weapons (maybe a std::vector) and just have an internal member of a player class that takes a weapon. Here is an example:
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
class Weapon {
    private:
        double _dmg;
        std::string _name;
        // ...

    public:
        // set the weapon data when the weapon is made
        Weapon(double dmg, const std::string& name)
            : _dmg(dmg), _name(name) {}

        // get the damage done by the weapon
        double getDamage() const { return _dmg; }

        // get the name of the weapon
        std::string getName() const { return _name; }

        // ...
};

class Player {
    private:
        Weapon _currWeapon;
        double _attack;
        //...

    public:
        // construct with no weapon
        Player() : _currWeapon(0.0, "Fists") {}

        // set the weapon of the player
        void setWeapon(const Weapon& weapon) {
            _currWeapon = weapon;
        }

        // get the damage dealt by the player
        double getDamageDealt() const {
            return _attack + _currWeapon.getDamage();
        }

        // ...
};

//...

std::vector<Weapon> wpns;
Player player;

wpns.push_back(Weapon(10.0, "Bronze Sword");
wpns.push_back(Weapon(20.0, "Long Bow");
wpns.push_back(Weapon(1500.0, "Rocket Launcher");
// so on

std::cout << "Choose a weapon: \n";
for (size_t i = 0; i < wpns.size(); ++i) 
    std::cout << i << ". " << wpns[i].getName() < "\n";

size_t choice;
std::cin >> choice;
player.setWeapon(wpns[choice]);

// checking damage
double dmg = player.getDamage();


There are other ways of doing this, some better than others. This is probably the easiest way of doing it, as it also makes it really easy to add more weapons. It isn't the most efficient, but does the program really need to be fast at setting your weapon? At any rate, it probably won't make a difference.

Just another little thing: I would wait for a while before trying to make a game, especially if you haven't known C++ for a while. Also, text based games are surprisingly hard, and you would be surprised at how easy it is to learn a graphics library (especially one like SFML or SDL).
Topic archived. No new replies allowed.