Components class

Some class for components

AnimationComponent || AnimationOneTimeComponent

class AnimationComponent {
    public:
        AnimationComponent(){}
    private:
};

class AnimationOneTimeComponent {
    public:
        AnimationOneTimeComponent(){}
        size_t _frame;
};

Can be used to animate an entity.

MovableComponent

class MovableComponent {
    public:
        MovableComponent(){}
    private:
};

When you add MovableComponent to an entity, it can move. Without this component, your entity cannot move.

ButtonComponent

class ButtonComponent {
    public:
        ButtonComponent(std::function<void()> callback);
        void executeAction();
    private:
        std::function<void()> _callback;
};

ButtonComponent is used for example to create a button. As you can see, you can give it a function or an anction to do when he's clicked and that's useful for a button.

ControllableComponent

class ControllableComponent {
    public:
        ControllableComponent(std::initializer_list<sf::Keyboard::Key> controls);
        const std::vector<sf::Keyboard::Key> &getControls() const;
        int getEvent() const;
        void setEvent(int event);

    private:
        std::vector<sf::Keyboard::Key> _controls;
        int _event = 0;
}; 

You will use this class to set your entity position.

  • The method ControllableComponent() present in this class will be useful to set which keys you want to control your entity. Ex: For our player, we set key up, key down, key right, key left.

  • The method getControls() will be useful to get all the keys registered with your entity. Ex: If user press key down, we will be loop on getControls function and check if the key has been pressed (if yes, player will move, otherwise player will not move).

  • The method getEvent() can be use to get actual events.

  • The method setEvent can set actuel event into an int.

LoadingBarComponent

class LoadingBarComponent {
    public:
        LoadingBarComponent(float maximumWidth, float textureWidth);
        float getMaximumWidth() const;
        float getTextureWidth() const;
        void setMaximumWidth(float maximumWidth);
        void setTextureWidth(float textureWidth);
        float calculate(float currentWidth);
    private:
        float _maximumWidth;
        float _textureWidth;
};

LoadingBarComponent is an usefull class to create flexible bar, like a life bar for example. The method calculate automatically set the width of your bar using its parameter float currentWidth.

MusicComponent

class MusicComponent {
    public:
        MusicComponent(const std::string &musicPath, float volume = 100.0f, bool loop = false): _musicPath(musicPath);
        int playMusic();
        int pauseMusic();
        int stopmusic();
        void setLoop(bool loop);
        void setVolume(float volume);
        sf::SoundSource::Status getStatus();
        float getVolume() const;
    private:
        std::string _musicPath;
        std::shared_ptr<sf::Music> _music;
};

MusicComponent as you may guess, is the component that you will use to handle the music behavior in your game. Its simply an encapsulation of the sfml sf::Music class.

ParallaxComponent

class ParallaxComponent {
    public:
        ParallaxComponent(float scrollSpeed, float backgroundWidth)
        float getScrollSpeed() const
        float getBackgroundWidth() const
        void setScrollSpeed(float scrollSpeed)

    private:
        float _scrollSpeed;
        float _backgroundWidth;
};

If you want to move an entity from left to the right automatically (like a parallax, ex: game's background) you can use the parallax component. This class needs to the size of your sprite sheet and the speed where it will move.

ParticuleComponent

class ParticuleComponent {
    public:
        ParticuleComponent(const sf::IntRect &rect, const sf::Color &color, const size_t &duration);
        std::chrono::system_clock::time_point getTime() const;
        sf::IntRect getRect() const;
        sf::Color getColor() const;
        size_t getDuration() const;
    private:
        std::chrono::system_clock::time_point _time;
        sf::IntRect _rect;
        sf::Color _color;
        size_t _duration;
};

The ParticuleComponent can be use to create particule with rectangle using sf::IntRect from SFML graphic library. You can either set the color of the particule and the duration.

PositionComponent

class PositionComponent {
    public:
        PositionComponent()
        PositionComponent(float x, float y)
        void setX(float newX)
        void setY(float newY)
        float getX() const
        float getY() const
    private:
        float _x;
        float _y;
};

This component can be use to set and get the position of an entity who've this component has been added.

  • At first, you will use this component to set the position of an entity at the. construction

  • Secondly, with the methods at your disposition, you will be able to change the current position of your entity (in game) "SET" methods.

  • Finally, you can get the position of your entity using "GET" methods.

RectangleShapeComponent

class RectangleShapeComponent {
    public:
        RectangleShapeComponent(const sf::Vector2f &size, const sf::Color &color, const sf::Vector2f &pos);
        RectangleShapeComponent(const std::shared_ptr<sf::Texture> &texture, const sf::IntRect &rect, const sf::Vector2f &pos);
        void setTexture(const std::shared_ptr<sf::Texture> &texture);
        void setSize(const sf::Vector2f &size);
        void setColor(const sf::Color &color);
        void setPosition(const sf::Vector2f &pos);
        void setTextureRect(const sf::IntRect &rect);
        const sf::IntRect &getTextureRect() const;
        const sf::Vector2f &getSize() const;
        const sf::Color &getColor() const;
        const sf::Vector2f &getPosition() const;
        sf::RectangleShape &getRectangleShape();

        void setScale(const float &scaleX, const float &scaleY):

    private:
        sf::RectangleShape _bar;
};

RectangleShapeComponent is the component that we use for the player life we can set which Rect that we want depending on the player life. You will find a lot of methods that are useful to handle the display of one of your entities.

ScaleComponent

class ScaleComponent {
    public:
        ScaleComponent()
        ScaleComponent(const float &scaleX, const float &scaleY)
        void setScale(const float &newScaleX, const float &newScaleY)
        float getScaleX() const
        float getScaleY() const
    private:
        float _scaleX;
        float _scaleY;
};

This component can be use if your entity does'nt fit as you want. Using this component, you can fit your entity as you want.

  • The first part of this class is useful to set the scale you want for your entity,

  • The second one will be useful if you want to get the scale set to an entity at his creation.

SoundComponent

class SoundComponent {
    public:
        SoundComponent(const std::string &soundPath, float volume = 100.0f, bool play = false): _soundPath(soundPath);
        int playSound();
        int pauseSound();
        int stopSound();
        void setVolume(float volume);
        sf::SoundSource::Status getStatus() const;
        float getVolume() const;
    private:
        std::string _soundPath;
        std::shared_ptr<sf::SoundBuffer> _soundBuffer;
        std::shared_ptr<sf::Sound> _sound;
};

SoundComponent is the component that we use for the sound created when the player shoots for example. A sound is different than a music because a sound will be shorter than a music. You will use that if you want a short sonore effect when an action has been done for example.

SpriteComponent

This component is use to store the entity's sprite

class SpriteComponent {
    public:
        SpriteComponent(const std::shared_ptr<sf::Texture> &texture)
        void setTexture(const sf::Texture &texture)
        void setPosition(const sf::Vector2f &position)
        void setScale(const float &scaleX, const float &scaleY)
        void setTextureRect(const sf::IntRect &rect)
        sf::Sprite &getSprite()
        const sf::Sprite &getSprite() const
    private:
        sf::Sprite _sprite;
};

If you're using this component, you can:

  • At first, you will create you sprite from a texture (give details/position)

  • After that's, you have can set the position of your sprite and set scale for your sprite

  • However, you can also get the sprite used using the getter method.

TextComponent

class TextComponent {
    public:
        TextComponent(const sf::Font &font, const std::string &str = "", const size_t &size = 20, const sf::Color &color = sf::Color::White, const sf::Text::Style &style = sf::Text::Style::Regular);
        void setString(const std::string &str);
        void setSize(const size_t &size);
        void setColor(const sf::Color &color);
        void setStyle(const sf::Text::Style &style);
        void setPosition(const float &x, const float &y);
        void setPosition(const sf::Vector2f &pos);
        void setScale(const float &x, const float &y);
        void setScale(const sf::Vector2f &scale);
        const sf::Text &getText() const;
    private:
        sf::Text _text;
};

TextComponent is the component that you have to use if you wantt to write something on your screen. In this one, you can adjust what you want like position, size, color, content or things like that for your text. You're free to write what you wan and above all how you want.

TextureRectComponent

class TextureRectComponent {
    public:
        TextureRectComponent(int left, int top, int totalWidth, int height, int numFrames, float frameDelay)
        sf::IntRect &getTextureRect()
        const sf::IntRect &getTextureRect() const
        sf::IntRect _textureRect
        int _frameWidth
        int _numFrames
        float _frameDelay
        float _timeSinceLastFrameChange
};

TextureRect component is useful to set your sprite correctly. To works normally, it needs to get the right informations about the position of your differents things in your sprite's file.

VelocityComponent

class VelocityComponent {
    public:
        VelocityComponent(float dx, float dy)
        float getInitialDX() const
        float getInitialDY() const
        float getDX() const
        float getDY() const
        void setDX(float dx)
        void setDY(float dy)
    private:
        float _initialDx;
        float _initialDy;
        float _dx;
        float _dy;
};

This component is very useful to set the velocity to an entity. It works with de MoveComponent component. To works correctly, it needs to get two mains informations : direction &. speed. Once it's setted, your entity will move depending on params you gave.

Last updated