#include "Level.h" #include #include #include bool canStep(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); 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::playerCanStep(int dx, int dy) { 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) { 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); }