-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combat.cpp
46 lines (36 loc) · 1.6 KB
/
Combat.cpp
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
#include "Combat.h"
Combat::Combat(Player& player, Enemy& enemy, Dungeon& dungeon)
: player(player), enemy(enemy), dungeon(dungeon) {}
std::string Combat::playerTurn() {
return calculateDamage(player.getName(), player, enemy.getName(), enemy);
}
std::string Combat::enemyTurn() {
return calculateDamage(enemy.getName(), enemy, player.getName(), player);
}
std::string Combat::calculateDamage(const std::string& attackerName, Entity& attacker, const std::string& defenderName, Entity& defender) {
int actualDamage = attacker.getDamage();
// Check for critical hit
float randValue = static_cast<float>(rand()) / RAND_MAX;
if (randValue < attacker.getCriticalDamagePercent()) {
actualDamage = static_cast<int>(actualDamage * attacker.getCriticalDamageMultiplier());
}
// Calculate damage after resistance
int damageDealt = actualDamage - defender.getResistance();
if (damageDealt < 0) damageDealt = 0;
// Apply damage to defender
int newHealth = defender.getHealth() - damageDealt;
defender.setHealth(newHealth);
// Return attack information as a string
return attackerName + " dealt " + std::to_string(damageDealt) + " damage to " + defenderName + "!";
}
std::string Combat::checkForDeaths() {
if (!player.getIsAlive()) {
return "Player has been defeated!";
}
if (!enemy.getIsAlive()) {
int expGained = 5 + (dungeon.getLevel() * 2);
player.gainExperience(expGained);
return "Enemy " + enemy.getName() + " has been defeated! You gained " + std::to_string(expGained) + " experience points!";
}
return "";
}