initial commit
[COMP345---Clone.git] / Character.cpp
blob4cc87beb764d7eecd389ba31af4d9da39dd743ca
1 //! @file Character.h
2 //! @author Patrick Nicoll 27218729
3 //! @brief Implementation of the class representing an instance of a character
5 #include "Character.h"
6 #include <random>
7 #include <iostream>
10 //! Default no param constructor
11 Character::Character() {
12 level = 1;
13 create();
16 //! Constructor that takes a level to initialize the stats accordingly
17 //! @param levelVal Level of character
18 Character::Character(int levelVal) {
19 level = 1;
20 create();
22 //Print statements used for testing
23 std::cout << "str: " << str << std::endl;
24 std::cout << "dex: " << dex << std::endl;
25 std::cout << "con: " << con << std::endl;
27 for (int i = 1; i < levelVal; i++) {
28 levelUp();
33 //! Destructor
34 Character::~Character()
38 //! Validates wheter the character has valid starting stats
39 //! @return Whether valid or not, true it is valid, false it is not
40 bool Character::validateNewCharacter()
42 bool valid = true;
44 if (str < 3 || str > 18)
45 valid = false;
46 if (dex < 3 || dex > 18)
47 valid = false;
48 if (con < 3 || con > 18)
49 valid = false;
50 if (intel < 3 || intel > 18)
51 valid = false;
52 if (wis < 3 || wis > 18)
53 valid = false;
54 if (cha < 3 || cha > 18)
55 valid = false;
57 return valid;
60 //! Method to call all the appropriate methods used during character creation.
61 void Character::create() {
62 generateRandomStats();
63 calculateHp();
64 calculateAc();
65 calculateBaseAttackBonus();
66 calculateDamageBonus();
69 //! Generate the attribute values for the six major stats.
70 //! Values are between 3-18
71 void Character::generateRandomStats()
73 // Will be used, but for testing purposes will hardcoded numbers will be set
74 int stats[6];
76 // create source of randomness, and initialize it with non-deterministic seed
77 std::random_device r;
78 std::seed_seq seed{ r(),r(),r(),r(),r(),r() };
79 std::mt19937 eng{ seed };
81 // a distribution that takes randomness and produces values in the range of 3 to 18
82 std::uniform_int_distribution<> dist(3, 18);
84 for (int i = 0; i < 6; i++) {
85 stats[i] = dist(eng);
88 //Set stats
89 str = stats[0];
90 dex = stats[1];
91 con = stats[2];
92 intel = stats[3];
93 wis = stats[4];
94 cha = stats[5];
96 //Set modifiers
97 str_mod = getModifier(stats[0]);
98 dex_mod = getModifier(stats[1]);
99 con_mod = getModifier(stats[2]);
100 intel_mod = getModifier(stats[3]);
101 wis_mod = getModifier(stats[4]);
102 cha_mod = getModifier(stats[5]);
105 //! Calculates the hitpoints of the character.
106 //! Hitpoints are calculated by adding the constitution modifier to the character level
107 void Character::calculateHp()
109 hp = abs(con_mod) + level;
110 maxHp = abs(con_mod) + level;
113 //! Calculates the armor class of the character.
114 //! Armor class is calculated by adding the dexterity modifier, armor bonus and shield bonus togther and then adding 10
115 void Character::calculateAc()
117 ac = 10 + dex_mod;
120 //! Calculates the base attack bonus of the character.
121 //! Base attack bonus is calculated by adding the strength modifier to the character level
122 void Character::calculateBaseAttackBonus()
124 attackBonus = str_mod + level;
127 //! Calculates the damage bonus of the character.
128 //! Damage bonus is calculated based on the strength modifier of the character
129 void Character::calculateDamageBonus()
131 damageBonus = str_mod;
134 //! Will increase attributes upon levelling up
135 void Character::levelUp() {
136 level++;
137 str++;
138 dex++;
139 con++;
141 //Call relevant methods to update based on new stat increase
142 str_mod = (getModifier(str));
143 dex_mod = (getModifier(dex));
144 con_mod = (getModifier(con));
145 calculateHp();
146 calculateAc();
147 calculateBaseAttackBonus();
148 calculateDamageBonus();
151 //! Determines modifier based on stat entered by evaluating which range it falls in
152 //! @param stat Stat to determine range of
153 //! @return Value of the range the stat fell under
154 int Character::getModifier(int stat)
156 int mod;
158 if (stat <= 1)
159 mod = -5;
160 else if (stat <= 3)
161 mod = -4;
162 else if (stat <= 5)
163 mod = -3;
164 else if (stat <= 7)
165 mod = -2;
166 else if (stat <= 9)
167 mod = -1;
168 else if (stat <= 11)
169 mod = 0;
170 else if (stat <= 13)
171 mod = 1;
172 else if (stat <= 15)
173 mod = 2;
174 else if (stat <= 17)
175 mod = 3;
176 else if (stat <= 19)
177 mod = 4;
178 else
179 mod = 5;
181 return mod;
184 //! Reduces character's hitpoints based on damage inputed
185 //! @param damage Damage to be taken away from hitpoints
186 void Character::hit(int damage) {
187 hp -= damage;