-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattle-handler.cpp
More file actions
106 lines (92 loc) · 3.77 KB
/
Copy pathbattle-handler.cpp
File metadata and controls
106 lines (92 loc) · 3.77 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
#include "game-actors.hpp"
#include "battle-handler.hpp"
#include <iostream>
#include <memory>
#include <string>
#include "game-elements.hpp"
#include "utilities.hpp"
using std::cout; using std::string; using std::cin;
using std::shared_ptr;
// GENERAL
BattleHandler::BattleHandler(shared_ptr<Player> player) : player_(player), playerTurn_(true) {};
bool BattleHandler::battleHasEnded() {return (player_->getHealth() <= 0 || enemy_->getHealth() <= 0);}
void BattleHandler::startBattle(shared_ptr<Actor> enemy) {
enemy_ = enemy;
while (!battleHasEnded()) {
if (playerTurn_) {
handlePlayerTurn();
} else {
handleEnemyTurn();
}
playerTurn_ = !playerTurn_;
}
displayBattleOutcome();
}
// Turn handling
void BattleHandler::handlePlayerTurn() {
displayPlayerTurnInfo();
// put additional calculations here
int i = -1;
while (i < 0 || i >= player_->getHarmonics().size()) {
cout << "[0 - " << (player_->getHarmonics().size() - 1) << "]: ";
cin >> i;
if (i < 0 || i >= player_->getHarmonics().size())
cout << "\n=-- Invalid! Please try again. --=\n\n";
}
shared_ptr<Harmonic> harmonic = player_->getHarmonics()[i];
harmonic->use([this, harmonic](int dmg) {
cout << "\nYou attack with: " << harmonic->getName() << " for: " << std::flush;
sleep(1);
cout << dmg << " DAMAGE!\n\n" << std::flush;
sleep(1);
handleDamageToTarget(enemy_, dmg);
});
}
void BattleHandler::handleEnemyTurn() {
displayEnemyTurnInfo();
// put additional calculations here
// dmg * dmgMultiplier
// crit chance
shared_ptr<Harmonic> harmonic = player_->getHarmonics()[0];
if (harmonic->getType() == Harmonic::Type::attack) {
harmonic->use([this, harmonic](int dmg){
cout << enemy_->getName() << " attacks you with: " << enemy_->getHarmonics()[0]->getName() << " for: " << std::flush;
sleep(1);
cout << dmg << " DAMAGE!\n\n" << std::flush;
sleep(1);
handleDamageToTarget(player_, dmg);
});
}
}
// Damage handling
void BattleHandler::handleDamageToTarget(shared_ptr<Actor> target, int dmg) {
// put additional calculations here
int newHealth = target->getHealth() - dmg;
target->setHealth(newHealth);
}
// DISPLAYS
void BattleHandler::displayPlayerTurnInfo() {
cout << "---------------------------------------------------------------------------------------------\n";
cout << "\n========Your Move========\n\n";
cout << "Your health: (" << player_->getHealth() << ')' << '\n' << "Your resonance: (" << player_->getResonance() << ")\n\n";
cout << enemy_->getName() << "'s health: (" << enemy_->getHealth() << ')' << '\n' << enemy_->getName()<< "'s resonance: " << '(' << enemy_->getResonance() << ")\n\n";
cout << "Choose a Harmonic!\n" << "-----------------\n";
for (int i = 0; i < player_->getHarmonics().size(); ++i) {
std::cout << "[" << i << "] " << player_->getHarmonics()[i]->getName() << '\n';
std::cout << " " << "Description: " << player_->getHarmonics()[i]->getDesc() << '\n';
std::cout << " " << "Resonance Requirement: " << player_->getHarmonics()[i]->getResReq() << '\n';;
}
}
void BattleHandler::displayEnemyTurnInfo() {
cout << "---------------------------------------------------------------------------------------------\n";
cout << "\n========Enemies Move========\n\n";
}
void BattleHandler::displayBattleOutcome() {
if (player_->getHealth() <= 0) {
cout << "\nYou have died...\n\n";
printSkull();
} else {
cout << "\nYou have defeated: " << enemy_->getName() << ", you are a glorious warrior!\n\n";
printTrophy();
}
}