1 #include <Entities/BoardParser.hpp> 2 #include <Config/Globals.hpp> 3 #include <Engine/Helpers/INI.hpp> 4 #include <Engine/Helpers/File.hpp> 5 #include <Engine/Helpers/String.hpp> 31 std::ifstream file(filename.c_str());
33 if (!(file.is_open()))
46 std::string metadata_buffer;
47 std::string level_buffer;
51 std::string current_line =
"";
53 while (std::getline(file, current_line))
57 current_line = Utils::String::trim(current_line);
61 if (current_line !=
"start")
62 metadata_buffer += (current_line +
'\n');
67 bool parsed_level =
false;
69 while (std::getline(file, current_line))
73 current_line = Utils::String::trim(current_line);
75 if (current_line ==
"end")
81 level_buffer += (current_line +
'\n');
89 "Abrupt ending of file while parsing level at line " +
90 Utils::String::toString(line_count)
100 int player_start_x = 1;
101 int player_start_y = 1;
106 std::vector<std::vector<bool> > rawBoard;
109 std::vector<std::string> level_lines = Utils::String::split(level_buffer,
'\n');
111 for (
size_t i = 0; i < level_lines.size(); ++i)
112 level_lines[i] = Utils::String::trim(level_lines[i]);
114 for (
size_t j = 0; j < (level_lines.size()); j++)
116 current_line = level_lines[j];
118 if (current_line.empty())
121 std::vector<bool> rawBoardLine;
127 for (
size_t i = 0; i < current_line.size(); i++)
129 if (current_line[i] == SNAKE_CHAR)
132 player_start_y = rawBoard.size();
135 rawBoardLine.push_back(
false);
138 rawBoardLine.push_back(current_line[i] == WALL_CHAR);
142 rawBoard.push_back(rawBoardLine);
147 int board_width = rawBoard[0].size();
148 int board_height = rawBoard.size();
152 ((Globals::Game::teleport) ?
158 board->setStartX(player_start_x);
159 board->setStartY(player_start_y);
163 std::stringstream stream;
164 stream << metadata_buffer;
165 INI::Parser parser(stream);
183 for (std::vector<std::string>::iterator it = levels.begin();
188 (*it) = (Utils::File::dropExtension(Utils::File::basename((*it))));
194 it = levels.erase(it);
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.