1 #include <Game/BoardParser.hpp> 2 #include <Config/Globals.hpp> 3 #include <Config/INI.hpp> 4 #include <Misc/Utils.hpp> 30 std::ifstream file(filename.c_str());
32 if (!(file.is_open()))
45 std::string metadata_buffer;
46 std::string level_buffer;
50 std::string current_line =
"";
52 while (std::getline(file, current_line))
56 current_line = Utils::String::trim(current_line);
60 if (current_line !=
"start")
61 metadata_buffer += (current_line +
'\n');
66 bool parsed_level =
false;
68 while (std::getline(file, current_line))
72 current_line = Utils::String::trim(current_line);
74 if (current_line ==
"end")
80 level_buffer += (current_line +
'\n');
88 "Abrupt ending of file while parsing level at line " +
89 Utils::String::toString(line_count)
99 int player_start_x = 1;
100 int player_start_y = 1;
105 std::vector<std::vector<bool> > rawBoard;
108 std::vector<std::string> level_lines = Utils::String::split(level_buffer,
'\n');
110 for (
size_t j = 0; j < (level_lines.size()); j++)
112 current_line = level_lines[j];
114 if (current_line.empty())
117 std::vector<bool> rawBoardLine;
123 for (
size_t i = 0; i < current_line.size(); i++)
125 if (current_line[i] == SNAKE_CHAR)
128 player_start_y = rawBoard.size();
131 rawBoardLine.push_back(
false);
134 rawBoardLine.push_back(current_line[i] == WALL_CHAR);
138 rawBoard.push_back(rawBoardLine);
143 int board_width = rawBoard[0].size();
144 int board_height = rawBoard.size();
148 ((Globals::Game::teleport) ?
154 board->setStartX(player_start_x);
155 board->setStartY(player_start_y);
159 std::stringstream stream;
160 stream << metadata_buffer;
179 for (std::vector<std::string>::iterator it = levels.begin();
190 it = levels.erase(it);
Loads, reads and parses the contents of an INI file (or string).
std::string extension(std::string path)
Returns the extension of a file.
static std::vector< std::string > listLevels()
Lists all levels found by the game.
static Board * loadFile(std::string filename)
Loads and parses the level at filename.
static std::string directory
Default directory where the level files are.
A level where the snake runs and eats fruits.
void setMetadata(std::string name, std::string value)
Sets a meta information from this level.
static Board * load(std::string filename)
Loads and parses level with name.
void setBoard(std::vector< std::vector< bool > > &newBoard)
Sets the whole level content.
static std::string extension
Default extension for nSnake level files.
Custom exception class to specify an error that occurred during a level loading.
std::vector< std::string > ls(std::string path)
Lists all files withing #path.
std::string basename(std::string path)
Returns the component of a pathname (file name and extension).
std::string dropExtension(std::string path)
Returns the filename without it's extension.