diff options
| author | Steph Enders <smenders@gmail.com> | 2022-06-16 16:14:18 -0400 | 
|---|---|---|
| committer | Steph Enders <smenders@gmail.com> | 2022-06-16 16:16:34 -0400 | 
| commit | c57ae8c42c1f2f2ed576719c00cff5cf613fe650 (patch) | |
| tree | 0e0c6533b099edbe2c283a8961b1b240d29ad904 /src/Level.cpp | |
| parent | 750b308d70cf1c903812316deeb0a8c4befa37ce (diff) | |
Added onUpdate logic to move the enemies etc
Created some algorithm logic for enemy movement
Allowed for default overrides
Diffstat (limited to 'src/Level.cpp')
| -rw-r--r-- | src/Level.cpp | 55 | 
1 files changed, 28 insertions, 27 deletions
| 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 <iostream>  #include <string> -void -Level::loadLevelFromFile(const char* filePath) -{ +bool canStep(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);    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); +} |