summaryrefslogtreecommitdiff
path: root/src/Level.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Level.cpp')
-rw-r--r--src/Level.cpp55
1 files changed, 28 insertions, 27 deletions
diff --git a/src/Level.cpp b/src/Level.cpp
index 013c2a1..5f06def 100644
--- a/src/Level.cpp
+++ b/src/Level.cpp
@@ -3,9 +3,11 @@
#include <iostream>
#include <string>
-void
-Level::loadLevelFromFile(const char* filePath)
-{
+bool canStep(Pos pos, int dx, int dy, std::vector<std::vector<char>> map) {
+ return map[pos.y + dy][pos.x + dx] != WALL_SPACE;
+}
+
+void Level::loadLevelFromFile(const char *filePath) {
std::ifstream mapFile(filePath);
if (mapFile.is_open()) {
@@ -15,7 +17,6 @@ Level::loadLevelFromFile(const char* filePath)
// 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)) {
@@ -28,14 +29,14 @@ Level::loadLevelFromFile(const char* filePath)
this->map[y].push_back(BLANK_SPACE);
} else if (c == ENEMY_TKN) {
this->enemyPositions.push_back(
- { .id = this->nextId(), .x = x, .y = y });
+ {.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->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 });
+ {.id = this->nextId(), .x = x, .y = y});
this->map[y].push_back(BLANK_SPACE);
} else {
continue;
@@ -48,26 +49,17 @@ Level::loadLevelFromFile(const char* filePath)
mapFile.close();
}
-bool
-Level::isEmpty(int x, int y)
-{
- return map[y][x] == BLANK_SPACE;
-}
+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;
+bool Level::playerCanStep(int dx, int dy) {
+ return canStep(player, dx, dy, map);
}
-void
-Level::print()
-{
+void Level::print() {
int x = 0;
int y = 0;
- for (auto& row : map) {
- for (auto& tile : row) {
+ for (auto &row : map) {
+ for (auto &tile : row) {
bool printed = false;
if (player.x == x && player.y == y) {
std::cout << "p";
@@ -101,8 +93,17 @@ Level::print()
}
}
-int
-Level::nextId()
-{
- return idCounter++;
-} \ No newline at end of file
+int Level::nextId() { return idCounter++; }
+
+int Level::getEnemyIndex(int id) {
+ for (int i = 0; i < enemyPositions.size(); i++) {
+ if (enemyPositions[i].id == id) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+bool Level::enemyCanStep(Pos pos, int dx, int dy) {
+ return canStep(pos, dx, dy, map);
+}