Create a new component

Step by step to create a component

Every entities are link to one or many components. The components are the attributes of entities. A component don't have a logic, it is use to store data only.

Components are store in the ECS/Components with one component per file, in the namespace ECS/components.

Here an example of a component:

namespace ECS {
    namespace components {
        class ParallaxComponent {
            public:
                ParallaxComponent(float scrollSpeed, float backgroundWidth): _scrollSpeed(scrollSpeed), _backgroundWidth(backgroundWidth) {}
                float getScrollSpeed() const { return _scrollSpeed; }
                float getBackgroundWidth() const { return _backgroundWidth; }
                
                void setScrollSpeed(const float &scrollSpeed);
                void setBackgroundWidth(const float &backgroundWidth);

            private:
                float _scrollSpeed;
                float _backgroundWidth;
        };
    }
}

It is compose of a constructor with data in argument:

ParallaxComponent(float scrollSpeed, float backgroundWidth): _scrollSpeed(scrollSpeed), _backgroundWidth(backgroundWidth) {}

Variable are store in private, and we can access or modify with getter and setter only:

float getScrollSpeed() const { return _scrollSpeed; } 
float getBackgroundWidth() const { return _backgroundWidth; }
void setScrollSpeed(const float &scrollSpeed);
void setBackgroundWidth(const float &backgroundWidth);

Last updated