-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDestructible.cpp
66 lines (57 loc) · 1.66 KB
/
Destructible.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
#include "stdafx.h"
Destructible::Destructible(float maxHp, float defense, const char *corpseName, int xp) :
maxHp(maxHp), hp(maxHp), defense(defense), xp(xp) {
this->corpseName = _strdup(corpseName);
}
Destructible::~Destructible() {
free((char*)corpseName);
}
float Destructible::takeDamage(Actor *owner, float damage) {
damage -= defense;
if (damage > 0) {
hp -= damage;
if (hp <= 0) {
hp = 0; // to make sure hp is not negative
die(owner);
}
}
else {
damage = 0;
}
return damage;
}
float Destructible::heal(float amount) {
hp += amount;
if (hp > maxHp) {
amount -= hp - maxHp; // so it returns the amount healed
hp = maxHp;
}
return amount;
}
void Destructible::die(Actor *owner) {
// transform the actor into a corpse
owner->ch = '%';
owner->col = TCODColor::darkestGreen;
owner->name = corpseName;
owner->blocks = false;
// make sure the corpses are drawn before living actors
engine.sendToBack(owner);
}
MonsterDestructible::MonsterDestructible(float maxHp, float defense, const char *corpseName, int xp) :
Destructible(maxHp, defense, corpseName, xp) {
}
void MonsterDestructible::die(Actor *owner) {
// transform it into a corpse, doesn't block, can't be atacked and doesn't move
engine.gui->message(TCODColor::white, "%s is dead. You gain %d xp",
owner->name, xp);
engine.player->destructible->xp += xp;
Destructible::die(owner);
}
PlayerDestructible::PlayerDestructible(float maxHp, float defense, const char *corpseName) :
Destructible(maxHp, defense, corpseName, 0) {
}
void PlayerDestructible::die(Actor *owner) {
engine.gui->message(TCODColor::red, "You died!");
Destructible::die(owner);
engine.gameStatus = Engine::DEFEAT;
}