Skip to content

Commit

Permalink
ECS: Modif now read data from json and create entity
Browse files Browse the repository at this point in the history
MAJOR
  • Loading branch information
KitetsuK committed Oct 4, 2023
1 parent b3cfa33 commit 52ed767
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 72 deletions.
3 changes: 2 additions & 1 deletion src/Client/Systems/CustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
7 changes: 7 additions & 0 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
#include <cstddef>
#include <functional>
#include <optional>
#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 {
Expand All @@ -26,6 +31,8 @@ namespace Types {
struct Position {
float x;
float y;

NLOHMANN_DEFINE_TYPE_INTRUSIVE(Position, x, y);
};

struct Health {
Expand Down
12 changes: 6 additions & 6 deletions src/ECS/Systems/Managers/SystemManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Systems {
std::vector<std::function<void(std::size_t, std::size_t)>> systems)
: _id(_managerNb),
_originalSystems(std::move(systems)),
_modifiedSystems(_originalSystems),
_modified(false)
{
_managerNb += 1;
Expand All @@ -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
Expand All @@ -62,6 +61,7 @@ namespace Systems {

void SystemManager::resetChanges()
{
std::cout << "je passes la dedans" << std::endl;
_modified = false;
_modifiedSystems.clear();
}
Expand Down
217 changes: 152 additions & 65 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "CustomTypes.hpp"
#include "Raylib.hpp"
#include "Registry.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "SystemManagersDirector.hpp"

namespace Systems {
Expand Down Expand Up @@ -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<> &parallaxData)
{
std::size_t id = Registry::getInstance().addEntity();

Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{parallaxData["spritePath"], parallaxData["width"], parallaxData["height"], id}
);
Registry::getInstance().getComponents<Types::Position>().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 &registry = Registry::getInstance();
Expand All @@ -93,6 +141,7 @@ namespace Systems {
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::CollisionRect), typeid(Types::Position)});


for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) {
checkCollisionEntity(itIds, ids, arrPosition, arrCollisionRect);
}
Expand Down Expand Up @@ -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<Raylib::Sprite>().insertBack(
{jsonData["spritePath"], jsonData["width"], jsonData["height"], id}
);
Registry::getInstance().getComponents<Types::Position>().insertBack(
{Types::Position(jsonData["position"])}
);
Registry::getInstance().getComponents<Types::Rect>().insertBack(
{Types::Rect(jsonData["rect"])}
);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(
{Types::CollisionRect(jsonData["collisionRect"])}
);
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(
Types::AnimRect(Types::Rect(jsonData["rect"]),
std::vector<Types::Rect>(jsonData["animRect"]["move"]),
std::vector<Types::Rect>(jsonData["animRect"]["attack"]),
std::vector<Types::Rect>(jsonData["animRect"]["dead"]))
);
Registry::getInstance().getComponents<Types::Player>().insertBack({});
Registry::getInstance().getComponents<Types::Damage>().insertBack(
{jsonData["damage"]}
);
Registry::getInstance().getComponents<Types::Health>().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};
Expand All @@ -145,78 +232,78 @@ namespace Systems {

void init(std::size_t managerId, std::size_t systemId)
{
std::size_t id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
playerPos);
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
// std::size_t id = Registry::getInstance().addEntity();
// Registry::getInstance().getComponents<Types::Position>().insertBack(
// playerPos);
// Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
// {playerPath, playerWidth, playerHeight, id});
// Registry::getInstance().getComponents<Types::Rect>().insertBack(
// spriteRect);
// Registry::getInstance()
// .getComponents<Types::CollisionRect>()
// .insertBack(collisionRect);
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
Registry::getInstance().getComponents<Types::AnimRect>().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<Types::AnimRect>().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<Types::Player>().insertBack({});
Registry::getInstance().getComponents<Types::Damage>().insertBack(
{playerDamage});
Registry::getInstance().getComponents<Types::Health>().insertBack(
{playerHealth});
Registry::getInstance().getComponents<Types::Dead>().insertBack(
{std::nullopt});
// Registry::getInstance().setToBackLayers(id);
// Registry::getInstance().getComponents<Types::Player>().insertBack({});
// Registry::getInstance().getComponents<Types::Damage>().insertBack(
// {playerDamage});
// Registry::getInstance().getComponents<Types::Health>().insertBack(
// {playerHealth});
// Registry::getInstance().getComponents<Types::Dead>().insertBack(
// {std::nullopt});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Enemy>().insertBack({});
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData + playerData + playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
spriteRect);
Registry::getInstance()
.getComponents<Types::CollisionRect>()
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Dead>().insertBack(
{std::nullopt});
Registry::getInstance().setToFrontLayers(id);

Registry::getInstance().getComponents<Raylib::Music>().insertBack(
{musicPath, musicVolume});
Registry::getInstance().getComponents<Raylib::Sound>().insertBack(
{soundPath, soundVolume});
Registry::getInstance().getComponents<Raylib::Text>().insertBack(
{"Press SPACE to play music, ENTER to play sound, J to reset "
"scene, ARROWS to move",
textPos,
fontScale,
Raylib::DarkBlue});
Registry::getInstance().getComponents<Types::Damage>().insertBack(
{enemyDamage});
Registry::getInstance().getComponents<Types::Health>().insertBack(
{playerHealth2});
// id = Registry::getInstance().addEntity();
// Registry::getInstance().getComponents<Types::Enemy>().insertBack({});
// Registry::getInstance().getComponents<Types::Position>().insertBack(
// {playerData, playerData + playerData + playerData});
// // Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
// // {playerPath, playerWidth, playerHeight, id});
// Registry::getInstance().getComponents<Types::Rect>().insertBack(
// spriteRect);
// Registry::getInstance()
// .getComponents<Types::CollisionRect>()
// .insertBack(collisionRect);
// Registry::getInstance().getComponents<Types::Dead>().insertBack(
// {std::nullopt});
// Registry::getInstance().setToFrontLayers(id);

// Registry::getInstance().getComponents<Raylib::Music>().insertBack(
// {musicPath, musicVolume});
// Registry::getInstance().getComponents<Raylib::Sound>().insertBack(
// {soundPath, soundVolume});
// Registry::getInstance().getComponents<Raylib::Text>().insertBack(
// {"Press SPACE to play music, ENTER to play sound, J to reset "
// "scene, ARROWS to move",
// textPos,
// fontScale,
// Raylib::DarkBlue});
// Registry::getInstance().getComponents<Types::Damage>().insertBack(
// {enemyDamage});
// Registry::getInstance().getComponents<Types::Health>().insertBack(
// {playerHealth2});
SystemManagersDirector::getInstance()
.getSystemManager(managerId)
.removeSystem(systemId);
}

std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems()
{
return {windowCollision, init, entitiesCollision, deathChecker};
return {init, initParalax, initPlayer, entitiesCollision, windowCollision, deathChecker};
}
} // namespace Systems

0 comments on commit 52ed767

Please sign in to comment.