summaryrefslogtreecommitdiff
path: root/src/Level.cpp
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-06-24 17:10:50 -0400
committerSteph Enders <smenders@gmail.com>2022-06-24 17:37:11 -0400
commitab37629c6e4798654fca1d533a611da7986b5053 (patch)
tree11b8a0b49bff176c9226b24b496588fd69f72cc3 /src/Level.cpp
parent20076f7271e19ecc259014681ba733047bea4d9b (diff)
Select level via command line (linux tested)
Diffstat (limited to 'src/Level.cpp')
-rw-r--r--src/Level.cpp63
1 files changed, 13 insertions, 50 deletions
diff --git a/src/Level.cpp b/src/Level.cpp
index 6db4b4e..5c8fbe0 100644
--- a/src/Level.cpp
+++ b/src/Level.cpp
@@ -27,17 +27,20 @@
#include "Level.h"
#include "SfmlUtils.h"
#include <fstream>
-#include <iostream>
#include <string>
-bool canStep(Pos pos, int dx, int dy, std::vector<std::vector<char>> map) {
+bool canStep(const 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);
+Level::Level(const char *filePath) {
+ this->file = std::make_unique<std::string>(filePath);
+}
+
+void Level::load() {
+ std::ifstream mapFile(this->file->c_str());
if (mapFile.is_open()) {
- this->file = filePath;
// each element in the map has a unique ID
// some magic but player is always 0
const int playerId = 0;
@@ -96,53 +99,13 @@ void Level::reset() {
this->treasurePositions.clear();
this->displayMap.clear();
this->enemyPositions.clear();
- this->loadLevelFromFile(this->file);
+ this->load();
}
-bool Level::isEmpty(int x, int y) { return map[y][x] == BLANK_SPACE; }
-
-bool Level::playerCanStep(int dx, int dy) {
+bool Level::playerCanStep(int dx, int dy) const {
return canStep(player, dx, dy, map);
}
-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++; }
int Level::getEnemyIndex(int id) {
@@ -154,8 +117,8 @@ int Level::getEnemyIndex(int id) {
return -1;
}
-bool Level::enemyCanStep(Pos pos, int dx, int dy) {
+bool Level::enemyCanStep(const Pos &pos, int dx, int dy) const {
return canStep(pos, dx, dy, map);
}
-int Level::getWidth() { return this->width; }
-int Level::getHeight() { return this->height; }
+int Level::getWidth() const { return this->width; }
+int Level::getHeight() const { return this->height; }