From c71104389d667a1cea401d581e86720fc8d06f56 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Thu, 17 Jun 2021 17:10:31 +0200 Subject: [PATCH] updated everything with node --- .../BombHolder/BombHolderComponent.cpp | 2 +- .../BombHolder/BombHolderComponent.hpp | 4 +- sources/Parser/Node.cpp | 10 + sources/Parser/Node.hpp | 1 + sources/Parser/ParserYaml.cpp | 207 ++++++------------ sources/Parser/ParserYaml.hpp | 29 +-- 6 files changed, 90 insertions(+), 163 deletions(-) diff --git a/sources/Component/BombHolder/BombHolderComponent.cpp b/sources/Component/BombHolder/BombHolderComponent.cpp index 05084bb5..3e803ef4 100644 --- a/sources/Component/BombHolder/BombHolderComponent.cpp +++ b/sources/Component/BombHolder/BombHolderComponent.cpp @@ -12,7 +12,7 @@ namespace BBM : WAL::Component(entity) {} - BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius) + BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, int explosionRadius) : WAL::Component(entity), maxBombCount(maxBombCount), explosionRadius(explosionRadius) diff --git a/sources/Component/BombHolder/BombHolderComponent.hpp b/sources/Component/BombHolder/BombHolderComponent.hpp index b93da94c..b0457232 100644 --- a/sources/Component/BombHolder/BombHolderComponent.hpp +++ b/sources/Component/BombHolder/BombHolderComponent.hpp @@ -26,7 +26,7 @@ namespace BBM //! @brief The number of nanosecond before the next bomb refill. std::chrono::nanoseconds nextBombRefill = refillRate; //! @brief The radius of the explosion. - float explosionRadius = 3; + int explosionRadius = 3; //! @brief The damage made by the explosion on an entity int damage = 1; @@ -37,7 +37,7 @@ namespace BBM explicit BombHolderComponent(WAL::Entity &entity); //! @brief Constructor - BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius = 3); + BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, int explosionRadius = 3); //! @brief A component can't be instantiated, it should be derived. BombHolderComponent(const BombHolderComponent &) = default; diff --git a/sources/Parser/Node.cpp b/sources/Parser/Node.cpp index cdc4095b..3ad73b1c 100644 --- a/sources/Parser/Node.cpp +++ b/sources/Parser/Node.cpp @@ -44,6 +44,16 @@ namespace BBM return childs; } + std::vector Node::getChildNodes(void) + { + std::vector childs; + + for (const auto &child : this->_childNodes) { + childs.emplace_back(child); + } + return childs; + } + void Node::setName(const std::string &name) { this->_name = name; diff --git a/sources/Parser/Node.hpp b/sources/Parser/Node.hpp index 8b15d002..13056c12 100644 --- a/sources/Parser/Node.hpp +++ b/sources/Parser/Node.hpp @@ -31,6 +31,7 @@ namespace BBM void addChildNode(const Node &childNode); std::vector getChildNodes(const std::string &childNodeName); + std::vector getChildNodes(void); void setProperty(const std::string &propertyName, const std::string &propertyValue); void setProperty(const std::pair &propertyNameValue); diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index c24f517c..3aa9f508 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -151,10 +151,9 @@ namespace BBM { _bonus = std::stringstream(); } - void ParserYAML::_loadPlayer(std::shared_ptr scene, std::vector lines, int &index, int countPlayer) + void ParserYAML::_loadPlayer(std::shared_ptr scene, Node &node, int countPlayer) { - std::string name; - std::string tmpAssets; + std::string tmpAssets = node.getProperty("texture_path"); std::map map = { {"red", RED}, {"blue", BLUE}, @@ -162,29 +161,13 @@ namespace BBM { {"green", GREEN} }; - for (; index != lines.size(); index++) { - if (lines[index].find("max_bomb") != std::string::npos && !name.empty()) { - playerBombCount.push_back(_parseMaxBomb(lines[index])); - } else if (lines[index].find("explosion_radius") != std::string::npos && !name.empty()) { - playerExplosionRange.push_back(_parseExplosionRadius(lines[index])); - } else if (lines[index].find("position") != std::string::npos && !name.empty()) { - playerPosition.push_back(_parsePosition(lines[index])); - } else if (lines[index].find("texture_path") != std::string::npos && !name.empty()) { - playerAssets.push_back(_parseAssets(lines[index])); - tmpAssets = _parseAssets(lines[index]); - } else if (lines[index].find("speed") != std::string::npos && !name.empty()) { - playerSpeed.push_back(_parseSpeed(lines[index])); - } else { - if (!name.empty()) { - break; - } - name = lines[index]; - name.erase(std::remove(name.begin(), name.end(), ' '), name.end()); - name = name.substr(0, name.find(':')); - std::replace(name.begin(), name.end(), '_', ' '); - playerName.push_back(name); - } - } + playerName.push_back(node.getName()); + playerAssets.push_back(node.getProperty("texture_path")); + playerBombCount.push_back(_parseMaxBomb(node.getProperty("max_bomb"))); + playerExplosionRange.push_back(_parseExplosionRadius(node.getProperty("explosion_radius"))); + playerSpeed.push_back(_parseSpeed(node.getProperty("speed"))); + playerPosition.push_back(_parsePosition(node.getProperty("position"))); + if ((tmpAssets.find("red.png") == std::string::npos && tmpAssets.find("blue.png") == std::string::npos && tmpAssets.find("green.png") == std::string::npos && tmpAssets.find("yellow.png") == std::string::npos && tmpAssets.find("ai.png") == std::string::npos) || !std::filesystem::exists(tmpAssets)) { @@ -205,45 +188,22 @@ namespace BBM { playerLogo.addComponent(countPlayer, ready, playerTile); } - void ParserYAML::_loadPlayers(std::shared_ptr scene) + void ParserYAML::_loadPlayers(std::shared_ptr scene, Node &node) { - std::ifstream file("save/" + fileName + "_player.yml"); - std::string line; - std::vector lines; int countPlayer = 0; + auto childNode = node.getChildNodes("players")[0].getChildNodes(); - if (!file.good()) - throw (ParserError("File error")); - while (std::getline(file, line)) { - if (line.empty() || line.find("players:") != std::string::npos) - continue; - lines.push_back(line); - } - for (int index = 0; lines.size() != index; index++) { - _loadPlayer(scene, lines, index, countPlayer); - index--; + for (auto child : childNode) { + _loadPlayer(scene, child, countPlayer); countPlayer++; } } - void ParserYAML::_loadBlock(std::shared_ptr scene, std::vector lines, int &index, MapGenerator::MapBlock &map) + void ParserYAML::_loadBlock(std::shared_ptr scene, Node child, MapGenerator::MapBlock &map) { - std::string tmpName = ""; - Vector3f pos; - MapGenerator::BlockType blockType = MapGenerator::NOTHING; + Vector3f pos = _parsePosition(child.getProperty("position")); + MapGenerator::BlockType blockType = _parseBlockType(child.getProperty("block_type")); - for (; index != lines.size(); index++) { - if (lines[index].find("position") != std::string::npos && !tmpName.empty()) { - pos = _parsePosition(lines[index]); - } else if (lines[index].find("block_type") != std::string::npos && !tmpName.empty()) { - blockType = _parseBlockType(lines[index]); - } else { - if (!tmpName.empty()) { - break; - } - tmpName = lines[index]; - } - } if (blockType == MapGenerator::NOTHING) throw (ParserError("Invalid block_type field.")); if (blockType == MapGenerator::HOLE) @@ -268,31 +228,18 @@ namespace BBM { for (int i = 0; i < Runner::mapWidth; i++) for (int j = 0; j < Runner::mapHeight; j++) map[std::make_tuple(i, 0, j)] = MapGenerator::NOTHING; - /*for (int index = 0; lines.size() != index; index++) { - _loadBlock(scene, lines, index, map); - index--; - }*/ + auto childNode = node.getChildNodes("blocks")[0].getChildNodes(); + for (auto child : childNode) + _loadBlock(scene, child, map); MapGenerator::loadMap(Runner::mapWidth, Runner::mapHeight, map, scene); } - void ParserYAML::_loadBonus(std::shared_ptr scene, std::vector lines, int &index) + void ParserYAML::_loadBonus(std::shared_ptr scene, Node &node) { - auto &entity = scene->addEntity(""); - Vector3f pos; - Bonus::BonusType bonusType; + auto &entity = scene->addEntity(node.getName()); + Vector3f pos = _parsePosition(node.getProperty("position")); + Bonus::BonusType bonusType = _parseBonusType(node.getProperty("bonus_type")); - for (; index != lines.size(); index++) { - if (lines[index].find("position") != std::string::npos && !entity.getName().empty()) { - pos = _parsePosition(lines[index]); - } else if (lines[index].find("bonus_type") != std::string::npos && !entity.getName().empty()) { - bonusType = _parseBonusType(lines[index]); - } else { - if (!entity.getName().empty()) { - break; - } - _parseEntityName(lines[index], entity); - } - } if (bonusType == Bonus::NOTHING) { entity.scheduleDeletion(); return; @@ -300,23 +247,11 @@ namespace BBM { MapGenerator::createBonus(entity, pos, bonusType); } - void ParserYAML::_loadBonuses(std::shared_ptr scene) + void ParserYAML::_loadBonuses(std::shared_ptr scene, Node &node) { - std::ifstream file("save/" + fileName + "_bonus.yml"); - std::string line; - std::vector lines; - - if (!file.good()) - throw (ParserError("File error")); - while (std::getline(file, line)) { - if (line.empty() || line.find("bonuses:") != std::string::npos) - continue; - lines.push_back(line); - } - for (int index = 0; lines.size() != index; index++) { - _loadBonus(scene, lines, index); - index--; - } + auto childNode = node.getChildNodes("bonuses")[0].getChildNodes(); + for (auto child : childNode) + _loadBonus(scene, child); } void ParserYAML::load(std::shared_ptr gameScene) @@ -325,94 +260,82 @@ namespace BBM { Node bonusesInfos = parseFile("save/save_bonus.yml"); Node playerInfos = parseFile("save/save_player.yml"); _loadBlocks(gameScene, blocksInfos); - _loadBonuses(gameScene); - _loadPlayers(gameScene); + _loadBonuses(gameScene, bonusesInfos); + _loadPlayers(gameScene, playerInfos); } - WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity) + Vector3f ParserYAML::_parsePosition(std::string line) { - line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); - auto name = line.substr(0, line.find(':')); - std::replace(name.begin(), name.end(), '_', ' '); - entity.setName(name); - return (entity); - } - - std::string ParserYAML::_parseAssets(std::string &line) - { - if (line.find(":", 0) == std::string::npos) - throw (ParserError("Error with saved map: Error parsing assets.\n Loading default maps...")); - auto start = line.find(":", 0) + 2; - return (line.substr(start, line.length() - start)); - } - - Vector3f ParserYAML::_parsePosition(std::string &line) - { - std::string x; - std::string y; - std::string z; + float x; + float y; + float z; std::string subStr; try { auto start = line.find("[", 0) + 1; subStr = line.substr(start, line.find("]", 0) - 1 - start); - auto pos = Utils::splitStr(subStr, ' '); + auto pos = Utils::splitStr(subStr, ','); if (pos.size() != 3) throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); - x = pos[0]; - y = pos[1]; - z = pos[2]; + if (!Utils::tryParseFloat(pos[0], x) || !Utils::tryParseFloat(pos[1], y) || !Utils::tryParseFloat(pos[2], z)) + throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); } catch (const std::out_of_range &err) { throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); } - //if (!_isFloat(x) || !_isFloat(y) || !_isFloat(z)) - // throw (ParserError("Error with saved map: Error parsing position.\n Loading default maps...")); - return Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str())); + return Vector3f(x, y, z); } - int ParserYAML::_parseMaxBomb(std::string &line) + int ParserYAML::_parseMaxBomb(std::string line) { - //if (line.find(": ") == std::string::npos || !_isInteger(line.substr(line.find(": ") + 2))) - // throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); + int maxBomb = 0; + if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); - return (std::atoi(line.substr(line.find(": ") + 2).c_str())); + if (!Utils::tryParseInteger(line, maxBomb)) + throw (ParserError("Error with saved map: Couldn't parse max bomb.\n Loading default maps...")); + return (maxBomb); } - float ParserYAML::_parseExplosionRadius(std::string &line) + int ParserYAML::_parseExplosionRadius(std::string line) { - //if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) - // throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); + int explosionRadius = 0; + if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); - return (std::atof(line.substr(line.find(": ") + 2).c_str())); + if (!Utils::tryParseInteger(line, explosionRadius)) + throw (ParserError("Error with saved map: Couldn't parse explosion radius.\n Loading default maps...")); + return (explosionRadius); } - float ParserYAML::_parseSpeed(std::string &line) + float ParserYAML::_parseSpeed(std::string line) { - //if (line.find(": ") == std::string::npos || !_isFloat(line.substr(line.find(": ") + 2))) - // throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); + float speed = 0; + if (line.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); - return (std::atof(line.substr(line.find(": ") + 2).c_str())); + if (!Utils::tryParseFloat(line, speed)) + throw (ParserError("Error with saved map: Couldn't parse speed.\n Loading default maps...")); + return (speed); } - MapGenerator::BlockType ParserYAML::_parseBlockType(std::string &blockType) + MapGenerator::BlockType ParserYAML::_parseBlockType(std::string blockType) { - //if (blockType.find(": ") == std::string::npos || !_isInteger(blockType.substr(blockType.find(": ") + 2))) - // throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); if (blockType.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); - return (static_cast(std::atoi(blockType.substr(blockType.find(": ") + 2).c_str()))); + int block = 0; + if (!Utils::tryParseInteger(blockType, block)) + throw (ParserError("Error with saved map: Couldn't parse block type.\n Loading default maps...")); + return (static_cast(block)); } - Bonus::BonusType ParserYAML::_parseBonusType(std::string &bonusType) + Bonus::BonusType ParserYAML::_parseBonusType(std::string bonusType) { - //if (bonusType.find(": ") == std::string::npos || !_isInteger(bonusType.substr(bonusType.find(": ") + 2))) - // throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); if (bonusType.find("-") != std::string::npos) throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); - return (static_cast(std::atoi(bonusType.substr(bonusType.find(": ") + 2).c_str()))); + int bonus = 0; + if (!Utils::tryParseInteger(bonusType, bonus)) + throw (ParserError("Error with saved map: Couldn't parse bonus type.\n Loading default maps...")); + return (static_cast(bonus)); } std::string ParserYAML::parseHeader(const std::string &line) diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index b5b01632..3e988312 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -37,58 +37,51 @@ namespace BBM { //!@brief transform bonus name static std::string _getBonusType(std::string bonusName); - //!@param line to parse - //!@param entity to update - //!@brief parse entity name - static WAL::Entity &_parseEntityName(std::string line, WAL::Entity &entity); //!@param line to parse //!@brief return max bomb parsed - static int _parseMaxBomb(std::string &line); + static int _parseMaxBomb(std::string line); //!@param line to parse //!@brief return explosion radius parsed - static float _parseExplosionRadius(std::string &line); + static int _parseExplosionRadius(std::string line); //!@param line to parse //!@brief return speed parsed - static float _parseSpeed(std::string &line); + static float _parseSpeed(std::string line); //!@param line to parse //!@brief return vector3f of position parsed - static Vector3f _parsePosition(std::string &line); + static Vector3f _parsePosition(std::string line); //!@param blockType to parse //!@brief return BlockType of type parsed - static MapGenerator::BlockType _parseBlockType(std::string &blockType); + static MapGenerator::BlockType _parseBlockType(std::string blockType); //!@param bonusType to parse //!@brief return bonusType of type parsed - static Bonus::BonusType _parseBonusType(std::string &bonusType); - //!@param assets to parse - //!@brief return assets parsed - static std::string _parseAssets(std::string &line); + static Bonus::BonusType _parseBonusType(std::string bonusType); //!@param scene Scene to update //!@param lines Lines of the file //!@param index index of the vector //!@brief add player into scene - static void _loadPlayer(std::shared_ptr scene, std::vector lines, int &index, int countPlayer); + static void _loadPlayer(std::shared_ptr scene, Node &node, int countPlayer); //!@param scene Scene to update //!@param lines Lines of the file //!@param index index of the vector //!@param map map of all the block //!@brief add block into scene - static void _loadBlock(std::shared_ptr scene, std::vector lines, int &index, MapGenerator::MapBlock &map); + static void _loadBlock(std::shared_ptr scene, Node child, MapGenerator::MapBlock &map); //!@param scene Scene to update //!@param lines Lines of the file //!@param index index of the vector //!@brief add bonus into scene - static void _loadBonus(std::shared_ptr scene, std::vector lines, int &index); + static void _loadBonus(std::shared_ptr scene, Node &node); //!@param scene Scene to update //!@brief load all players into scene - static void _loadPlayers(std::shared_ptr scene); + static void _loadPlayers(std::shared_ptr scene, Node &node); //!@param scene Scene to update //!@brief load all blocks into scene static void _loadBlocks(std::shared_ptr scene, Node &node); //!@param scene Scene to update //!@brief load all blocks into scene - static void _loadBonuses(std::shared_ptr scene); + static void _loadBonuses(std::shared_ptr scene, Node &node); static std::string parseHeader(const std::string &line);