Skip to content

Instantly share code, notes, and snippets.

@benphelps
Created April 25, 2013 20:57
Show Gist options
  • Save benphelps/5463112 to your computer and use it in GitHub Desktop.
Save benphelps/5463112 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct statsBase {
int stamina;
int agility;
int defence;
int intellect;
statsBase(int s = 0, int a = 0, int d = 0, int i = 0 ):stamina(s), agility(a), defence(d), intellect(i) {}
};
// matrix {1,1,1,1}
struct armorItem {
statsBase stats;
armorItem(int s = 0, int a = 0, int d = 0, int i = 0):stats(s,a,d,i) {}
};
struct armorBase {
armorItem healm;
armorItem chest;
armorItem pants;
armorItem boots;
};
struct raceBase {
statsBase boost;
};
struct characterBase {
statsBase stats;
raceBase race;
armorBase armor;
} characterDefault;
// a player is really just a character, so inherit the struct
class Player : public characterBase {
// how much damage the player has taken
int healthDeficit;
// calculate the total health value
int totalStamina() {
int health =
stats.stamina + // base HP
race.boost.stamina + // racial HP boost
armor.healm.stats.stamina + // Armor HP
armor.chest.stats.stamina +
armor.pants.stats.stamina +
armor.boots.stats.stamina ;
return health;
}
public:
// constructor
Player() {
healthDeficit = 0;
}
// do damage to the player
void hit(int amount = 0) {
healthDeficit += amount;
}
// whats the players HP
int health() {
return totalStamina() - healthDeficit;
}
};
int main() {
// create our player
Player player;
// give the player a nice item
player.armor.healm = armorItem(15,0,5,3);
// whats our HP ?
printf( "HP: %d \n", player.health() );
// hit our player for 5 damage
player.hit(5);
// whats our HP ?
printf( "HP: %d \n", player.health() );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment