- Nov 20, 2005
- 6,161
- 2
- 81
I'm trying to write a console based RPG game, but I am stuck on the damage function part of it.
The damage portion works fine, it takes ((attack power - defense roll) - armor) and will come up with the correct number. But when I go to modify the enemy hit points it doesn't subtract. It just prints the default value (12 in this case for Goblins).
Is there something wrong with my syntax? I am trying to use inheritance from a base class and as long as I get the attack function it should roll into other classes just fine.
My issue is that the damage is calculated correctly, it just doesn't modify hit points to reflect the damage.
The damage portion works fine, it takes ((attack power - defense roll) - armor) and will come up with the correct number. But when I go to modify the enemy hit points it doesn't subtract. It just prints the default value (12 in this case for Goblins).
Is there something wrong with my syntax? I am trying to use inheritance from a base class and as long as I get the attack function it should roll into other classes just fine.
My issue is that the damage is calculated correctly, it just doesn't modify hit points to reflect the damage.
Code:
//Hit Point Function in base class
void setHP(double hp){
hitpoints = hp;
}
void modHP(double hp, double dmg) //will modify based on attack roll
{
hp = hp - dmg;
hitpoints = hp;
}
double getHP(){
return hitpoints;
}
do
{
double ap = rand() % 12; //effectively rolls a 2D6
double arp = 3; //armor;
double hp = 12;
cout << "Your attack power was rolled to: " << ap << endl;
dp = rand() % 6; //goblin defense roll
cout << "Defense power for that enemy is: " << dp << endl; //shows user the defense power for the current enemy
gobs->setdefensepower(dp); //sets goblin defense power
double dmg = ((ap - dp) - arp);
//will print if there was no damage dealt
if (dmg <= 0) {
cout << "Your enemy defended your attack!" << endl;
break;
}
else if (dmg > 0) {
gobs->modHP(hp, dmg);
gobs->setHP(hp);
cout << "Enemy health remaining: " << gobs->getHP() << endl;
}
else if (gobs->getHP <= 0){
cout << "You have slain the enemy!" << endl;
}
}
break;
} while(select == 1);