From c57ae8c42c1f2f2ed576719c00cff5cf613fe650 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Thu, 16 Jun 2022 16:14:18 -0400 Subject: Added onUpdate logic to move the enemies etc Created some algorithm logic for enemy movement Allowed for default overrides --- src/Level.cpp | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'src/Level.cpp') diff --git a/src/Level.cpp b/src/Level.cpp index 013c2a1..5f06def 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -3,9 +3,11 @@ #include #include -void -Level::loadLevelFromFile(const char* filePath) -{ +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()) { @@ -15,7 +17,6 @@ Level::loadLevelFromFile(const char* filePath) // 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)) { @@ -28,14 +29,14 @@ Level::loadLevelFromFile(const char* filePath) this->map[y].push_back(BLANK_SPACE); } else if (c == ENEMY_TKN) { this->enemyPositions.push_back( - { .id = this->nextId(), .x = x, .y = y }); + {.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->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 }); + {.id = this->nextId(), .x = x, .y = y}); this->map[y].push_back(BLANK_SPACE); } else { continue; @@ -48,26 +49,17 @@ Level::loadLevelFromFile(const char* filePath) mapFile.close(); } -bool -Level::isEmpty(int x, int y) -{ - return map[y][x] == BLANK_SPACE; -} +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; +bool Level::playerCanStep(int dx, int dy) { + return canStep(player, dx, dy, map); } -void -Level::print() -{ +void Level::print() { int x = 0; int y = 0; - for (auto& row : map) { - for (auto& tile : row) { + for (auto &row : map) { + for (auto &tile : row) { bool printed = false; if (player.x == x && player.y == y) { std::cout << "p"; @@ -101,8 +93,17 @@ Level::print() } } -int -Level::nextId() -{ - return idCounter++; -} \ No newline at end of file +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); +} -- cgit v1.2.3-54-g00ecf