Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/rb 119 fix json #95

Merged
merged 9 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/Json/enemyData.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions src/Client/Systems/Graphic/ParallaxSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(elem, "spritePath"),
Json::getInstance().getDataFromJson<float>(elem, "width"),
Json::getInstance().getDataFromJson<float>(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<Types::Position>(elem, "position");
Types::Velocity velocity = Json::getInstance().getDataFromJson<Types::Velocity>(elem, "velocity");

if (Json::getInstance().isDataExist(elem, "rect")) {
Types::Rect rect = {Types::Rect(Json::getInstance().getDataFromJson(elem, "rect"))};
Types::Rect rect = Json::getInstance().getDataFromJson<Types::Rect>(elem, "rect");
Registry::getInstance().getComponents<Types::Rect>().insertBack((rect));
}

Expand All @@ -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<bool>(elem, "copy") == true) {
initParallaxEntity(elem, maxOutParallaxRight);
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/ECS/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <fstream>
#include <sstream>
#include <utility>
#include "Logger.hpp"

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Json Json::_instance = Json();
Expand Down Expand Up @@ -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]);
Expand Down
12 changes: 9 additions & 3 deletions src/ECS/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
#include "Logger.hpp"

enum class JsonType {
DEFAULT_ENEMY,
Expand All @@ -35,8 +36,6 @@ class Json {
std::vector<nlohmann::basic_json<>>
getDatasByJsonType(const std::vector<std::string> &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<nlohmann::basic_json<>>
Expand All @@ -47,7 +46,14 @@ class Json {

std::vector<nlohmann::basic_json<>> getDatasFromList(const nlohmann::basic_json<> &list);

protected:
template <typename T>
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<T>();
}
TTENSHII marked this conversation as resolved.
Show resolved Hide resolved

private:
Json();
Expand Down
55 changes: 30 additions & 25 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(elem, "spritePath"),
Json::getInstance().getDataFromJson<float>(elem, "width"),
Json::getInstance().getDataFromJson<float>(elem, "height"),
id,
LayerType::DEFAULTLAYER,
0};

nlohmann::basic_json<> animRectData = Json::getInstance().getDataFromJson(elem, "animRect");
Types::AnimRect animRect = {
Types::Rect rect = Json::getInstance().getDataFromJson<Types::Rect>(elem, "rect");

nlohmann::basic_json<> animRectData =
Json::getInstance().getDataFromJson<nlohmann::basic_json<>>(elem, "animRect");
Types::AnimRect animRect = {
rect,
Json::getInstance().getDataFromJson(animRectData, "move").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "attack").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "dead").get<std::vector<Types::Rect>>()};
#else
Registry::getInstance().addEntity();
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(animRectData, "move"),
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(animRectData, "attack"),
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(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<Types::Velocity>(elem, "velocity");
Types::Position position =
Json::getInstance().getDataFromJson<Types::Position>(elem, "position");
Types::CollisionRect collisionRect =
Json::getInstance().getDataFromJson<Types::CollisionRect>(elem, "collisionRect");
Types::Damage damageComp = {Json::getInstance().getDataFromJson<int>(elem, "damage")};
struct health_s healthComp = {Json::getInstance().getDataFromJson<int>(elem, "health")};

#ifdef CLIENT
Registry::getInstance().getComponents<Types::Rect>().insertBack((rect));
Registry::getInstance().getComponents<Types::Rect>().insertBack(rect);
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::SpriteDatas>().insertBack(enemy);
#endif

Registry::getInstance().getComponents<Types::Position>().insertBack(position);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Velocity>().insertBack(velocity);
Expand Down Expand Up @@ -382,9 +387,9 @@ namespace Systems {
Json::getInstance().getDataByVector({"player", "animRect"}, playerType);
Types::AnimRect animRect = {
rect,
Json::getInstance().getDataFromJson(animRectData, "move").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "attack").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "dead").get<std::vector<Types::Rect>>()};
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(animRectData, "move"),
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(animRectData, "attack"),
Json::getInstance().getDataFromJson<std::vector<Types::Rect>>(animRectData, "dead")};

#endif
Types::Position position = {
Expand Down