From 56c84b09d563f99ce2f7342cfc14703a6829c4a7 Mon Sep 17 00:00:00 2001 From: EternalRat <44569175+EternalRat@users.noreply.github.com> Date: Mon, 14 Jun 2021 19:20:10 +0200 Subject: [PATCH] load working --- sources/Map/Map.cpp | 3 +-- sources/Parser/ParserYaml.cpp | 44 +++++++++++++++--------------- sources/Parser/ParserYaml.hpp | 17 +++++------- sources/Runner/GameScene.cpp | 51 ++++++++++++++++++----------------- 4 files changed, 58 insertions(+), 57 deletions(-) diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index f5a44c00..09a1f9f6 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -211,7 +211,6 @@ namespace BBM {UPPERFLOOR, &createUpperFloor}, }; - std::cout << "blockType: " << blockType << std::endl; if (blockType == NOTHING || blockType == SPAWNER) return; auto element = elements.at(blockType); @@ -475,7 +474,7 @@ namespace BBM void MapGenerator::loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene) { - //generateHeightCollision(map, width, height, scene); + generateHeightCollision(map, width, height, scene); generateWall(width, height, scene); generateFloor(map, width, height, scene); for (int x = 0; x < width + 1; x++) diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index 779b8e60..f2cb0c9e 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -29,6 +29,7 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { + const std::string ParserYAML::fileName = "save"; std::string ParserYAML::_block = ""; std::string ParserYAML::_bonus = "bonuses:"; std::string ParserYAML::_player = "players:"; @@ -98,11 +99,11 @@ namespace BBM { _block.append("position: [" + std::to_string(position->getX()) + " " + std::to_string(position->getY()) + " " + std::to_string(position->getZ()) + "]"); } - void ParserYAML::save(std::shared_ptr scene, std::string filename) + void ParserYAML::save(std::shared_ptr scene) { - std::string block = std::string("save/" + filename + "_block.yml"); - std::string player = std::string("save/" + filename + "_player.yml"); - std::string bonus = std::string("save/" + filename + "_bonus.yml"); + std::string block = std::string("save/" + fileName + "_block.yml"); + std::string player = std::string("save/" + fileName + "_player.yml"); + std::string bonus = std::string("save/" + fileName + "_bonus.yml"); std::map> savingGame = { {"Bonus", &_saveBonus}, {"Block", &_saveBlock}, @@ -140,8 +141,8 @@ namespace BBM { {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, //{SoundComponent::DEATH, "assets/sounds/death.ogg"} }; - int maxBomb = 0; - float explosionRadius = 0; + int maxBomb = 3; + float explosionRadius = 3; Vector3f pos; for (; index != lines.size(); index++) { @@ -178,14 +179,14 @@ namespace BBM { }); } - void ParserYAML::_loadPlayers(std::shared_ptr scene, std::string filename) + void ParserYAML::_loadPlayers(std::shared_ptr scene) { - std::ifstream file("save/" + filename + "_player.yml"); + std::ifstream file("save/" + fileName + "_player.yml"); std::string line; std::vector lines; if (!file.good()) - return; + throw (ParserError("File error")); while (std::getline(file, line)) { if (line.empty() || !line.compare("players:")) continue; @@ -201,7 +202,7 @@ namespace BBM { { std::string tmpName = ""; Vector3f pos; - MapGenerator::BlockType blockType; + MapGenerator::BlockType blockType = MapGenerator::NOTHING; for (; index != lines.size(); index++) { if (lines[index].find("position") != std::string::npos) { @@ -215,20 +216,22 @@ namespace BBM { tmpName = lines[index]; } } + if (blockType == MapGenerator::NOTHING) + throw (ParserError("Invalid block_type field.")); if (blockType == MapGenerator::HOLE) pos.y += 1.0f; map[std::make_tuple(pos.x, pos.y, pos.z)] = blockType; } - void ParserYAML::_loadBlocks(std::shared_ptr scene, std::string filename) + void ParserYAML::_loadBlocks(std::shared_ptr scene) { - std::ifstream file("save/" + filename + "_block.yml"); + std::ifstream file("save/" + fileName + "_block.yml"); std::string line; std::vector lines; MapGenerator::MapBlock map; if (!file.good()) - return; + throw (ParserError("File error")); for (int i = 0; i < Runner::width; i++) for (int j = 0; j < Runner::height; j++) map[std::make_tuple(i, 0, j)] = MapGenerator::NOTHING; @@ -249,7 +252,6 @@ namespace BBM { _loadBlock(scene, lines, index, map); index--; } - std::cout << "Test LoadMap" << std::endl; MapGenerator::loadMap(Runner::width, Runner::height, map, scene); } @@ -295,14 +297,14 @@ namespace BBM { std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); } - void ParserYAML::_loadBonuses(std::shared_ptr scene, std::string filename) + void ParserYAML::_loadBonuses(std::shared_ptr scene) { - std::ifstream file("save/" + filename + "_bonus.yml"); + std::ifstream file("save/" + fileName + "_bonus.yml"); std::string line; std::vector lines; if (!file.good()) - return; + throw (ParserError("File error")); while (std::getline(file, line)) { if (line.empty() || !line.compare("bonuses:")) continue; @@ -314,11 +316,11 @@ namespace BBM { } } - void ParserYAML::load(std::shared_ptr scene, std::string filename) + void ParserYAML::load(std::shared_ptr scene) { - _loadBlocks(scene, filename); - _loadBonuses(scene, filename); - _loadPlayers(scene, filename); + _loadBlocks(scene); + _loadBonuses(scene); + _loadPlayers(scene); } WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity) diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 7a57a995..06cb0c36 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -73,26 +73,23 @@ namespace BBM { static void _loadBonus(std::shared_ptr scene, std::vector lines, int &index); //!@param scene Scene to update - //!@param filename filename of the file to read //!@brief load all players into scene - static void _loadPlayers(std::shared_ptr scene, std::string filename); + static void _loadPlayers(std::shared_ptr scene); //!@param scene Scene to update - //!@param filename filename of the file to read //!@brief load all blocks into scene - static void _loadBlocks(std::shared_ptr scene, std::string filename); + static void _loadBlocks(std::shared_ptr scene); //!@param scene Scene to update - //!@param filename filename of the file to read //!@brief load all blocks into scene - static void _loadBonuses(std::shared_ptr scene, std::string filename); + static void _loadBonuses(std::shared_ptr scene); public: //!@param scene Scene to update - //!@param filename filename of the file to read //!@brief save yaml - static void save(std::shared_ptr scene, std::string filename); + static void save(std::shared_ptr scene); //!@param scene Scene to update - //!@param filename filename of the file to read //!@brief load yaml - static void load(std::shared_ptr scene, std::string filename); + static void load(std::shared_ptr scene); + + static const std::string fileName; }; } \ No newline at end of file diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index ebf8abd3..1f854835 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -42,33 +42,36 @@ namespace BBM {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, //{SoundComponent::DEATH, "assets/sounds/death.ogg"} }; - /*scene->addEntity("Player") - .addComponent(0, 1.01, 0) - .addComponent("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) - .addComponent() - .addComponent() - .addComponent() - .addComponent() - .addComponent() - .addComponent("assets/shaders/glsl330/predator.fs") - .addComponent>() - //.addComponent(0) - .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) - .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) - .addComponent() - .addComponent(soundPath) - .addComponent("assets/musics/music_battle.ogg") - .addComponent() - .addComponent() - .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { - auto &animation = entity.getComponent(); - animation.setAnimIndex(5); - });*/ scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); - //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); - ParserYAML::load(scene, "test"); + try { + ParserYAML::load(scene); + } catch (std::exception const &err) { + scene->addEntity("Player") + .addComponent(0, 1.01, 0) + .addComponent("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) + .addComponent() + .addComponent() + .addComponent() + .addComponent() + .addComponent() + .addComponent("assets/shaders/glsl330/predator.fs") + .addComponent>() + //.addComponent(0) + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) + .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) + .addComponent() + .addComponent(soundPath) + .addComponent("assets/musics/music_battle.ogg") + .addComponent() + .addComponent() + .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); + MapGenerator::loadMap(Runner::width, Runner::height, MapGenerator::createMap(Runner::width, Runner::height), scene); + } return scene; }