diff --git a/CMakeLists.txt b/CMakeLists.txt index ea86c23d..198b10b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,10 +109,12 @@ set(SOURCES sources/System/Sound/PlayerSoundManagerSystem.hpp sources/System/Music/MusicSystem.hpp sources/System/Music/MusicSystem.cpp - sources/System/Bomb/BombSystem.cpp - sources/System/Bomb/BombSystem.hpp sources/Parser/ParserYaml.hpp sources/Parser/ParserYaml.cpp + sources/Exception/Error.hpp + sources/Exception/Error.cpp + sources/System/Bomb/BombSystem.cpp + sources/System/Bomb/BombSystem.hpp ) add_executable(bomberman sources/main.cpp diff --git a/sources/Exception/Error.cpp b/sources/Exception/Error.cpp index 56e4e568..ed1d660c 100644 --- a/sources/Exception/Error.cpp +++ b/sources/Exception/Error.cpp @@ -11,6 +11,6 @@ namespace BBM {} ParserError::ParserError(const std::string &what) - : WalError(what) + : Error(what) {} } // namespace BBM \ No newline at end of file diff --git a/sources/Exception/Error.hpp b/sources/Exception/Error.hpp index bddef6e1..15f8a7d1 100644 --- a/sources/Exception/Error.hpp +++ b/sources/Exception/Error.hpp @@ -2,7 +2,7 @@ // Created by hbenjamin on 11/06/2021. // -#pramga once +#pragma once #include #include diff --git a/sources/Items/Bonus.hpp b/sources/Items/Bonus.hpp index 6c15efd4..d15e47e3 100644 --- a/sources/Items/Bonus.hpp +++ b/sources/Items/Bonus.hpp @@ -15,11 +15,6 @@ namespace BBM { //! @brief Apply bonus effect that allows players to carry one more bomb than before static void BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); - //! @param bonus bonus - //! @param player the entity on which the effect will be applied - //! @brief Apply bonus effect who increased the bomb damage - static void DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); - //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect that expend the explosion range of the bomb diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index f2c75f28..ed73cf42 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -64,6 +64,11 @@ namespace BBM //! @param scene Scene where the map is instanced //! @brief Generate the map static void loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene); + + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create element of the map + static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); private: using MapElem = std::function scene)>; @@ -97,11 +102,6 @@ namespace BBM //! @brief Generate the floor of the map static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create element of the map - static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); - //! @param coords coords of the element //! @param scene Scene where the map is instanced //! @brief Create breakable of the map diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index ea03e5ee..8a49a05b 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -22,6 +22,7 @@ #include #include "ParserYaml.hpp" #include +#include namespace RAY3D = RAY::Drawables::Drawables3D; @@ -131,55 +132,117 @@ namespace BBM { _bonus.clear(); } - WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity) + void ParserYAML::_loadPlayer(std::shared_ptr scene, std::vector lines, int &index) { - line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); - auto name = line.substr(0, line.find(':')); - entity.setName(name); - return (entity); + auto &entity = scene->addEntity(""); + static std::map soundPath ={ + {SoundComponent::JUMP, "assets/sounds/jump.wav"}, + {SoundComponent::MOVE, "assets/sounds/move.ogg"}, + {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, + //{SoundComponent::DEATH, "assets/sounds/death.ogg"} + }; + int maxBomb = 0; + float explosionRadius = 0; + Vector3f pos; + + for (; index != lines.size(); index++) { + if (lines[index].find("max_bomb") != std::string::npos) { + maxBomb = _parseMaxBomb(lines[index]); + } else if (lines[index].find("explosion_radius") != std::string::npos) { + explosionRadius = _parseExplosionRadius(lines[index]); + } else if (lines[index].find("position") != std::string::npos) { + pos = _parsePosition(lines[index]); + } else { + if (!entity.getName().empty()) { + break; + } + _parseEntityName(lines[index], entity); + } + } + entity.addComponent(pos) + .addComponent(maxBomb, explosionRadius) + .addComponent("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) + .addComponent() + .addComponent() + .addComponent() + .addComponent("assets/shaders/glsl330/predator.fs") + .addComponent>() + .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(1, [](WAL::Entity &entity, WAL::Wal &wal) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); } void ParserYAML::_loadPlayers(std::shared_ptr scene, std::string filename) { std::ifstream file("save/" + filename + "_player.yml"); std::string line; - WAL::Entity entity; + std::vector lines; - if (file.good()) { - while (std::getline(file, line)) { - if (line.find("max_bomb") != std::string::npos) - _parseMaxBomb(line, entity); - else if (line.find("explosion_radius") != std::string::npos) - _parseExplosionRadius(line, entity); - else if (line.find("position") != std::string::npos) - _parsePosition(line, entity); - else - _parseEntityName(line, entity); + if (!file.good()) + return; + while (std::getline(file, line)) { + line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); + line.erase(std::remove(line.begin(), line.end(), '\t'), line.end()); + if (line.empty() || !line.compare("players:")) + continue; + lines.push_back(line); + } + for (int index = 0; lines.size() != index; index++) { + _loadPlayer(scene, lines, index); + index--; + } + } + + void ParserYAML::_loadBlock(std::shared_ptr scene, std::vector lines, int &index) + { + std::string tmpName = ""; + Vector3f pos; + MapGenerator::BlockType blockType; + + for (; index != lines.size(); index++) { + if (lines[index].find("position") != std::string::npos) { + pos = _parsePosition(lines[index]); + } else if (lines[index].find("block_type") != std::string::npos) { + blockType = _parseBlockType(lines[index]); + } else { + if (!tmpName.empty()) { + break; + } + tmpName = lines[index]; } } + MapGenerator::createElement(pos, scene, blockType); } void ParserYAML::_loadBlocks(std::shared_ptr scene, std::string filename) { std::ifstream file("save/" + filename + "block.yml"); - WAL::Entity entity(*scene, ""); std::string line; + std::vector lines; - if (file.good()) { - while (std::getline(file, line)) { - if (line.find("max_bomb") != std::string::npos) - _parseMaxBomb(line, entity); - else if (line.find("explosion_radius") != std::string::npos) - _parseExplosionRadius(line, entity); - else if (line.find("position") != std::string::npos) - _parsePosition(line, entity); - else - _parseEntityName(line, entity); - } + if (!file.good()) + return; + while (std::getline(file, line)) { + line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); + line.erase(std::remove(line.begin(), line.end(), '\t'), line.end()); + if (line.empty() || !line.compare("blocks:")) + continue; + lines.push_back(line); + } + for (int index = 0; lines.size() != index; index++) { + _loadBlock(scene, lines, index); + index--; } } - void ParserYAML::_loadBonuses(std::shared_ptr scene, std::string filename) + void ParserYAML::_loadBonus(std::shared_ptr scene, std::vector lines, int &index) { static std::map> map = { {Bonus::BonusType::BOMBSTOCK, {"Bonus Bomb Up", "assets/items/bombup"}}, @@ -189,15 +252,57 @@ namespace BBM { static std::vector> func = { &Bonus::BombUpBonus, &Bonus::SpeedUpBonus, &Bonus::ExplosionRangeBonus }; - WAL::Entity entity(*scene, ""); - std::string line; - std::ifstream file("save/" + filename + "_bonus.yml"); - - if (file.good()) { - while (std::getline(file, line)) { + auto &entity = scene->addEntity(""); + std::string tmpName = ""; + Vector3f pos; + Bonus::BonusType bonusType; + for (; index != lines.size(); index++) { + if (lines[index].find("position") != std::string::npos) { + pos = _parsePosition(lines[index]); + } else if (lines[index].find("block_type") != std::string::npos) { + bonusType = _parseBonusType(lines[index]); + } else { + if (!tmpName.empty()) { + break; + } + _parseEntityName(lines[index], entity); } } + entity.addComponent(pos) + .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { + entity.scheduleDeletion(); + }) + .addComponent(pos.y) + .addComponent([](WAL::Entity &bonus, const WAL::Entity &player, CollisionComponent::CollidedAxis axis) { + bonus.scheduleDeletion(); + }, func[bonusType - 1], 0.5, .5) + .addComponent(5s, [](WAL::Entity &bonus, WAL::Wal &wal){ + bonus.scheduleDeletion(); + }) + .addComponent(map.at(bonusType)[1] + ".obj", false, + std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); + } + + void ParserYAML::_loadBonuses(std::shared_ptr scene, std::string filename) + { + std::ifstream file("save/" + filename + "bonus.yml"); + std::string line; + std::vector lines; + + if (!file.good()) + return; + while (std::getline(file, line)) { + line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); + line.erase(std::remove(line.begin(), line.end(), '\t'), line.end()); + if (line.empty() || !line.compare("bonuses:")) + continue; + lines.push_back(line); + } + for (int index = 0; lines.size() != index; index++) { + _loadBonus(scene, lines, index); + index--; + } } void ParserYAML::load(std::shared_ptr scene, std::string filename) @@ -207,7 +312,16 @@ namespace BBM { _loadPlayers(scene, filename); } - WAL::Entity &ParserYAML::_parsePosition(std::string &line, WAL::Entity &entity) { + WAL::Entity &ParserYAML::_parseEntityName(std::string line, WAL::Entity &entity) + { + 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); + } + + Vector3f ParserYAML::_parsePosition(std::string &line) { std::string x; std::string y; std::string z; @@ -221,33 +335,21 @@ namespace BBM { } catch (const std::out_of_range &err) { throw (ParserError("Error parsing position")); } - return entity.addComponent(Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str()))); + return Vector3f(std::atof(x.c_str()), std::atof(y.c_str()), std::atof(z.c_str())); } - WAL::Entity &ParserYAML::_parseMaxBomb(std::string &line, WAL::Entity &entity) + int ParserYAML::_parseMaxBomb(std::string &line) { - auto *bombHolder = entity.tryGetComponent(); - if (line.find(": ") == std::string::npos) throw (ParserError("Couldn't parse max bomb")); - if (bombHolder) { - bombHolder->maxBombCount = std::atoi(line.substr(line.find(": ")).c_str()); - return (entity); - } - return (entity.addComponent(std::atoi(line.substr(line.find(": ")).c_str()))); + return (std::atoi(line.substr(line.find(": ")).c_str())); } - WAL::Entity &ParserYAML::_parseExplosionRadius(std::string &line, WAL::Entity &entity) + float ParserYAML::_parseExplosionRadius(std::string &line) { - auto *bombHolder = entity.tryGetComponent(); - if (line.find(": ") == std::string::npos) throw (ParserError("Couldn't parse explosion radius")); - if (bombHolder) { - bombHolder->explosionRadius = std::atoi(line.substr(line.find(": ")).c_str()); - return (entity); - } - return (entity.addComponent(3, bombHolder->explosionRadius = std::atoi(line.substr(line.find(": ")).c_str()))); + return (std::atof(line.substr(line.find(": ")).c_str())); } MapGenerator::BlockType ParserYAML::_parseBlockType(std::string blockType) diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 41db3f7a..fb244da0 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include "Items/Bonus.hpp" namespace BBM { class ParserYAML { @@ -20,12 +21,16 @@ namespace BBM { static std::string _getBonusType(std::string bonusName); static WAL::Entity &_parseEntityName(std::string line, WAL::Entity &entity); - static WAL::Entity &_parseMaxBomb(std::string &filename, WAL::Entity &entity); - static WAL::Entity &_parseExplosionRadius(std::string &filename, WAL::Entity &entity); - static WAL::Entity &_parsePosition(std::string &filename, WAL::Entity &entity); + static int _parseMaxBomb(std::string &filename); + static float _parseExplosionRadius(std::string &filename); + static Vector3f _parsePosition(std::string &filename); static MapGenerator::BlockType _parseBlockType(std::string blockType); static Bonus::BonusType _parseBonusType(std::string bonusType); + static void _loadPlayer(std::shared_ptr scene, std::vector lines, int &index); + static void _loadBlock(std::shared_ptr scene, std::vector lines, int &index); + static void _loadBonus(std::shared_ptr scene, std::vector lines, int &index); + 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); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index ee036355..1fa579d5 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -41,6 +41,7 @@ #include "System/MenuControllable/MenuControllableSystem.hpp" #include #include +#include #include "Component/Music/MusicComponent.hpp" #include "Component/Sound/SoundComponent.hpp" #include "System/Sound/PlayerSoundManagerSystem.hpp" @@ -56,11 +57,13 @@ namespace BBM void Runner::updateState(WAL::Wal &engine, GameState &state) { - if (RAY::Window::getInstance().shouldClose()) + if (RAY::Window::getInstance().shouldClose()) { engine.shouldClose = true; + } if (gameState.currentScene == GameState::SceneID::GameScene) { for (auto &[_, component]: engine.getScene()->view()) { if (component.pause) { + ParserYAML::save(engine.getScene(), "test"); gameState.nextScene = GameState::SceneID::PauseMenuScene; break; } @@ -544,7 +547,7 @@ namespace BBM .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); - + //ParserYAML::load(scene, "test"); return scene; }