-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature.hpp
189 lines (155 loc) · 5.91 KB
/
creature.hpp
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
/*
The MIT License (MIT)
Copyright (c) 2013 Daniel Mansfield
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef CREATURE_HPP
#define CREATURE_HPP
#include "inventory.hpp"
#include "weapon.hpp"
#include <string>
class Creature
{
public:
// Name of the creature and the name of its class, if it has one
// Class may be Fighter, Rogue etc
std::string name;
std::string className;
// Creature stats. Reasonable values are in parentheses
int health; // Current hit points (10-1000+)
int maxHealth; // Maximum hit points (10-1000+)
int str; // Strength. Determines damage in battle (1-100)
int end; // Endurance. Determines maximum health (1-100)
int dex; // Dexterity. Determines speed in battle (1-100)
double hitRate; // Modifier to hit chance. (1-150)
// Current level of the creature. Determines the amount of experience
// that it gives to the victor when defeated (see Battle class for more)
// and the amount of experience required to level up again. Upon
// levelling up the creature will gain stat improvements.
// 1-50 is reasonable
unsigned int level;
// Current experience. 0-1M is reasonable, see the levelup() function
// for a decent scale
unsigned int exp;
// Items that the creature possesses
Inventory inventory;
// Currently equipped weapon. Used as a pointer to an atlas entry,
// but not necessary. nullptr denotes that no weapon is equipped
Weapon* equippedWeapon;
Creature(std::string name, int health, int str, int end, int dex, double hitRate,
unsigned int level = 1, std::string className = "")
{
this->name = name;
this->health = health;
this->maxHealth = health;
this->str = str;
this->end = end;
this->dex = dex;
this->hitRate = hitRate;
this->className = className;
this->equippedWeapon = nullptr;
this->level = level;
this->exp = 0;
}
Creature()
{
this->equippedWeapon = nullptr;
this->level = 1;
this->exp = 0;
}
// Equip a weapon by setting the equipped weapon pointer. Currently
// a pointless function (simple enough to be rewritten each time)
// but handy if dual wielding is ever added, or shields etc
void equipWeapon(Weapon* weapon)
{
this->equippedWeapon = weapon;
return;
}
// Calculates the experience required to reach a certain level,
// *in total*. Really this is class specific and not object specific
unsigned int expToLevel(unsigned int level)
{
// Exp to level x = 128*x^2
return 128 * level * level;
}
// Level the creature to the next level if it has enough experience
// to do so, returning true if it could level up and false otherwise.
bool levelUp()
{
// We want the experience to the next level, not the current level
if(this->exp >= expToLevel(this->level+1))
{
// Advance to the next level
++level;
// Variables to keep track of stat changes. Neater than
// having a bunch of stat increases all over the place,
// and removes the issue of the next level's stats affecting
// themselves (increasing endurance then increasing health
// based on the boosted instead of the original value, for
// example
unsigned int healthBoost = 0;
unsigned int strBoost = 0;
unsigned int endBoost = 0;
unsigned int dexBoost = 0;
// Give a large boost to health every third level
if(level % 3 == 0)
{
// Randomly increase health, but always give a sizeable
// chunk proportional to the creature's endurance
healthBoost = 10 + (rand() % 4) + this->end / 4;
}
else
{
// Just increase health by a small amount
healthBoost = this->end / 4;
}
// If the creature is a fighter, then favour strength and
// endurance boosts over dexterity, but increase dexterity
// 50% of the time too
if(this->className == "Fighter")
{
strBoost = 1;
endBoost = 1;
if(rand() % 2 == 0) dexBoost = 1;
}
// Same as for fighter but favour dexterity and endurance
// instead. Rogue's favour endurance too in order to keep
// them at roughly the same capability
else if(this->className == "Rogue")
{
endBoost = 1;
dexBoost = 1;
if(rand() % 2 == 0) strBoost = 1;
}
// Adjust all of the variables accordingly
this->maxHealth += healthBoost;
this->str += strBoost;
this->end += endBoost;
this->dex += dexBoost;
// Tell the user that they grew a level, what the boosts where
// and what their stats are now
std::cout << this->name << " grew to level " << level << "!\n";
std::cout << "Health +" << healthBoost << " -> " << this->maxHealth << std::endl;
std::cout << "Str +" << strBoost << " -> " << this->str << std::endl;
std::cout << "End +" << endBoost << " -> " << this->end << std::endl;
std::cout << "Dex +" << dexBoost << " -> " << this->dex<< std::endl;
std::cout << "----------------\n";
return true;
}
return false;
}
};
#endif /* CREATURE_HPP */