blob: 013c2a11eda3b9c089a68a6783165ec679e548bd (
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
|
#include "Level.h"
#include <fstream>
#include <iostream>
#include <string>
void
Level::loadLevelFromFile(const char* filePath)
{
std::ifstream mapFile(filePath);
if (mapFile.is_open()) {
// each element in the map has a unique ID
// some magic but player is always 0
const int playerId = 0;
// from 1 -> N each enemy and treasure has its own unique ID
// IDs are unique entirely, not just per enemy or treasure
std::string line;
int y = 0;
while (std::getline(mapFile, line)) {
this->map.emplace_back();
int x = 0;
for (char c : line) {
if (c == WALL_TKN) {
this->map[y].push_back(WALL_SPACE);
} else if (c == EMPTY_TKN) {
this->map[y].push_back(BLANK_SPACE);
} else if (c == ENEMY_TKN) {
this->enemyPositions.push_back(
{ .id = this->nextId(), .x = x, .y = y });
this->map[y].push_back(BLANK_SPACE);
} else if (c == PLAYER_TKN) {
this->player = { .id = playerId, .x = x, .y = y };
this->map[y].push_back(BLANK_SPACE);
} else if (c == TREASURE_TKN) {
this->treasurePositions.push_back(
{ .id = this->nextId(), .x = x, .y = y });
this->map[y].push_back(BLANK_SPACE);
} else {
continue;
}
++x;
}
++y;
}
}
mapFile.close();
}
bool
Level::isEmpty(int x, int y)
{
return map[y][x] == BLANK_SPACE;
}
bool
Level::canStep(int dx, int dy)
{
bool res = map[player.y + dy][player.x + dx] != WALL_SPACE;
return res;
}
void
Level::print()
{
int x = 0;
int y = 0;
for (auto& row : map) {
for (auto& tile : row) {
bool printed = false;
if (player.x == x && player.y == y) {
std::cout << "p";
printed = true;
}
for (auto pos : enemyPositions) {
if (pos.x == x && pos.y == y) {
std::cout << "e";
printed = true;
}
}
for (auto pos : treasurePositions) {
if (pos.x == x && pos.y == y) {
std::cout << "t";
printed = true;
}
}
if (tile == WALL_SPACE) {
std::cout << tile;
printed = true;
}
if (!printed) {
std::cout << " ";
}
std::cout << " ";
++x;
}
std::cout << "\n";
++y;
x = 0;
}
}
int
Level::nextId()
{
return idCounter++;
}
|