From ab37629c6e4798654fca1d533a611da7986b5053 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Fri, 24 Jun 2022 17:10:50 -0400 Subject: Select level via command line (linux tested) --- src/Level.cpp | 63 ++++++++++++----------------------------------------------- 1 file changed, 13 insertions(+), 50 deletions(-) (limited to 'src/Level.cpp') 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 -#include #include -bool canStep(Pos pos, int dx, int dy, std::vector> map) { +bool canStep(const Pos &pos, int dx, int dy, + std::vector> 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(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; } -- cgit v1.2.3-54-g00ecf