-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicAttackAction.cpp
More file actions
41 lines (33 loc) · 1.43 KB
/
BasicAttackAction.cpp
File metadata and controls
41 lines (33 loc) · 1.43 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
#include "BasicAttackAction.hpp"
#include "Battle.hpp"
#include "BattleLog.hpp"
#include "BattleRules.hpp"
#include "Character.hpp"
BasicAttackAction::BasicAttackAction(std::shared_ptr<Character> attacker,
std::shared_ptr<Character> defender)
: Action(std::move(attacker), 0), target(std::move(defender)) {}
bool BasicAttackAction::validate(Battle& battle) const {
(void)battle;
return actor && actor->isAlive() && target && target->isAlive();
}
void BasicAttackAction::execute(Battle& battle) {
battle.getLog()->add(" " + actor->getName() + " attacks " + target->getName() + "!");
if (!battle.getRules().rollHit(actor, target)) { // getRules() returns the object of BattleRules class
battle.getLog()->add(" Miss!");
return;
}
int dmg = battle.getRules().computeDamage(actor, target, 5, DamageType::Physical);
target->takeDamage(dmg);
battle.getLog()->add(" Deals " + std::to_string(dmg) + " damage. (" +
target->getName() + ": " + std::to_string(target->getHP()) +
"/" + std::to_string(target->getMaxHP()) + " HP)");
if (!target->isAlive()) {
battle.getLog()->add(" " + target->getName() + " is defeated!");
}
}
std::string BasicAttackAction::getActionName() const {
return "Basic Attack";
}
const std::shared_ptr<Character>& BasicAttackAction::getTarget() const {
return target;
}