summaryrefslogtreecommitdiff
path: root/src/Level.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Level.cpp')
-rw-r--r--src/Level.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/Level.cpp b/src/Level.cpp
new file mode 100644
index 0000000..013c2a1
--- /dev/null
+++ b/src/Level.cpp
@@ -0,0 +1,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++;
+} \ No newline at end of file