Rally X
ELEN3009 Project by Justin Wernick and David Schneider
|
00001 #include "LevelReader.h" 00002 00003 LevelReader::LevelReader(string filename) 00004 :_filename(filename) 00005 {} 00006 00007 void LevelReader::readLevel(Maze& maze, list<PlayerCar>& players, list<EnemyCar>& enemies, list<Checkpoint>& checkpoints, list<Rock>& rocks) 00008 { 00009 ifstream file(_filename.c_str()); 00010 if (!file) 00011 { 00012 al_show_native_message_box(NULL, "Fatal error", "Fatal error", "The requested level file could not be opened.", NULL, ALLEGRO_MESSAGEBOX_ERROR); 00013 throw FileOpenError(); 00014 } 00015 00016 int maxX = 0; 00017 int maxY = 0; 00018 00019 string line; 00020 char element; 00021 int y = 0; 00022 vector <pair<int, int> > walls; 00023 00024 while (!file.eof()) 00025 { 00026 getline (file, line); 00027 00028 for (int x = 0; x < static_cast<int>(line.length()); ++x) 00029 { 00030 element = line.at(x); 00031 switch (element) 00032 { 00033 case PLAYER_CHAR: players.push_back(PlayerCar(x,y)); 00034 break; 00035 case ENEMY_CHAR: enemies.push_back (EnemyCar(x,y)); 00036 break; 00037 case CHECKPOINT_CHAR: checkpoints.push_back(Checkpoint(x,y)); 00038 break; 00039 case ROCK_CHAR: rocks.push_back(Rock(x,y)); 00040 break; 00041 case WALL_CHAR: walls.push_back (make_pair(x,y)); 00042 break; 00043 } 00044 if (maxX < x) maxX = x; 00045 if (maxY < y) maxY = y; 00046 } 00047 00048 ++y; 00049 } 00050 00051 maze.generateMaze (walls, maxX, maxY); 00052 00053 file.close(); 00054 }