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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
===========================================================
CHARACTER CLASS
===========================================================
*/
class Character
{
public:
Character(string className, int health, int maxHealth, int attackPower, int maxAttackPower) :
className(className),
health(health),
maxHealth(maxHealth),
attackPower(attackPower),
maxAttackPower(maxAttackPower){}
Character() = default;
string GetClassName() { return className; }
int GetHealth() const { return health; }
int GetAttackPwr() const { return attackPower; }
int GetMaxAttackPower() const { return maxAttackPower; }
int GetMaxHealth() const { return maxHealth; }
void TakeDamage(const int damageTaken);
void RestoreHealth(const int restoreAmount);
void UpgradeAttackPower(const int upgradeAmount);
private:
string className{};
int health{};
int maxHealth{};
int attackPower{};
int maxAttackPower{};
};
/*
===========================================================
ITEM CLASS
===========================================================
*/
class Item
{
public:
Item(string name, unsigned price) : name(name), price(price) {}
string GetName() const { return name; }
int GetPrice() const { return price; }
private:
string name{};
unsigned price{};
};
/*
===========================================================
PLAYER CLASS
===========================================================
*/
class Player : public Character
{
public:
Player(string name, string chosenClass, float experience, unsigned level, int money, string characterClass, int health, int maxHealth, int attackPower, int maxAttackPower) :
name(name),
Character(characterClass, health, maxHealth, attackPower, maxAttackPower){}
void GetInventory();
float GetExperience() const { return experience; }
float GetExperienceNeeded() const { return experienceNeeded; }
int GetLevel() const { return level; }
string GetName() const { return name; }
int GetMoney() const { return money; }
void AddItemToInventory(string itemName, int amountToAdd);
void RemoveItemFromInventory();
void UseItem();
void GiveExperience(const int xpToGive);
void LevelUp(const int level);
void Buy(Character& character);
void Sell();
void Shop();
private:
string name{};
string chosenClass{};
int experience{};
int experienceNeeded = 10;
unsigned level{};
vector < std::pair<string, int>> inventory{};
int money{};
};
/*
===========================================================
GIVE EXPERIENCE FUNCTION
===========================================================
*/
void Player::GiveExperience(const int xpToGive)
{
const float multiplier = 1.7; //Amount to multiply XP needed by.
unsigned levelUpAmount = 0;
experience += xpToGive;
//This wiil apply all the levels the player earns if they get a huge XP boost that spans multiple levels.
while (experience >= experienceNeeded)
{
levelUpAmount++;
experienceNeeded = experienceNeeded * multiplier;
cout << "Level up amount " << levelUpAmount << endl; //Debug only
}
LevelUp(levelUpAmount);
}
//Add amount of xp to multiply by.
void Player::LevelUp(const int giveLevel)
{
level += giveLevel;
}
void Player::AddItemToInventory(string itemName, const int amountToAdd)
{
inventory.push_back(std::make_pair(itemName, amountToAdd));
}
void Player::GetInventory()
{
for (int i = 0; i < inventory.size(); i++)
{
cout << inventory[i].first << " " << inventory[i].second << endl;
}
}
ostream& operator<< (ostream& os, Character& character)
{
os << "Class Name: " << character.GetClassName() << endl;
os << "Health: " << character.GetHealth() << endl;
os << "Max Health: " << character.GetMaxHealth() << endl;
os << "Attack Power: " << character.GetAttackPwr() << endl;
os << "Max Attack Power: " << character.GetMaxAttackPower() << endl;
return os;
}
void Character::RestoreHealth(const int restoreAmount)
{
if (health + restoreAmount <= maxHealth)
{
health += restoreAmount;
}
else if (health + restoreAmount >= maxHealth)
{
health = maxHealth;
}
}
void Character::TakeDamage(const int damageTaken)
{
if (health > 0)
{
health -= damageTaken;
if (health <= 0)
{
health = 0;
cout << "The " << GetClassName() << ", was defeated." << endl;
}
}
}
int main()
{
Character Knight
(
"Knight", /*Class Name*/
120, /*Health*/
150, /*Max Health*/
3, /*Attack Power*/
6 /*Max Attack Power*/
);
Character Samurai
(
"Samurai", /*Class Name*/
40, /*Health*/
60, /*Max Health*/
7, /*Attack Power*/
10 /*Max Attack Power*/
);
Character Marine
(
"Marine", /*Class Name*/
80, /*Health*/
150, /*Max Health*/
2, /*Attack Power*/
3 /*Max Attack Power*/
);
Character Archer
(
"Archer", /*Class Name*/
10, /*Health*/
20, /*Max Health*/
6, /*Attack Power*/
8 /*Max Attack Power*/
);
float experience = 0;
unsigned level = 1;
string name{};
int startingMoney = 100;
cin >> name;
Player player
(
name,
Knight.GetClassName(),
experience,
level,
startingMoney,
Knight.GetClassName(),
Knight.GetHealth(),
Knight.GetMaxHealth(),
Knight.GetAttackPwr(),
Knight.GetMaxAttackPower()
);
cout << player.GetClassName() << endl;
cout << player.GetExperience() << endl;
cout << player.GetName() << endl;
cout << player.GetExperienceNeeded() << endl;
int choice = 0;
while (choice != -3)
{
cout << "Current XP " << player.GetExperience() << endl;
cout << "Experience Needed " << player.GetExperienceNeeded() << endl;
cout << "Level " << player.GetLevel() << "\n" << endl;
cout << "What would you liketo do?" << endl;
cout << "1) Give XP" << endl;
cin >> choice;
switch (choice)
{
case 1:
player.GiveExperience(13);
break;
default:
cout << "Invalid Choice" << endl;
}
}
player.Shop();
player.GetInventory();
return 0;
}
/*
===========================================================
SHOP FUNCTION
===========================================================
*/
void Player::Shop()
{
Item Healthpack("Health Pack", /*Item Price*/ 800);
Item Map("Map", /*Item Price*/ 150);
vector<string> shopInventory;
shopInventory.push_back(Healthpack.GetName());
shopInventory.push_back(Map.GetName());
int choice{};
int counter = 1;
int amount{};
while (choice != -3)
{
amount = 0;
cout << "What would you like to buy?" << endl;
for (int i = 0; i < shopInventory.size(); i++)
{
cout << counter++ << ") " << shopInventory[i] << endl;
}
cin >> choice;
if (choice == -3)
{
continue;
}
cout << "How many do you want?" << endl;
cin >> amount;
switch (choice - 1)
{
case 0:
AddItemToInventory(Healthpack.GetName(), amount);
break;
case 1:
AddItemToInventory(Map.GetName(), amount);
break;
default:
cout << "Invalid Choice" << endl;
}
}
}
|