summaryrefslogtreecommitdiff
path: root/src/Level.cpp
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-06-15 23:36:43 -0400
committerSteph Enders <smenders@gmail.com>2022-06-15 23:36:43 -0400
commiteaaf57114cfb74c29f1b0c4e9786bed6d225225c (patch)
tree8f4946942a9f93f2bba8b1c4c39045e8ee02811d /src/Level.cpp
Initial setup commit
This setups up the project for messing around with C++ and Lua bindings. So far this project just prints the defined dng map and lets you move the character around. What this fails to do is actually provide any reason to use Lua at the moment. So I need to figure out some way of enabling logic on the processing side of things.
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