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
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename tag, typename value_type>
struct parameter_type
{
value_type m_Value;
explicit parameter_type(const value_type& val) : m_Value{val}
{
}
};
namespace health
{
using value_type = float;
namespace start
{
struct tag {};
using type = parameter_type<tag, value_type>;
const type Knight{100};
}
namespace max
{
struct tag {};
using type = parameter_type<tag, value_type>;
const type Knight{150};
}
}
class Character
{
public:
Character(string charClass, const health::start::type& charHealth, const health::max::type& charMaxHealth, int charAttackPwr, int charMaxAttackPwr) :
charClass(charClass),
charHealth(charHealth.m_Value),
charMaxHealth(charMaxHealth.m_Value),
charAttackPwr(charAttackPwr),
charMaxAttackPwr(charMaxAttackPwr) {}
string GetCharClass() { return charClass; }
health::value_type GetCharHealth() const { return charHealth; }
int GetCharAttackPwr() { return charAttackPwr; }
int GetCharMaxAttackPwr() { return charMaxAttackPwr; }
health::value_type GetCharMaxHealth() const { return charMaxHealth; }
void DealDamage(int damageDealt);
void TakeDamage(int damageTaken);
void RestoreHealth(int restoreAmount);
void UpgradeAttackPower(int upgradeAmount);
void DisplayCharacterInfo(Character& character);
private:
string charClass{};
health::value_type charHealth{};
health::value_type charMaxHealth{};
int charAttackPwr{};
int charMaxAttackPwr{};
};
ostream& operator<< (ostream& os, Character& character)
{
os << "Class Name: " << character.GetCharClass() << endl;
os << "Health: " << character.GetCharHealth() << endl;
os << "Max Health: " << character.GetCharMaxHealth() << endl;
os << "Attack Power: " << character.GetCharAttackPwr() << endl;
os << "Max Attack Power: " << character.GetCharMaxAttackPwr() << endl;
return os;
}
void Character::RestoreHealth(int restoreAmount)
{
if (charHealth < GetCharMaxHealth())
{
if (restoreAmount < charHealth)
{
charHealth += restoreAmount;
}
if (restoreAmount > charHealth)
{
charHealth = GetCharMaxHealth();
}
}
else
{
cout << "Player health is full!" << endl;
}
}
void Character::DisplayCharacterInfo(Character& character)
{
cout << *this character << endl;
}
//Create a struct to give values a name instead of just a confusing number.
//Could have used an enum, but enums cant contain floats, so this fixes that potential issue.
struct Data
{
int health{};
int maxHealth{};
int attackPower{};
int maxAttackPower{};
};
int main()
{
Data data;
Character Knight("Knight", health::start::Knight, health::max::Knight, data.attackPower = 50, data.maxAttackPower = 50);
//Character Samurai("Samurai", data.health = 50, data.maxHealth = 70, data.attackPower = 300, data.maxAttackPower = 300);
//Character Marine("Marine", data.health = 200, data.maxHealth = 250, data.attackPower = 100, data.maxAttackPower = 120);
vector<Character> charList;
charList.push_back(Knight);
//charList.push_back(Samurai);
//charList.push_back(Marine);
for (int i = 0; i < charList.size(); i++)
{
cout << charList[i] << endl;
}
//Debug
cout << "--DEBUG--\n" << endl;
//Samurai.RestoreHealth(36);
//Samurai.DisplayCharacterInfo(Samurai);
Knight.RestoreHealth(49);
Knight.DisplayCharacterInfo(Knight);
cout << "--DEBUG--" << endl;
//End Debug
return 0;
}
|