-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharacter.cpp
More file actions
251 lines (198 loc) · 6.18 KB
/
Copy pathCharacter.cpp
File metadata and controls
251 lines (198 loc) · 6.18 KB
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
#include "Character.hpp"
#include "Battle.hpp"
#include "BattleLog.hpp"
#include "Skill.hpp"
#include "StatusEffect.hpp"
#include "Buff.hpp"
#include "Party.hpp"
#include "StatModifier.hpp"
#include <algorithm>
Character::Character(std::string charName, int charLevel, StatBlock stats,
int startWeight, int weightLimit)
: name(std::move(charName)), level(charLevel), baseStats(stats),
hp(stats.getMaxHP()), mana(0, 100), weight(startWeight),
maxWeight(weightLimit), defending(false), ownerParty(nullptr) {}
const std::string& Character::getName() const {
return name;
}
int Character::getHP() const {
return hp;
}
int Character::getMaxHP() const {
return getFinalStat(StatType::MaxHP);
}
int Character::getLevel() const {
return level;
}
int Character::getWeight() const {
return weight;
}
int Character::getMaxWeight() const {
return maxWeight;
}
int Character::getMana() const {
return mana.getCurrent();
}
int Character::getMaxMana() const {
return mana.getMax();
}
bool Character::isDefending() const {
return defending;
}
Party* Character::getOwnerParty() const {
return ownerParty;
}
const std::vector<std::shared_ptr<Skill>>& Character::getSkills() const {
return skills;
}
const StatBlock& Character::getBaseStats() const {
return baseStats;
}
bool Character::isAlive() const {
return hp > 0;
}
bool Character::isOverencumbered() const {
return weight > maxWeight;
}
void Character::setOwnerParty(Party* party) {
ownerParty = party;
}
void Character::setDefending(bool def) {
defending = def;
}
void Character::setLevel(int lvl) {
level = std::max(1, lvl);
}
void Character::addWeight(int amount) {
weight += amount;
checkOverencumbered();
}
void Character::removeWeight(int amount) {
weight = std::max(0, weight - amount);
}
void Character::increaseAttack(int amount) {
baseStats.increaseStat(StatType::Attack, amount);
}
void Character::takeDamage(int amount) {
hp = std::max(0, hp - std::max(0, amount));
}
void Character::heal(int amount) {
hp = std::min(getMaxHP(), hp + std::max(0, amount));
}
void Character::consumeMana(int amount) {
mana.consume(amount);
}
void Character::restoreMana(int amount) {
mana.restore(amount);
}
void Character::addSkill(std::shared_ptr<Skill> s) {
skills.push_back(std::move(s));
}
void Character::removeSkill(const std::shared_ptr<Skill>& s) {
skills.erase(std::remove(skills.begin(), skills.end(), s), skills.end());
}
void Character::applyStatus(Battle& battle, std::shared_ptr<StatusEffect> status) {
if (!status) return;
statuses.push_back(status);
battle.getLog()->add(" " + name + " gains status: " + status->getName());
status->onApply(battle, shared_from_this());
}
void Character::applyBuff(Battle& battle, std::shared_ptr<Buff> buff) {
if (!buff) return;
buffs.push_back(buff);
battle.getLog()->add(" " + name + " gains buff: " + buff->getName());
}
void Character::tickStatuses(Battle& battle, TickTiming when) {
for (auto& status : statuses) {
if (status && status->getTiming() == when) {
status->onTick(battle, shared_from_this());
status->decrement();
if (status->expired()) {
battle.getLog()->add(" " + name + "'s " + status->getName() + " expired");
status->onExpire(battle, shared_from_this());
}
}
}
statuses.erase(
std::remove_if(statuses.begin(), statuses.end(),
[](const std::shared_ptr<StatusEffect>& s) { return !s || s->expired(); }),
statuses.end()
);
}
void Character::tickBuffs(Battle& battle) {
for (auto& buff : buffs) {
if (buff) {
buff->decrementDuration();
if (buff->isExpired()) {
battle.getLog()->add(" " + name + "'s " + buff->getName() + " expired");
}
}
}
buffs.erase(
std::remove_if(buffs.begin(), buffs.end(),
[](const std::shared_ptr<Buff>& b) { return !b || b->isExpired(); }),
buffs.end()
);
}
void Character::tickCooldowns() {
for (auto& skill : skills) {
if (skill) {
skill->decrementCooldown();
}
}
}
bool Character::canAct() const {
for (const auto& status : statuses) {
if (status && status->blocksAction()) {
return false;
}
}
return true;
}
void Character::checkOverencumbered() {
// This is called automatically when weight changes
}
int Character::getFinalStat(StatType type) const {
int base = baseStats.getStat(type);
int flatSum = 0;
float mult = 1.0f;
// Apply status modifiers
for (const auto& status : statuses) {
if (!status) continue;
for (const auto& mod : status->modifiers()) {
if (mod.getType() == type) {
flatSum += mod.getFlat();
mult *= mod.getMult();
}
}
}
int val = static_cast<int>((base + flatSum) * mult);
return std::max(0, val);
}
int Character::getModifiedAttack() const {
int baseAttack = getFinalStat(StatType::Attack);
// Apply attack buffs - identified by name since new Buff uses name-based dispatch
for (const auto& buff : buffs) {
if (buff && buff->getName() == "StrengthUp") {
baseAttack += 10; // magnitude mirrors applyEffect() hardcoded value in Buff.cpp
}
}
return baseAttack;
}
int Character::getModifiedDefense() const {
int baseDefense = getFinalStat(StatType::Defense);
// Apply defense buffs - identified by name since new Buff uses name-based dispatch
for (const auto& buff : buffs) {
if (buff && buff->getName() == "DefenseUp") {
baseDefense += 10; // magnitude mirrors applyEffect() hardcoded value in Buff.cpp
}
}
return baseDefense;
}
void Character::applyNerf(int percentage, Battle& battle) {
int damage = (hp * percentage) / 100;
takeDamage(damage);
battle.getLog()->add(" " + name + " is nerfed! Lost " +
std::to_string(damage) + " HP (" +
std::to_string(percentage) + "% of current health)");
}