-
Notifications
You must be signed in to change notification settings - Fork 0
/
playercharacter.hpp
377 lines (330 loc) · 12.5 KB
/
playercharacter.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "ability.hpp"
#include "item.hpp"
#include "pointwell.hpp"
#include "statblock.hpp"
#include "types.hpp"
using exptype = std::uint64_t;
using leveltype = std::uint16_t;
class PlayerCharacterDelegate : public statblock {
public:
PlayerCharacterDelegate()
: statblock(0, 0, 0),
CurrentLevel(static_cast<leveltype>(1u)),
CurrentEXP(static_cast<leveltype>(0u)) {
HP = std::make_unique<PointWell>(1u, 1u);
}
virtual ~PlayerCharacterDelegate() = default;
void gainExp(exptype exp) {
CurrentEXP += exp;
while (check_if_leveled()) {
// Do nothing
};
}
void applyBuff(buff b) {
for (auto& buff : Buffs) {
if (buff.name == b.name) {
buff.duration = b.duration;
return;
}
}
AddNewBuff(b);
}
virtual void LevelUp() = 0;
virtual auto getClassName() -> std::string = 0;
std::unique_ptr<PointWell> HP;
std::unique_ptr<PointWell> MP;
std::vector<Ability> Abilities;
auto getLevel() const -> leveltype { return CurrentLevel; }
auto getCurrentEXP() const -> exptype { return CurrentEXP; }
auto getEXPToNextLevel() const -> exptype { return EXPToNextLevel; }
private:
static const exptype LEVEL2AT = 100u;
leveltype CurrentLevel = 1u;
leveltype CurrentEXP = 1u;
exptype EXPToNextLevel = LEVEL2AT;
auto check_if_leveled() -> bool {
static const leveltype LEVELSCALAR = 2u;
if (CurrentEXP >= EXPToNextLevel) {
CurrentLevel++;
LevelUp();
EXPToNextLevel *= LEVELSCALAR;
return true;
}
return false;
}
};
class PlayerCharacter {
private:
std::unique_ptr<PlayerCharacterDelegate> pcclass;
std::array<std::unique_ptr<equipmentDelegate>,
static_cast<u_int16_t>(ARMORSLOTS::NUM_SLOTS)>
EquippedArmors;
std::array<std::unique_ptr<equipmentDelegate>,
static_cast<u_int16_t>(WEAPONSLOT::NUM_SLOTS)>
EquippedWeapon;
public:
PlayerCharacter() = delete;
PlayerCharacter(const PlayerCharacter&) = delete;
PlayerCharacter(PlayerCharacter&&) = delete;
explicit PlayerCharacter(PlayerCharacterDelegate* pc) : pcclass(pc) {}
~PlayerCharacter() = default;
auto getClassName() -> std::string { return pcclass->getClassName(); }
auto getLevel() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getLevel());
}
auto getCurrentEXP() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getCurrentEXP());
}
auto getEXPToNextLevel() const -> exptype {
return static_cast<uint16_t>(pcclass->getEXPToNextLevel());
}
auto getCurrentHP() const -> uint16_t {
return static_cast<uint16_t>(pcclass->HP->getCurrent());
}
auto getMaxHP() const -> uint16_t {
return static_cast<uint16_t>(pcclass->HP->getMax());
}
auto getCurrentMP() const -> uint16_t {
if (!pcclass->MP) return 0;
return static_cast<uint16_t>(pcclass->MP->getCurrent());
}
auto getMaxMP() const -> uint16_t {
if (!pcclass->MP) return 0;
return static_cast<uint16_t>(pcclass->MP->getMax());
}
auto getBaseStrength() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getBaseStrength());
}
auto getTotalStrength() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getTotalStrength());
}
auto getBaseIntelligence() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getBaseInteligence());
}
auto getTotalIntelligence() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getTotalInteligence());
}
auto getBaseAgility() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getBaseAgility());
}
auto getTotalAgility() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getTotalAgility());
}
auto getBaseArmor() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getBaseArmor());
}
auto getTotalArmor() const -> uint16_t {
stattype armorFromArmor = 0;
for (const auto& armor : EquippedArmors) {
if (armor) {
armorFromArmor += armor->stats.Armor;
}
}
return static_cast<uint16_t>(pcclass->getTotalArmor() + armorFromArmor);
}
auto getTotalElementRes() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getTotalElementRes());
}
auto getBaseElementRes() const -> uint16_t {
return static_cast<uint16_t>(pcclass->getBaseElementRes());
}
auto getAbilities() const -> std::vector<Ability> {
return pcclass->Abilities;
}
auto getArmors(ARMORSLOTS slot) const -> std::string {
if (const auto& armor = EquippedArmors[static_cast<u_int16_t>(slot)];
armor) {
return armor->name;
}
return "None";
}
auto gainEXP(exptype exp) -> void { pcclass->gainExp(exp); }
auto takeDamage(welltype damage) -> void {
pcclass->HP->reduceCurrent(damage);
}
auto healDamage(welltype amount) -> void {
pcclass->HP->increaseCurrent(amount);
}
auto applyBuff(buff buff) -> void { pcclass->applyBuff(buff); }
auto equip(std::unique_ptr<item> item_to_equip) -> bool {
if (!item_to_equip || !item_to_equip->data) return false;
if (auto armorPtr = dynamic_cast<armor*>(item_to_equip->data.get());
armorPtr) {
auto slot_num = static_cast<std::uint16_t>(armorPtr->slot);
if (EquippedArmors[slot_num]) {
// Remove the existing armor and replace it (or move to inventory, etc.)
EquippedArmors[slot_num].reset();
}
// Equip the new armor in the designated slot
EquippedArmors[slot_num] = std::unique_ptr<equipmentDelegate>(
dynamic_cast<equipmentDelegate*>(item_to_equip->data.release()));
return true;
}
if (auto weaponPtr = dynamic_cast<weapon*>(item_to_equip->data.get());
weaponPtr) {
auto slot_num = static_cast<std::uint16_t>(weaponPtr->slot);
if (EquippedWeapon[slot_num]) {
// Remove the existing weapon and replace it (or move to inventory,
// etc.)
EquippedWeapon[slot_num].reset();
}
// Equip the new weapon in the designated slot
EquippedWeapon[slot_num] = std::unique_ptr<equipmentDelegate>(
dynamic_cast<equipmentDelegate*>(item_to_equip->data.release()));
return true;
}
return false;
}
auto use_item(std::unique_ptr<item> item_to_use) -> bool {
if (!item_to_use || !item_to_use->data) return false;
if (auto potionPtr = dynamic_cast<Potion*>(item_to_use->data.get());
potionPtr) {
if (potionPtr->Quantity > 0) {
if (potionPtr->healamount > 0 && pcclass->HP->isFull() == false) {
pcclass->HP->increaseCurrent(potionPtr->healamount);
potionPtr->Quantity--;
item_to_use.reset();
return true;
}
if (potionPtr->pbuff) {
pcclass->applyBuff(*potionPtr->pbuff);
potionPtr->Quantity--;
item_to_use.reset();
return true;
}
} else {
return false;
}
}
// other usable items
return false;
}
};
#define PCCONSTRUCT \
HP->setMax(BASEHP); \
HP->increaseCurrent(BASEHP); \
if (MP) { \
MP->setMax(BASEMP); \
MP->increaseCurrent(BASEMP); \
} \
increaseStats(BASESTR, BASEINT, BASEAGI);
#define LEVELUP \
auto new_max = static_cast<welltype>((BASEHP / 2.f) + HP->getMax()); \
auto new_current = static_cast<welltype>((BASEHP / 2.f)); \
HP->setMax(new_max); \
HP->increaseCurrent(new_current); \
if (MP) { \
new_max = ((BASEMP / 2.f) + MP->getMax()); \
new_current = ((BASEMP / 2.f)); \
MP->setMax(new_max); \
MP->increaseCurrent(new_current); \
} \
auto new_str = static_cast<stattype>((BASESTR + 1u) / 2.f); \
auto new_int = static_cast<stattype>((BASEINT + 1u) / 2.f); \
auto new_agi = static_cast<stattype>((BASEAGI + 1u) / 2.f); \
increaseStats(new_str, new_int, new_agi);
// #define CHARACTERCLASS(classname, basehp, basestr, baseint, baseagi) \
// class classname : public PlayerCharacterDelegate { \
// public: \
// static const welltype BASEHP = static_cast<welltype>(basehp); \
// static const stattype BASESTR = static_cast<stattype>(basestr); \
// static const stattype BASEINT = static_cast<stattype>(baseint); \
// static const stattype BASEAGI = static_cast<stattype>(baseagi); \
// classname() : PlayerCharacterDelegate() { PCCONSTRUCT } \
// auto getClassName() -> std::string override { return #classname; } \
// \
// private: \
// void LevelUp() override { LEVELUP } \
// };
class Cleric : public PlayerCharacterDelegate {
public:
static constexpr welltype BASEHP = static_cast<welltype>(14u);
static constexpr welltype BASEMP = static_cast<welltype>(10u);
static constexpr stattype BASESTR = static_cast<stattype>(3u);
static constexpr stattype BASEINT = static_cast<stattype>(5u);
static constexpr stattype BASEAGI = static_cast<stattype>(1u);
Cleric() : PlayerCharacterDelegate() {
MP = std::make_unique<PointWell>(BASEMP, BASEMP);
PCCONSTRUCT
Abilities.emplace_back("Heal", 2u, 1u, ABILITYTARGET::ALLY, 2u,
ABILITYSCALER::INT);
}
auto getClassName() -> std::string override { return "Cleric"; }
private:
void LevelUp() override {
LEVELUP
Abilities.emplace_back("Smite", 2u, 1u, ABILITYTARGET::ENEMY, 2u,
ABILITYSCALER::INT);
}
};
class Wizzard : public PlayerCharacterDelegate {
public:
static constexpr welltype BASEHP = static_cast<welltype>(10u);
static constexpr welltype BASEMP = static_cast<welltype>(14u);
static constexpr stattype BASESTR = static_cast<stattype>(1u);
static constexpr stattype BASEINT = static_cast<stattype>(8u);
static constexpr stattype BASEAGI = static_cast<stattype>(1u);
Wizzard() : PlayerCharacterDelegate() {
MP = std::make_unique<PointWell>(BASEMP, BASEMP);
PCCONSTRUCT
Abilities.emplace_back("Firebolt", 2u, 1u, ABILITYTARGET::ENEMY, 4u,
ABILITYSCALER::INT);
}
auto getClassName() -> std::string override { return "Wizzard"; }
private:
void LevelUp() override {
LEVELUP
if (getLevel() == 2) {
Abilities.emplace_back("Icebolt", 2u, 1u, ABILITYTARGET::ENEMY, 6u,
ABILITYSCALER::INT);
MP->setMax(MP->getMax() + 2u);
MP->increaseCurrent(1u);
increaseStats(0, 1, 0);
}
}
};
class Warrior : public PlayerCharacterDelegate {
public:
static constexpr welltype BASEHP = static_cast<welltype>(18u);
static constexpr welltype BASEMP = static_cast<welltype>(0u);
static constexpr stattype BASESTR = static_cast<stattype>(6u);
static constexpr stattype BASEINT = static_cast<stattype>(2u);
static constexpr stattype BASEAGI = static_cast<stattype>(2u);
Warrior() : PlayerCharacterDelegate() { PCCONSTRUCT }
auto getClassName() -> std::string override { return "Warrior"; }
private:
void LevelUp() override {
LEVELUP
if (getLevel() == 2) {
Abilities.emplace_back("PowerAttack", 0u, 3u, ABILITYTARGET::ENEMY, 4u,
ABILITYSCALER::STR);
}
}
};
class Rogue : public PlayerCharacterDelegate {
public:
static constexpr welltype BASEHP = static_cast<welltype>(12u);
static constexpr welltype BASEMP = static_cast<welltype>(0u);
static constexpr stattype BASESTR = static_cast<stattype>(3u);
static constexpr stattype BASEINT = static_cast<stattype>(3u);
static constexpr stattype BASEAGI = static_cast<stattype>(6u);
Rogue() : PlayerCharacterDelegate() { PCCONSTRUCT }
auto getClassName() -> std::string override { return "Rogue"; }
private:
void LevelUp() override {
LEVELUP
if (getLevel() == 2) {
Abilities.emplace_back("PreciseAttack", 0u, 3u, ABILITYTARGET::ENEMY, 6u,
ABILITYSCALER::AGI);
}
}
};
// CHARACTERCLASS(Cleric, 14u, 3u, 5u, 1u)
// CHARACTERCLASS(Wizzard, 10u, 1u, 7u, 1u)
// CHARACTERCLASS(Warrior, 18u, 5u, 2u, 2u)
// CHARACTERCLASS(Rogue, 12u, 4u, 4u, 5u)