From 7073d9f97b54f41d227b960d05dd28af74d5776d Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 6 Jun 2015 17:56:30 +0200 Subject: Refactored reading in game state file --- include/game_state.h | 2 ++ src/game_state.cpp | 81 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/include/game_state.h b/include/game_state.h index db20b84..1b35fc6 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -19,4 +19,6 @@ private: std::vector missiles; std::vector shields; std::vector spaceships; + + int addEntity(int x, int y, char type); }; diff --git a/src/game_state.cpp b/src/game_state.cpp index e8151e3..59eb8c1 100644 --- a/src/game_state.cpp +++ b/src/game_state.cpp @@ -6,6 +6,24 @@ const int OPENING_LINES = 6; const int GAME_AREA_LINES = 25; +void moveToNextChar(int &x, int &y, int &width, char &nextChar, std::ifstream &mapFile) +{ + if (nextChar == '\n') + { + x = 0; + ++y; + } + else + { + x += width; + } + if (width > 1) + { + mapFile.ignore(width-1); + } + nextChar = mapFile.get(); +} + GameState::GameState(std::string mapFilename) { std::ifstream mapFile(mapFilename); @@ -13,44 +31,39 @@ GameState::GameState(std::string mapFilename) { mapFile.ignore(std::numeric_limits::max(), '\n'); } - - int y = 0; - for (int x=-1; y <= GAME_AREA_LINES; ++x) + char nextChar = mapFile.get(); + for (int x=0, y=0, width=1; + y < GAME_AREA_LINES && nextChar != EOF; + moveToNextChar(x, y, width, nextChar, mapFile)) { - char nextChar = mapFile.get(); - if (nextChar == EOF) - { - break; - } + width = addEntity(x, y, nextChar); + } +} - switch (nextChar) - { - case Alien::MAP_CHAR: - aliens.push_back(Alien(x,y)); - break; - case EnemyBullet::ALIEN_MAP_CHAR: - case EnemyBullet::ENEMY_MISSILE_MAP_CHAR: - bullets.push_back(EnemyBullet(x,y)); - break; - case PlayerMissile::MAP_CHAR: - missiles.push_back(PlayerMissile(x,y)); - break; - case Shield::MAP_CHAR: - shields.push_back(Shield(x,y)); - break; - case Spaceship::ENEMY_MAP_CHAR: - case Spaceship::PLAYER_MAP_CHAR: - spaceships.push_back(Spaceship(x+1,y)); - x += 2; - mapFile.ignore(2); - break; - case '\n': - ++y; - x = -1; - break; - } +int GameState::addEntity(int x, int y, char type) +{ + switch (type) + { + case Alien::MAP_CHAR: + aliens.push_back(Alien(x,y)); + return 1; + case EnemyBullet::ALIEN_MAP_CHAR: + case EnemyBullet::ENEMY_MISSILE_MAP_CHAR: + bullets.push_back(EnemyBullet(x,y)); + return 1; + case PlayerMissile::MAP_CHAR: + missiles.push_back(PlayerMissile(x,y)); + return 1; + case Shield::MAP_CHAR: + shields.push_back(Shield(x,y)); + return 1; + case Spaceship::ENEMY_MAP_CHAR: + case Spaceship::PLAYER_MAP_CHAR: + spaceships.push_back(Spaceship(x+1,y)); + return 3; } + return 1; } void GameState::logState() -- cgit v1.2.3