From 958a9328fedbe83bef7020f5d5d55fea90a3563f Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Fri, 11 Jun 2021 11:58:15 +0200 Subject: [PATCH] start of the load --- CMakeLists.txt | 1 + .../BombHolder/BombHolderComponent.cpp | 5 +- .../BombHolder/BombHolderComponent.hpp | 2 +- sources/Map/Map.hpp | 3 +- sources/Parser/ParserYaml.cpp | 118 +++++++++++++++--- sources/Parser/ParserYaml.hpp | 19 ++- tests/ParserTest.cpp | 37 ++++++ 7 files changed, 156 insertions(+), 29 deletions(-) create mode 100644 tests/ParserTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ac5c9ea..ea86c23d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,6 +132,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/MoveTests.cpp tests/ViewTest.cpp tests/CollisionTest.cpp + tests/ParserTest.cpp ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/sources/Component/BombHolder/BombHolderComponent.cpp b/sources/Component/BombHolder/BombHolderComponent.cpp index 9f75b8ae..05084bb5 100644 --- a/sources/Component/BombHolder/BombHolderComponent.cpp +++ b/sources/Component/BombHolder/BombHolderComponent.cpp @@ -12,9 +12,10 @@ namespace BBM : WAL::Component(entity) {} - BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount) + BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius) : WAL::Component(entity), - maxBombCount(maxBombCount) + maxBombCount(maxBombCount), + explosionRadius(explosionRadius) {} WAL::Component *BombHolderComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/BombHolder/BombHolderComponent.hpp b/sources/Component/BombHolder/BombHolderComponent.hpp index 43242338..b93da94c 100644 --- a/sources/Component/BombHolder/BombHolderComponent.hpp +++ b/sources/Component/BombHolder/BombHolderComponent.hpp @@ -37,7 +37,7 @@ namespace BBM explicit BombHolderComponent(WAL::Entity &entity); //! @brief Constructor - BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount); + BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount, float explosionRadius = 3); //! @brief A component can't be instantiated, it should be derived. BombHolderComponent(const BombHolderComponent &) = default; diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index a322958b..f2c75f28 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -41,7 +41,8 @@ namespace BBM FLOOR, BUMPER, SPAWNER, - UNBREAKABLE + UNBREAKABLE, + INVISIBLE }; using MapBlock = std::map, BlockType>; diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index 9b9edaf8..f4360c88 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -7,15 +7,28 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "ParserYaml.hpp" +namespace RAY3D = RAY::Drawables::Drawables3D; + namespace BBM { - std::string ParserYAML::_block = "blocks:\n\t"; - std::string ParserYAML::_bonus = "bonuses:\n\t"; - std::string ParserYAML::_player = "players:\n\t"; + std::string ParserYAML::_block = "\nblocks:"; + std::string ParserYAML::_bonus = "\nbonuses:"; + std::string ParserYAML::_player = "players:"; - char *_getBlockType(std::string blockName) + const char *ParserYAML::_getBlockType(std::string blockName) { static std::map map { {"Upper Floor", MapGenerator::BlockType::UPPERFLOOR}, @@ -30,45 +43,62 @@ namespace BBM { {"Hole Block", MapGenerator::BlockType::HOLE} }; - return (std::string(map.at(blockName))); + return (std::to_string(map.at(blockName)).c_str()); } - void ParserYAML::_saveBonus(WAL::Entity entity) + void ParserYAML::_saveBonus(const WAL::Entity &entity) { auto *position = entity.tryGetComponent(); + auto name = entity.getName(); if (!position) return; - _bonus.append(entity.getName() + ":\n\t\t"); + std::replace(name.begin(), name.end(), ' ', '_'); + _bonus.append("\n\t" + name + ":\n\t\t"); _bonus.append("position:\n\t\t\t"); _bonus.append("x: " + std::to_string(position->getX()) + "\n\t\t\t"); _bonus.append("y: " + std::to_string(position->getY()) + "\n\t\t\t"); - _bonus.append("z: " + std::to_string(position->getZ()) + "\n"); + _bonus.append("z: " + std::to_string(position->getZ())); } - void ParserYAML::_savePlayer(WAL::Entity entity) - { - - } - - void ParserYAML::_saveBlock(WAL::Entity entity) + void ParserYAML::_savePlayer(const WAL::Entity &entity) { auto *position = entity.tryGetComponent(); + auto *bombHolder = entity.tryGetComponent(); + auto name = entity.getName(); + + if (!position || !bombHolder) + return; + std::replace(name.begin(), name.end(), ' ', '_'); + _player.append("\n\t" + name + ":\n\t\t"); + _player.append("max_bomb: " + std::to_string(bombHolder->maxBombCount) + "\n\t\t"); + _player.append("explosion_radius: " + std::to_string(bombHolder->explosionRadius) + "\n\t\t"); + _player.append("position:\n\t\t\t"); + _player.append("x: " + std::to_string(position->getX()) + "\n\t\t\t"); + _player.append("y: " + std::to_string(position->getY()) + "\n\t\t\t"); + _player.append("z: " + std::to_string(position->getZ())); + } + + void ParserYAML::_saveBlock(const WAL::Entity &entity) + { + auto *position = entity.tryGetComponent(); + auto name = entity.getName(); if (!position) return; - _block.append(entity.getName() + ":\n\t\t"); + std::replace(name.begin(), name.end(), ' ', '_'); + _block.append("\n\t" + name + ":\n\t\t"); _block.append(std::string("block_type: ") + _getBlockType(entity.getName()) + "\n\t\t"); _block.append("position:\n\t\t\t"); _block.append("x: " + std::to_string(position->getX()) + "\n\t\t\t"); _block.append("y: " + std::to_string(position->getY()) + "\n\t\t\t"); - _block.append("z: " + std::to_string(position->getZ()) + "\n"); + _block.append("z: " + std::to_string(position->getZ())); } - void ParserYAML::save(std::shared_ptr scene, std::string filename) + void ParserYAML::save(std::shared_ptr scene, std::string &filename) { std::string path = std::string("save/" + filename); - std::map> savingGame = { + std::map> savingGame = { {"Bonus", &_saveBonus}, {"Block", &_saveBlock}, {"Floor", &_saveBlock}, @@ -76,12 +106,60 @@ namespace BBM { {"Player", &_savePlayer} }; std::ofstream file(path.c_str()); - for (auto entity : scene->getEntities()) { - for (auto type : savingGame) { + for (const auto &entity : scene->getEntities()) { + for (const auto& type : savingGame) { if (entity.getName().find(type.first) != std::string::npos) { type.second(entity); } } } + file << _player << _block << _bonus << std::endl; + } + + void ParserYAML::loadPlayers(std::shared_ptr scene, std::string filename) + { + + } + + void ParserYAML::loadBlocks(std::shared_ptr scene, std::string filename) + { + + } + + void ParserYAML::loadBonuses(std::shared_ptr scene, std::string filename) + { + + } + + void ParserYAML::load(std::shared_ptr scene, std::string &filename) + { + std::map> parser = { + {"players", {}}, + {"blocks", {}}, + {"bonuses", {}} + }; + std::ifstream file("save/" + filename); + if (file.good()) { + std::string line; + while (std::getline(file, line)) { + + } + } + } + + Vector3f ParserYAML::getPosition(std::string &filename) + { + + return Vector3f(); + } + + unsigned int ParserYAML::getMaxBomb(std::string &filename) + { + return 0; + } + + float ParserYAML::getExplosionRadius(std::string &filename) + { + return 0; } } \ No newline at end of file diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 4c5f7642..b49db14a 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -13,11 +13,20 @@ namespace BBM { static std::string _bonus; static std::string _player; - static void _saveBlock(WAL::Entity entity); - static void _saveBonus(WAL::Entity entity); - static void _savePlayer(WAL::Entity entity); - static char *_getBlockType(std::string blockName); + static void _saveBlock(const WAL::Entity &entity); + static void _saveBonus(const WAL::Entity &entity); + static void _savePlayer(const WAL::Entity &entity); + static const char *_getBlockType(std::string blockName); + static Vector3f getPosition(std::string &filename); + static unsigned int getMaxBomb(std::string &filename); + static float getExplosionRadius(std::string &filename); + + static void loadPlayers(std::shared_ptr scene, std::string filename); + static void loadBlocks(std::shared_ptr scene, std::string filename); + static void loadBonuses(std::shared_ptr scene, std::string filename); public: - static void save(std::shared_ptr scene, std::string filename); + static void save(std::shared_ptr scene, std::string &filename); + static void load(std::shared_ptr scene, std::string &filename); + }; } \ No newline at end of file diff --git a/tests/ParserTest.cpp b/tests/ParserTest.cpp new file mode 100644 index 00000000..c9116476 --- /dev/null +++ b/tests/ParserTest.cpp @@ -0,0 +1,37 @@ +// +// Created by hbenjamin on 11/06/2021. +// + +#include "Entity/Entity.hpp" +#include "Component/Position/PositionComponent.hpp" +#include +#include + +#define private public +#include "System/Controllable/ControllableSystem.hpp" +#include "System/Movable/MovableSystem.hpp" +#include +#include +#include "Component/BombHolder/BombHolderComponent.hpp" +#include "Parser/ParserYaml.hpp" + +using namespace WAL; +using namespace BBM; + +TEST_CASE("Parser Test", "[PARSER]") +{ + Wal wal; + wal.changeScene(std::make_shared()); + wal.getScene()->addEntity("Player") + .addComponent() + .addComponent() + .addComponent() + .addComponent(); + wal.getScene()->addEntity("Bonus Fire Up") + .addComponent(2, 0, 2); + wal.getScene()->addEntity("Breakable Block") + .addComponent(1, 0, 1); + wal.getScene()->addEntity("Unbreakable Block") + .addComponent(1, 5, 1); + ParserYAML::save(wal.getScene(), "test_filename.yml"); +} \ No newline at end of file