Creating a transform component
With the ability to attach and return components, let's get our first component built and added. We'll start with a simple one first. Currently, all objects have a position by default that's provided by the Object
base class. Let's break this behavior into its own component.
Encapsulating transform behavior
Since we're converting an inheritance-based approach to a component-based one, the first task is to take the transform behavior out of the Object
class. Currently, that consists of a single
position variable and a function to both get
and set
that value.
Let's create a new class named TransformComponent
and move this behavior into it, as follows:
#ifndef TRANSFORMCOMPONENT_H #define TRANSFORMCOMPONENT_H #include "Component.h" class TransformComponent : public Component { public: TransformComponent(); void SetPosition(sf::Vector2f position); sf::Vector2f& GetPosition(); private: sf::Vector2f m_position; }; #endif
We'll also take the function...