diff --git a/src/Client/Systems/CustomTypes.hpp b/src/Client/Systems/CustomTypes.hpp index d904b8d2..0acdfc4e 100644 --- a/src/Client/Systems/CustomTypes.hpp +++ b/src/Client/Systems/CustomTypes.hpp @@ -12,12 +12,13 @@ #include "ECSCustomTypes.hpp" namespace Types { - struct Rect { float x; float y; float width; float height; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(Rect, x, y, width, height); }; enum RectListType { DEFAULTRECT, MOVE, ATTACK, DEAD }; diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 1f118b70..d080414b 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -10,12 +10,17 @@ #include #include #include +#include "nlohmann/json.hpp" + +// all values are in percentage of the screen namespace Types { struct CollisionRect { float width; float height; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(CollisionRect, width, height); }; struct RectangleShape { @@ -26,6 +31,8 @@ namespace Types { struct Position { float x; float y; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(Position, x, y); }; struct Health { diff --git a/src/ECS/Systems/Managers/SystemManager.cpp b/src/ECS/Systems/Managers/SystemManager.cpp index 2498e767..56af1cea 100644 --- a/src/ECS/Systems/Managers/SystemManager.cpp +++ b/src/ECS/Systems/Managers/SystemManager.cpp @@ -24,6 +24,7 @@ namespace Systems { std::vector> systems) : _id(_managerNb), _originalSystems(std::move(systems)), + _modifiedSystems(_originalSystems), _modified(false) { _managerNb += 1; @@ -35,16 +36,14 @@ namespace Systems { for (auto &system : getSystems()) { system(_id, i); - std::cout << "System id : " << i << std::endl; i++; } for (auto &id : _toRemove) { - if (getSystems().size() > 0) { - auto it = _modifiedSystems.begin(); - std::advance(it, id); - _modifiedSystems.erase(it); - } + auto it = _modifiedSystems.begin(); + std::advance(it, id); + _modifiedSystems.erase(it); } + _toRemove.clear(); } void @@ -62,6 +61,7 @@ namespace Systems { void SystemManager::resetChanges() { + std::cout << "je passes la dedans" << std::endl; _modified = false; _modifiedSystems.clear(); } diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 880f93eb..00ef64b2 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -10,6 +10,10 @@ #include "CustomTypes.hpp" #include "Raylib.hpp" #include "Registry.hpp" +#include +#include +#include +#include #include "SystemManagersDirector.hpp" namespace Systems { @@ -83,6 +87,50 @@ namespace Systems { } } + static nlohmann::json openJsonData(const std::string &path) + { + std::ifstream fileData(path); + std::ostringstream input; + nlohmann::json jsonData = {}; + + if (fileData.is_open()) { + input << fileData.rdbuf(); + if (nlohmann::json::accept(input.str())) { + jsonData = nlohmann::json::parse(input.str()); + return jsonData; + } + } + std::cout << "Could not load the json data : An error occured." << std::endl; + return jsonData; + } + + static void initParallaxEntity(nlohmann::json_abi_v3_11_2::basic_json<> ¶llaxData) + { + std::size_t id = Registry::getInstance().addEntity(); + + Registry::getInstance().getComponents().insertBack( + {parallaxData["spritePath"], parallaxData["width"], parallaxData["height"], id} + ); + Registry::getInstance().getComponents().insertBack( + {Types::Position(parallaxData["position"])} + ); + Registry::getInstance().setToBackLayers(id); + } + + void initParalax(std::size_t managerId, std::size_t systemId) + { + nlohmann::json jsonData = openJsonData("assets/Json/parallaxData.json"); + + for (auto &e : jsonData["parallax"]) { + initParallaxEntity(e); + } std::cout << "C est pas bon" << std::endl; + + + SystemManagersDirector::getInstance() + .getSystemManager(managerId) + .removeSystem(systemId); + } + void entitiesCollision(std::size_t /*unused*/, std::size_t /*unused*/) { Registry ®istry = Registry::getInstance(); @@ -93,6 +141,7 @@ namespace Systems { std::vector ids = registry.getEntitiesByComponents( {typeid(Types::CollisionRect), typeid(Types::Position)}); + for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) { checkCollisionEntity(itIds, ids, arrPosition, arrCollisionRect); } @@ -125,9 +174,47 @@ namespace Systems { } } + void initPlayer(std::size_t managerId, std::size_t systemId) + { + nlohmann::json jsonData = openJsonData("assets/Json/playerData.json"); + std::size_t id = Registry::getInstance().addEntity(); + + if (jsonData["player"] == nullptr) { + return; + } + jsonData = jsonData["player"]; + Registry::getInstance().getComponents().insertBack( + {jsonData["spritePath"], jsonData["width"], jsonData["height"], id} + ); + Registry::getInstance().getComponents().insertBack( + {Types::Position(jsonData["position"])} + ); + Registry::getInstance().getComponents().insertBack( + {Types::Rect(jsonData["rect"])} + ); + Registry::getInstance().getComponents().insertBack( + {Types::CollisionRect(jsonData["collisionRect"])} + ); + Registry::getInstance().getComponents().insertBack( + Types::AnimRect(Types::Rect(jsonData["rect"]), + std::vector(jsonData["animRect"]["move"]), + std::vector(jsonData["animRect"]["attack"]), + std::vector(jsonData["animRect"]["dead"])) + ); + Registry::getInstance().getComponents().insertBack({}); + Registry::getInstance().getComponents().insertBack( + {jsonData["damage"]} + ); + Registry::getInstance().getComponents().insertBack( + {jsonData["health"]} + ); + SystemManagersDirector::getInstance() + .getSystemManager(managerId) + .removeSystem(systemId); + } + const std::string musicPath = "assets/Audio/Musics/Title.mp3"; - const std::string soundPath = "assets/Audio/Sounds/fire.ogg"; - const std::string playerPath = "assets/R-TypeSheet/r-typesheet14.gif"; + const std::string soundPath = "assets/Audio/Sounds/fire.gif"; const Types::Rect spriteRect = {2, 2, 48, 48}; const Types::CollisionRect collisionRect = {25, 25}; const Raylib::Vector2 textPos = {20, 50}; @@ -145,71 +232,71 @@ namespace Systems { void init(std::size_t managerId, std::size_t systemId) { - std::size_t id = Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().insertBack( - playerPos); - Registry::getInstance().getComponents().insertBack( - {playerPath, playerWidth, playerHeight, id}); - Registry::getInstance().getComponents().insertBack( - spriteRect); - Registry::getInstance() - .getComponents() - .insertBack(collisionRect); + // std::size_t id = Registry::getInstance().addEntity(); + // Registry::getInstance().getComponents().insertBack( + // playerPos); + // Registry::getInstance().getComponents().insertBack( + // {playerPath, playerWidth, playerHeight, id}); + // Registry::getInstance().getComponents().insertBack( + // spriteRect); + // Registry::getInstance() + // .getComponents() + // .insertBack(collisionRect); // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - Registry::getInstance().getComponents().insertBack({ - spriteRect, - {{2, 51, 46, 47}, - {101, 2, 48, 47}, - {152, 2, 46, 47}, - {201, 2, 46, 47} }, - {{2, 51, 46, 47}, - {101, 2, 48, 47}, - {152, 2, 46, 47}, - {201, 2, 46, 47} }, - {{180, 140, 18, 12}, - {211, 140, 18, 12}, - {230, 140, 18, 12}, - {250, 140, 18, 12}} - }); + // Registry::getInstance().getComponents().insertBack({ + // spriteRect, + // {{2, 51, 46, 47}, + // {101, 2, 48, 47}, + // {152, 2, 46, 47}, + // {201, 2, 46, 47} }, + // {{2, 51, 46, 47}, + // {101, 2, 48, 47}, + // {152, 2, 46, 47}, + // {201, 2, 46, 47} }, + // {{180, 140, 18, 12}, + // {211, 140, 18, 12}, + // {230, 140, 18, 12}, + // {250, 140, 18, 12}} + // }); // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - Registry::getInstance().setToBackLayers(id); - Registry::getInstance().getComponents().insertBack({}); - Registry::getInstance().getComponents().insertBack( - {playerDamage}); - Registry::getInstance().getComponents().insertBack( - {playerHealth}); - Registry::getInstance().getComponents().insertBack( - {std::nullopt}); + // Registry::getInstance().setToBackLayers(id); + // Registry::getInstance().getComponents().insertBack({}); + // Registry::getInstance().getComponents().insertBack( + // {playerDamage}); + // Registry::getInstance().getComponents().insertBack( + // {playerHealth}); + // Registry::getInstance().getComponents().insertBack( + // {std::nullopt}); - id = Registry::getInstance().addEntity(); - Registry::getInstance().getComponents().insertBack({}); - Registry::getInstance().getComponents().insertBack( - {playerData, playerData + playerData + playerData}); - Registry::getInstance().getComponents().insertBack( - {playerPath, playerWidth, playerHeight, id}); - Registry::getInstance().getComponents().insertBack( - spriteRect); - Registry::getInstance() - .getComponents() - .insertBack(collisionRect); - Registry::getInstance().getComponents().insertBack( - {std::nullopt}); - Registry::getInstance().setToFrontLayers(id); - - Registry::getInstance().getComponents().insertBack( - {musicPath, musicVolume}); - Registry::getInstance().getComponents().insertBack( - {soundPath, soundVolume}); - Registry::getInstance().getComponents().insertBack( - {"Press SPACE to play music, ENTER to play sound, J to reset " - "scene, ARROWS to move", - textPos, - fontScale, - Raylib::DarkBlue}); - Registry::getInstance().getComponents().insertBack( - {enemyDamage}); - Registry::getInstance().getComponents().insertBack( - {playerHealth2}); + // id = Registry::getInstance().addEntity(); + // Registry::getInstance().getComponents().insertBack({}); + // Registry::getInstance().getComponents().insertBack( + // {playerData, playerData + playerData + playerData}); + // // Registry::getInstance().getComponents().insertBack( + // // {playerPath, playerWidth, playerHeight, id}); + // Registry::getInstance().getComponents().insertBack( + // spriteRect); + // Registry::getInstance() + // .getComponents() + // .insertBack(collisionRect); + // Registry::getInstance().getComponents().insertBack( + // {std::nullopt}); + // Registry::getInstance().setToFrontLayers(id); + + // Registry::getInstance().getComponents().insertBack( + // {musicPath, musicVolume}); + // Registry::getInstance().getComponents().insertBack( + // {soundPath, soundVolume}); + // Registry::getInstance().getComponents().insertBack( + // {"Press SPACE to play music, ENTER to play sound, J to reset " + // "scene, ARROWS to move", + // textPos, + // fontScale, + // Raylib::DarkBlue}); + // Registry::getInstance().getComponents().insertBack( + // {enemyDamage}); + // Registry::getInstance().getComponents().insertBack( + // {playerHealth2}); SystemManagersDirector::getInstance() .getSystemManager(managerId) .removeSystem(systemId); @@ -217,6 +304,6 @@ namespace Systems { std::vector> getECSSystems() { - return {windowCollision, init, entitiesCollision, deathChecker}; + return {init, initParalax, initPlayer, entitiesCollision, windowCollision, deathChecker}; } } // namespace Systems