aboutsummaryrefslogtreecommitdiff
path: root/backend/Player.cpp
blob: 66197faa42fc291c7e960aee5a5ce5effa3adcc2 (plain)
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
104
105
106
107
108
109
#include <assert.h>

#include "Player.h"
#include "Exception.h"
#include "print.h"
#include "Dungeon.h"
#include "util.h"

Player::Player(Dungeon & dungeon) : dungeon(dungeon) { }
Player::~Player() {
	safe_free(&this->weapon);
	safe_free(&this->armor);
}

float Player::get_attack() const {
	if (this->cheating) return 1.f;
	return this->attack_chance;
}

unsigned Player::get_health() const {
	return this->health_points;
}

bool Player::is_dead() const {
	return this->health_points == 0;
}

void Player::take_damage(unsigned int dmg) {
	if (this->cheating) return;
	if (this->is_dead()) return;

	// reduce damage by armor's protection value (if player is wearing any)
	if (this->armor != nullptr)
		dmg -= min(dmg, this->armor->get_protection());

	// make sure health_points doesn't go below 0
	dmg = min(dmg, this->health_points);

	this->health_points -= dmg;

	auto & hp = this->health_points;
	lprtf("Je hebt %s%d levenspunt%s over.\n", hp > 0 ? "nog " : "", hp, hp == 1 ? "" : "en");
}

Location & Player::get_location() const {
	assert(this->location != nullptr);
	return *this->location;
}

void Player::set_location(Location & location) {
	this->location = &location;
	lprtf("Je staat nu bij de locatie %s.\n", location.get_name().c_str());
}

void Player::equip(Object * object) {
	if (object == nullptr) return;

	auto weapon = dynamic_cast<WeaponObject *>(object);
	if (weapon != nullptr)
		return this->equip(weapon);

	auto armor = dynamic_cast<ArmorObject *>(object);
	if (armor != nullptr)
		return this->equip(armor);

	throw Exception("object \"%s\" is niet draagbaar", object->get_name().c_str());
}

void Player::equip(WeaponObject * weapon) noexcept {
	if (weapon == nullptr) return;

	lprtf("Je ");
	if (this->weapon != nullptr) {
		lprtf("laat %s vallen en ", this->weapon->get_name().c_str());
		this->inventory.push_back(this->weapon);
	}
	this->weapon = weapon;
	lprtf("pakt %s vast.\n", this->weapon->get_name().c_str());
}

void Player::equip(ArmorObject * armor) noexcept {
	if (armor == nullptr) return;

	lprtf("Je ");
	if (this->armor != nullptr) {
		lprtf("trekt %s uit en ", this->armor->get_name().c_str());
		this->inventory.push_back(this->armor);
	}
	this->armor = armor;
	lprtf("doet %s aan.\n", this->armor->get_name().c_str());
}

void Player::add_health(unsigned int bonus) {
	if (this->is_dead()) return;
	this->health_points += bonus;
	lprtf("Je hebt %d levenspunt%s erbij gekregen.\n", bonus, bonus == 1 ? "" : "en");
}

void Player::add_attack(float bonus) {
	float max_bonus = 0.90f - this->attack_chance;
	if (max_bonus < 0.f) {
		lprtf("Je aanvalskans is niet verder verhoogd.\n");
		return;
	}
	bonus = min(max_bonus, bonus);
	this->attack_chance += bonus;
	lprtf("Je aanvalskans is verhoogd met %.1f%%.\n", bonus * 100);
}