-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.cpp
95 lines (75 loc) · 2.4 KB
/
actor.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
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
#include "actor.h"
// constructor
Actor::Actor(int x, int y, int hitPoints, int armorPoints, int strengthPoints, int dexterityPoints)
: position(x, y), hitPoints(hitPoints), armorPoints(armorPoints),
strengthPoints(strengthPoints), dexterityPoints(dexterityPoints), weapon(nullptr) {}
// actor coordinates
Coordinate Actor::GetPosition() const {
return position;
}
void Actor::SetPosition(int x, int y) {
position.SetX(x);
position.SetY(y);
}
// actor statistics
int Actor::GetHitPoints() const {
return hitPoints;
}
void Actor::SetHitPoints(int hitPoints) {
this->hitPoints = hitPoints;
}
int Actor::GetArmorPoints() const {
return armorPoints;
}
void Actor::SetArmorPoints(int armorPoints) {
this->armorPoints = armorPoints;
}
int Actor::GetStrengthPoints() const {
return strengthPoints;
}
void Actor::SetStrengthPoints(int strengthPoints) {
this->strengthPoints = strengthPoints;
}
int Actor::GetDexterityPoints() const {
return dexterityPoints;
}
void Actor::SetDexterityPoints(int dexterityPoints) {
this->dexterityPoints = dexterityPoints;
}
// actor weapon/item
std::shared_ptr<Weapons> Actor::GetWeapon() const {
return weapon;
}
void Actor::SetWeapon(std::shared_ptr<Weapons> weapon) {
this->weapon = weapon;
}
bool Actor::IsDead() const {
return hitPoints <= 0;
}
// all actors can attack functionality
void Actor::Attack(Actor& target, Game& game) {
auto weapon = GetWeapon();
if (!weapon) return;
// Calculate the attack and defense points
int attackerPoints = GetDexterityPoints() + weapon->GetDexterityBonus();
int defenderPoints = target.GetDexterityPoints() + target.GetArmorPoints();
// Determine if the attack hits
if (randInt(1, attackerPoints) >= randInt(1, defenderPoints)) {
attackedHit = true;
// Calculate damage
int damagePoints = randInt(0, GetStrengthPoints() + weapon->GetDamageAmount() - 1);
target.SetHitPoints(target.GetHitPoints() - damagePoints);
// Check if the monster is dead
if (target.IsDead()) {
target.OnDeath(game);
finalBlow = true;
} else {
finalBlow = false;
}
} else {
// Attack missed
attackedHit = false;
finalBlow = false;
}
attacked = true;
}