Entities factory

Factory to create entities

class Factory {
    public:
        Factory(ECS::Registry &registry);
        ~Factory();
        entity_t createButton(float x, float y, const std::shared_ptr<sf::Texture> &texture, const sf::Vector2f &scale, std::function<void()> callback);
        entity_t createScreen(float x, float y, const std::shared_ptr<sf::Texture> &texture, const sf::Vector2f &scale = {1.0, 1.0});
        entity_t createPlayer(float x, float y, const std::shared_ptr<sf::Texture> &texture);
        entity_t createParallax(float x, float y, const std::shared_ptr<sf::Texture> &texture, float scrollSpeed, const sf::Vector2f &scale, float ratio = 1.0f);
        entity_t createMissile(float x, float y, const std::shared_ptr<sf::Texture> &texture);
        entity_t createEnnemi(float x, float y, const std::shared_ptr<sf::Texture> &texture);
        entity_t createEnnemi4frames(float x, float y, const std::shared_ptr<sf::Texture> &texture, float scale);
        entity_t createBlackband(sf::IntRect rect, const std::shared_ptr<sf::Texture> &texture);

        entity_t createScoreCoche(float x, float y, const std::shared_ptr<sf::Texture> &texture, float scale);

        entity_t createMusic(const std::string &musicPath, float volume = 100.0f, bool loop = false);
        entity_t createStrobe(const std::shared_ptr<sf::Texture> &texture, float x, float y);

        entity_t createText(const std::string &text, const std::shared_ptr<sf::Font> &font, const float &x = 0, const float &y = 0, const size_t &size = 20, const sf::Color &color = sf::Color::White, const sf::Text::Style &style = sf::Text::Style::Regular);
        entity_t createLoadingBar(float x, float y, const std::shared_ptr<sf::Texture> &texture, const std::shared_ptr<sf::Texture> &textureRect, float scale);
        entity_t createSound(const std::string &soundPath, float volume = 100.0f, bool play = false);
        entity_t createBonus(const std::shared_ptr<sf::Texture> &texture, float x, float y, float scale);
        entity_t createMissileAnnimated(float x, float y, const std::shared_ptr<sf::Texture> &texture, float scale);
        entity_t createBomb(const std::shared_ptr<sf::Texture> &texture, float x, float y, float scale);
        entity_t createPod(const std::shared_ptr<sf::Texture> &texture, float scale);
        entity_t createExplosion(const std::shared_ptr<sf::Texture> &texture, float x, float y, float scale);
        entity_t createLaser(const std::shared_ptr<sf::Texture> &texture, float x, float y, float scale);
    private:
        ECS::Registry &_registry;
};

Entities factory is a class where you have to add a method to init an entity that you want in your game.

Firstly, we can see a constructor method where we have to register each component that we want to use for our entities.

Constructor method:

Factory::Factory(ECS::Registry &registry): _registry(registry)
{
    // register components
    this->_registry.register_component<ECS::components::SpriteComponent>();
    this->_registry.register_component<ECS::components::PositionComponent>();
    this->_registry.register_component<ECS::components::TextureRectComponent>();
    this->_registry.register_component<ECS::components::VelocityComponent>();
    this->_registry.register_component<ECS::components::ControllableComponent>();
    this->_registry.register_component<ECS::components::ParallaxComponent>();
    this->_registry.register_component<ECS::components::MovableComponent>();
    this->_registry.register_component<ECS::components::ScaleComponent>();
    this->_registry.register_component<ECS::components::ButtonComponent>();
    this->_registry.register_component<ECS::components::AnimationComponent>();
    this->_registry.register_component<ECS::components::MusicComponent>();
    this->_registry.register_component<ECS::components::TextComponent>();
    this->_registry.register_component<ECS::components::RectangleShapeComponent>();
    this->_registry.register_component<ECS::components::LoadingBarComponent>();
    this->_registry.register_component<ECS::components::SoundComponent>();
    this->_registry.register_component<ECS::components::AnimationOneTimeComponent>();
}

You have to add the component that you want to use (if still not registered).

Method example:

entity_t createPlayer(float x, float y, const std::shared_ptr<sf::Texture> &texture)

You will use this method to create a player and use it like this ->

entity_t Factory::createPlayer(float x, float y, const std::shared_ptr<sf::Texture> &texture)
{
    entity_t newEntity = _registry.spawn_entity();
    _registry.emplace_component<ECS::components::PositionComponent>(newEntity, ECS::components::PositionComponent{x, y});
    _registry.emplace_component<ECS::components::MovableComponent>(newEntity, ECS::components::MovableComponent{});
    _registry.emplace_component<ECS::components::ControllableComponent>(newEntity, ECS::components::ControllableComponent{sf::Keyboard::Key::Up, sf::Keyboard::Key::Down, sf::Keyboard::Key::Left, sf::Keyboard::Key::Right, sf::Keyboard::Key::Space});
    _registry.emplace_component<ECS::components::TextureRectComponent>(newEconst sf::Texturentity, ECS::components::TextureRectComponent{0, 0, (int)texture.getSize().x, (int)texture.getSize().y, 5, 150.0f});
    _registry.emplace_component<ECS::components::SpriteComponent>(newEntity, ECS::components::SpriteComponent{texture});
    _registry.emplace_component<ECS::components::AnimationComponent>(newEntity, ECS::components::AnimationComponent{});
    return newEntity;
}

To resume, in this method you will add all the components that you want on your entity.

Last updated