The ComponentFactory class is responsible for creating and managing components for entities within the ECS architecture.

This factory reads configuration files, interprets component data, and attaches the respective components to the entities in the registry.

namespace ecs {
    class ComponentFactory {
      public:
        ComponentFactory(std::shared_ptr<Registry> &registry);
        ComponentFactory();
        ~ComponentFactory();

        void createEntity(const std::string &file);
        void createComponent(const Entity e, const std::string &component, const nlohmann::json &node);

      protected:
      private:
        std::shared_ptr<Registry> _r;

        void createPositionComponent(const Entity e, const nlohmann::json &node);
        void createSpriteComponent(const Entity e, const nlohmann::json &node);
        void createAnimationsComponent(const Entity e, const nlohmann::json &node);
        void createParallaxComponent(const Entity e, const nlohmann::json &node);
        void createTextComponent(const Entity e, const nlohmann::json &node);
        void createClickedComponent(const Entity e, const nlohmann::json &node);
        void createControllableComponent(const Entity e, const nlohmann::json &node);
        void createDrawableComponent(const Entity e, const nlohmann::json &node);
        void createFilledColorComponent(const Entity e, const nlohmann::json &node);
        void createHoverComponent(const Entity e, const nlohmann::json &node);
        void createMusicComponent(const Entity e, const nlohmann::json &node);
        void createOutlinedColorComponent(const Entity e, const nlohmann::json &node);
        void createScoreComponent(const Entity e, const nlohmann::json &node);
        void createShieldComponent(const Entity e, const nlohmann::json &node);
        void createSizeComponent(const Entity e, const nlohmann::json &node);
        void createSoundEffectComponent(const Entity e, const nlohmann::json &node);
        void createVelocityComponent(const Entity e, const nlohmann::json &node);
        std::unordered_map<std::string, std::function<void(const Entity, const nlohmann::json)>> functions;
    };

} // namespace ecs

Public Members:

Private Members:

Couple of Methods (main) :

ComponentFactory::ComponentFactory(std::shared_ptr<Registry> &r) : _r(r)
    {
        functions["position"] = [this](const Entity e, const nlohmann::json &node) {
            createPositionComponent(e, node);
        };
        functions["drawable"] = [this](const Entity e, const nlohmann::json &node) {
            createDrawableComponent(e, node);
        };
        functions["sprite"] = [this](const Entity e, const nlohmann::json &node) { createSpriteComponent(e, node); };
        functions["animations"] = [this](const Entity e, const nlohmann::json &node) {
            createAnimationsComponent(e, node);
        };
        functions["parallax"] = [this](const Entity e, const nlohmann::json &node) {
            createParallaxComponent(e, node);
        };
        functions["size"] = [this](const Entity e, const nlohmann::json &node) { createSizeComponent(e, node); };

        functions["text"] = [this](const Entity e, const nlohmann::json &node) { createTextComponent(e, node); };
        functions["clicked"] = [this](const Entity e, const nlohmann::json &node) { createClickedComponent(e, node); };
        functions["controllable"] = [this](const Entity e, const nlohmann::json &node) {
            createControllableComponent(e, node);
        };
        functions["filledColor"] = [this](const Entity e, const nlohmann::json &node) {
            createFilledColorComponent(e, node);
        };
        functions["hover"] = [this](const Entity e, const nlohmann::json &node) { createHoverComponent(e, node); };
        functions["music"] = [this](const Entity e, const nlohmann::json &node) { createMusicComponent(e, node); };
        functions["outlinedColor"] = [this](const Entity e, const nlohmann::json &node) {
            createOutlinedColorComponent(e, node);
        };
        functions["score"] = [this](const Entity e, const nlohmann::json &node) { createScoreComponent(e, node); };
        functions["shield"] = [this](const Entity e, const nlohmann::json &node) { createShieldComponent(e, node); };
        functions["soundEffect"] = [this](const Entity e, const nlohmann::json &node) {
            createSoundEffectComponent(e, node);
        };
        functions["velocity"] = [this](const Entity e, const nlohmann::json &node) {
            createVelocityComponent(e, node);
        };
    }

    ComponentFactory::~ComponentFactory() {}

    void ComponentFactory::createEntity(const std::string &file)
    {
        std::ifstream f(file);
        nlohmann::json config = nlohmann::json::parse(f);

        Entity e = _r->spawn_entity();
        _r->_entitys.addEntity(e);

        for (auto &c : config["active"]) {
            createComponent(e, c, config["components"][c]);
        }
    }

    void ComponentFactory::createComponent(const Entity e, const std::string &name, const nlohmann::json &node)
    {
        functions[name](e, node);
    }

    void ComponentFactory::createPositionComponent(const Entity e, const nlohmann::json &node)
    {
        auto &pos_array = _r->register_if_not_exist<component::Position>();

        pos_array[e.getId()] = component::Position{node["x"], node["y"]};
    }

    void ComponentFactory::createDrawableComponent(const Entity e, const nlohmann::json &node)
    {
        auto &drawable_array = _r->register_if_not_exist<component::Drawable>();

        drawable_array[e.getId()] = component::Drawable{node};
    }

    void ComponentFactory::createSpriteComponent(const Entity e, const nlohmann::json &node)
    {
        auto &sprite_array = _r->register_if_not_exist<component::Sprite>();

        sprite_array[e.getId()] = component::Sprite{node};
    }

    void ComponentFactory::createAnimationsComponent(const Entity e, const nlohmann::json &node)
    {
        auto &animations_array = _r->register_if_not_exist<component::Animations>();

        component::Object type = component::Background;
        if (node["type"] == "player")
            type = component::Player;
        else if (node["type"] == "weapon")
            type = component::Weapon;
        else if (node["type"] == "ennemies")
            type = component::Ennemies;
        else if (node["type"] == "background")
            type = component::Background;

        animations_array[e.getId()] = component::Animations{
            sf::Clock(), node["width"], node["height"], node["x"], node["y"], node["rotation"], type
        };
    }

Private Methods: