ComponentFactory class is responsible for creating and managing components for entities within the ECS architecture.namespace ecs {
class ComponentFactory {
public:
ComponentFactory(std::shared_ptr<Registry> ®istry);
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
ComponentFactory(std::shared_ptr<Registry> ®istry)
ComponentFactory with a reference to a Registry instance to manage entities and components.position, drawable, sprite, etc.) in a map for later use.ComponentFactory()
ComponentFactory class.~ComponentFactory()
ComponentFactory instance.void createEntity(const std::string &file)
file: The path to the configuration file that contains entity and component definitions.createComponent.void createComponent(const Entity e, const std::string &component, const nlohmann::json &node)
e: The entity to which the component will be attached.component: The name of the component to create (like position, sprite, etc.).node: A JSON node containing the component's properties and values.functions) to invoke the correct component creation function.std::shared_ptr<Registry> _r
Registry object that manages entities and their components.std::unordered_map<std::string, std::function<void(const Entity, const nlohmann::json)>> functions
std::string) to functions (std::function) that create components.Entity and a nlohmann::json object (containing component data) as input.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
};
}
void createPositionComponent(const Entity e, const nlohmann::json &node)
Position component and attaches it to the entity.x: X-coordinate of the position.y: Y-coordinate of the position.void createDrawableComponent(const Entity e, const nlohmann::json &node)
Drawable component and attaches it to the entity.void createSpriteComponent(const Entity e, const nlohmann::json &node)
Sprite component and attaches it to the entity.void createAnimationsComponent(const Entity e, const nlohmann::json &node)
Animations component and attaches it to the entity.player, weapon, ennemies, background).type field in the JSON node to determine the type of object the animation belongs to (e.g., Player, Weapon).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 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 createSoundEffectComponent(const Entity e, const nlohmann::json &node)void createVelocityComponent(const Entity e, const nlohmann::json &node)