I'm trying to design a weapons class for a game. This is some of the code I have come up with to fit my needs:
class weapon {
public:
int fireRate;
int bulletDamage;
int range;
ofImage sprite;
ofImage bulletSprite;
bullet bullets[50];
int activeBullet;
public:
void fire();
};
class machineGun: public weapon {
public:
void fire();
};
class flamer: public weapon {
public:
void fire();
};
And then I would like to define a weapons array like so:
//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
I would like element [0] to represent the machineGun class and element [1] to represent the flamer class. Am I going about this problem the correct way? Should I refactor this in some way? How do I achieve an array that holds these two different weapons?
The idea is so when I call weapons[0].fire();
I get a machineGun class and when I call weapons[1].fire();
I get flamer fire.
Edit: Thanks for helping guys. I'm having a few problems with using "weapons[0] = new machineGun;". I get an error "cannot allocate an array of constant size 0" when I try to run this code.
Has anyone got any insight into why this isn't working? My updated code looks something like this:
//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
weapons[0] = new machineGun;
weapons[1] = new flamer;
But I get lots of errors:
1>gameplay.cpp(49) : error C2466: cannot allocate an array of constant size 0
1>gameplay.cpp(49) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(49) : error C2371: 'weapons' : redefinition; different basic types
1> gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(49) : error C2440: 'initializing' : cannot convert from 'machineGun *' to 'int []'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>gameplay.cpp(50) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(50) : error C2369: 'weapons' : redefinition; different subscripts
1> gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(50) : error C2440: 'initializing' : cannot convert from 'flamer *' to 'int [1]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
I have taken another different approach and gotten some different errors. I'm still pretty unsure how this is all meant to glue together.
//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons] = {new machineGun, new flamer};
With errors:
1>gameplay.cpp(48) : error C2275: 'machineGun' : illegal use of this type as an expression
1> gameplay.h(36) : see declaration of 'machineGun'
1>gameplay.cpp(48) : error C2275: 'flamer' : illegal use of this type as an expression
1> gameplay.h(41) : see declaration of 'flamer'
The answer ended up being something like this:
//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon *weapons[totalWeapons] = {new machineGun, new flamer};
Thanks everyone who helped me get my head around this!