I am looking to make this more efficient, how would I Re-write the Enemy class to use inheritance and virtual functions? Including any new child classes.
class Enemy
{
public:
int type; // 0 = Dragon, 1 = Robot
int health; // 0 = dead, 100 = full
string name;
Enemy();
Enemy(int t, int h, string n);
int getDamage(); // How much damage this enemy does
};
Enemy::Enemy() : type(0), health(100), name("")
{ }
Enemy::Enemy(int t, int h, string n) :
type(t), health(h), name(n)
{ }
int Enemy::getDamage() {
int damage = 0;
if (type == 0) {
damage = 10; // Dragon does 10
// 10% change of extra damage
if (rand() % 10 == 0)
damage += 10;
}
else if (type == 1) {
// Sometimes robot glitches and does no damage
if (rand() % 5 == 0)
damage = 0;
else
damage = 3; // Robot does 3
}
return damage;
}
This calculates how much total damage the band will dish out.
int calculateDamage(vector<Enemy*> bandOfEnemies)
{
int damage = 0;
for (int i = 0; i < bandOfEnemies.size(); i++)
{
damage += bandOfEnemies[i]->getDamage();
}
return damage;
}