aboutsummaryrefslogtreecommitdiff
path: root/backend/Dungeon.cpp
blob: 3fe9a5809111a9773c7eb4e0d761571742f051e2 (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
#include <assert.h>

#include "Location.h"
#include "Dungeon.h"
#include "RNG.h"
#include "ListIterator.h"
#include "print.h"

Dungeon::Dungeon() : player(*this) { }

void Dungeon::update() {
	ListRange<Enemy *> enemies = this->player.get_location().get_enemies();
	if (enemies.size() > 0)
		this->update_attacks(enemies);
	else
		this->update_movement();
}

void Dungeon::update_attacks(ListRange<Enemy *> & enemies) {
	bool plural = enemies.size() != 1;
	RNG & rng = RNG::get();
	bool first = true;
	for (Enemy * enemy : enemies) {
		if (enemy->is_dead()) continue;
		if (rng.rand_double() < enemy->get_attack()) continue;
		if (first)
			lprtf(":: De %s in je locatie %s aan! ::\n", plural ? "vijanden" : "vijand", plural ? "vallen" : "valt");

		unsigned damage = rng.rand_int(enemy->get_damage());
		lprtf("%s raakt en doet %d punt schade.\n", enemy->get_displayname().c_str(), damage);
		this->player.take_damage(damage);
		first = false;
	}
}

void Dungeon::update_movement() {
	bool moved = false;
	for (Location * location : this->locations) {
		for (Enemy * enemy : location->get_enemies()) {
			if (enemy->is_dead()) continue;
			if (RNG::get().rand_double() < 0.5) continue;

			if (!moved)
				lprtf(":: De vijanden bewegen ::\n");
			Direction direction = random_direction(*location);
			location->remove_enemy(enemy);
			Location & new_location = location->get_exit(direction);
			new_location.add_enemy(enemy);
			if (&this->player.get_location() == &new_location)
				lprtf("%s komt de huidige locatie binnen\n", enemy->get_name().c_str());
			moved = true;
		}
	}
}

void Dungeon::add_location(Location * location) {
	this->locations.push_back(location);
}

Location & Dungeon::get_start_location() {
	size_t size = this->locations.size();
	assert(size > 0);
	size_t index = RNG::get().rand_int(size);
	return *this->locations[index];
}

Player & Dungeon::get_player() {
	return this->player;
}