diff --git a/assets/Json/enemyData.json b/assets/Json/enemyData.json index 60acc03f..501aa1d6 100644 --- a/assets/Json/enemyData.json +++ b/assets/Json/enemyData.json @@ -2,7 +2,7 @@ "enemy" : [ { "spritePath": "assets/R-TypeSheet/r-typesheet20.gif", - "damage": 10.0, + "damage": 10, "health": 1.0, "width": 10.0, "height": 10.0, diff --git a/src/Client/Systems/Graphic/ParallaxSystems.cpp b/src/Client/Systems/Graphic/ParallaxSystems.cpp index a759d11c..ee78ab3b 100644 --- a/src/Client/Systems/Graphic/ParallaxSystems.cpp +++ b/src/Client/Systems/Graphic/ParallaxSystems.cpp @@ -17,15 +17,16 @@ namespace Systems::ParallaxSystems { { std::size_t id = Registry::getInstance().addEntity(); Raylib::Sprite parralax = { - Json::getInstance().getDataFromJson(elem, "spritePath"), - Json::getInstance().getDataFromJson(elem, "width"), - Json::getInstance().getDataFromJson(elem, "height"), + Json::getInstance().getDataFromJson(elem, "spritePath"), + Json::getInstance().getDataFromJson(elem, "width"), + Json::getInstance().getDataFromJson(elem, "height"), id}; - Types::Position position = {Types::Position(Json::getInstance().getDataFromJson(elem, "position"))}; - Types::Velocity velocity = {Types::Velocity(Json::getInstance().getDataFromJson(elem, "velocity"))}; + + Types::Position position = Json::getInstance().getDataFromJson(elem, "position"); + Types::Velocity velocity = Json::getInstance().getDataFromJson(elem, "velocity"); if (Json::getInstance().isDataExist(elem, "rect")) { - Types::Rect rect = {Types::Rect(Json::getInstance().getDataFromJson(elem, "rect"))}; + Types::Rect rect = Json::getInstance().getDataFromJson(elem, "rect"); Registry::getInstance().getComponents().insertBack((rect)); } @@ -49,7 +50,7 @@ namespace Systems::ParallaxSystems { for (auto &elem : parallaxData) { initParallaxEntity(elem); if (Json::getInstance().isDataExist(elem, "copy") - && Json::getInstance().getDataFromJson(elem, "copy") == true) { + && Json::getInstance().getDataFromJson(elem, "copy") == true) { initParallaxEntity(elem, maxOutParallaxRight); } } diff --git a/src/ECS/Json.cpp b/src/ECS/Json.cpp index 6d11a042..77c7b815 100644 --- a/src/ECS/Json.cpp +++ b/src/ECS/Json.cpp @@ -9,7 +9,6 @@ #include #include #include -#include "Logger.hpp" // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) Json Json::_instance = Json(); @@ -55,14 +54,6 @@ nlohmann::basic_json<> Json::getDataByJsonType(JsonType dataType) return (data); } -nlohmann::basic_json<> &Json::getDataFromJson(nlohmann::basic_json<> jsonData, const std::string &index) -{ - if (jsonData[index] == nullptr) { - Logger::error(std::string("Key : " + index + " is not valid")); - } - return jsonData[index]; -} - nlohmann::basic_json<> Json::getDataByJsonType(const std::string &index, JsonType dataType) { nlohmann::basic_json<> finalData(_jsonDatas[dataType]); diff --git a/src/ECS/Json.hpp b/src/ECS/Json.hpp index 54b0d2dd..a9e99484 100644 --- a/src/ECS/Json.hpp +++ b/src/ECS/Json.hpp @@ -9,6 +9,7 @@ #include #include #include +#include "Logger.hpp" enum class JsonType { DEFAULT_ENEMY, @@ -35,8 +36,6 @@ class Json { std::vector> getDatasByJsonType(const std::vector &indexes, JsonType dataType); - nlohmann::json &getDataFromJson(nlohmann::basic_json<> jsonData, const std::string &index); - bool isDataExist(nlohmann::basic_json<> jsonData, const std::string &index); std::vector> @@ -47,7 +46,14 @@ class Json { std::vector> getDatasFromList(const nlohmann::basic_json<> &list); - protected: + template + T getDataFromJson(nlohmann::basic_json<> jsonData, const std::string &index) + { + if (jsonData[index] == nullptr) { + Logger::error(std::string("Key : " + index + " is not valid")); + } + return jsonData[index].get(); + } private: Json(); diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index a8f28388..f906a7a5 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -186,41 +186,46 @@ namespace Systems { for (auto &elem : enemyData) { #ifdef CLIENT std::size_t id = Registry::getInstance().addEntity(); +#else + Registry::getInstance().addEntity(); +#endif + +#ifdef CLIENT - Types::Rect rect = {Types::Rect(Json::getInstance().getDataFromJson(elem, "rect"))}; Types::SpriteDatas enemy = { - Json::getInstance().getDataFromJson(elem, "spritePath"), - Json::getInstance().getDataFromJson(elem, "width"), - Json::getInstance().getDataFromJson(elem, "height"), + Json::getInstance().getDataFromJson(elem, "spritePath"), + Json::getInstance().getDataFromJson(elem, "width"), + Json::getInstance().getDataFromJson(elem, "height"), id, LayerType::DEFAULTLAYER, 0}; - nlohmann::basic_json<> animRectData = Json::getInstance().getDataFromJson(elem, "animRect"); - Types::AnimRect animRect = { + Types::Rect rect = Json::getInstance().getDataFromJson(elem, "rect"); + + nlohmann::basic_json<> animRectData = + Json::getInstance().getDataFromJson>(elem, "animRect"); + Types::AnimRect animRect = { rect, - Json::getInstance().getDataFromJson(animRectData, "move").get>(), - Json::getInstance().getDataFromJson(animRectData, "attack").get>(), - Json::getInstance().getDataFromJson(animRectData, "dead").get>()}; -#else - Registry::getInstance().addEntity(); + Json::getInstance().getDataFromJson>(animRectData, "move"), + Json::getInstance().getDataFromJson>(animRectData, "attack"), + Json::getInstance().getDataFromJson>(animRectData, "dead")}; + #endif - Types::Enemy enemyComp = (setId ? Types::Enemy {enemyId} : Types::Enemy {}); - Types::Position position = { - Types::Position(Json::getInstance().getDataFromJson(elem, "position"))}; - Types::CollisionRect collisionRect = { - Types::CollisionRect(Json::getInstance().getDataFromJson(elem, "collisionRect"))}; - struct health_s healthComp = {Json::getInstance().getDataFromJson(elem, "health")}; - Types::Damage damageComp = {Json::getInstance().getDataFromJson(elem, "damage")}; - Types::Velocity velocity = { - Types::Velocity(Json::getInstance().getDataFromJson(elem, "velocity"))}; + Types::Enemy enemyComp = (setId ? Types::Enemy {enemyId} : Types::Enemy {}); + Types::Velocity velocity = + Json::getInstance().getDataFromJson(elem, "velocity"); + Types::Position position = + Json::getInstance().getDataFromJson(elem, "position"); + Types::CollisionRect collisionRect = + Json::getInstance().getDataFromJson(elem, "collisionRect"); + Types::Damage damageComp = {Json::getInstance().getDataFromJson(elem, "damage")}; + struct health_s healthComp = {Json::getInstance().getDataFromJson(elem, "health")}; #ifdef CLIENT - Registry::getInstance().getComponents().insertBack((rect)); + Registry::getInstance().getComponents().insertBack(rect); Registry::getInstance().getComponents().insertBack(animRect); Registry::getInstance().getComponents().insertBack(enemy); #endif - Registry::getInstance().getComponents().insertBack(position); Registry::getInstance().getComponents().insertBack(collisionRect); Registry::getInstance().getComponents().insertBack(velocity); @@ -382,9 +387,9 @@ namespace Systems { Json::getInstance().getDataByVector({"player", "animRect"}, playerType); Types::AnimRect animRect = { rect, - Json::getInstance().getDataFromJson(animRectData, "move").get>(), - Json::getInstance().getDataFromJson(animRectData, "attack").get>(), - Json::getInstance().getDataFromJson(animRectData, "dead").get>()}; + Json::getInstance().getDataFromJson>(animRectData, "move"), + Json::getInstance().getDataFromJson>(animRectData, "attack"), + Json::getInstance().getDataFromJson>(animRectData, "dead")}; #endif Types::Position position = {