Component
systems
Since my blog on OOP vs. DDP I’ve wanted to write a blog
about implementing a component system. Just to refresh component systems are
different from objects because they are more worried about the data involved. A
component system streamlines data and processes.
In a component system there is the entity manager, generally
at the top. In order to do most things you will have to talk to it. The reason
for this is because it lets other related systems know what is happening. The
related systems need to know because there are some operations they will need
to do to keep things going smoothly.
The entity manager will know how to create an entity and
broadcast its creation to the component systems. Also, the entity manager knows
about all the entities and all the component systems. The entity manager will know how to register a
component system to its list, how to add a component to an entity and any other
implementation required.
A component system works off of a base class. The base class
can hold the base interactions the entity manager will need. The base
operations are things like a pure virtual function that is called to let the
system know that its component type has been added to an entity. The base
system will also need a string name to tag it and a list of the entities that
have it.
The second level to a component system is where all component
specific processes are held. The component system is responsible for processing
its component type. It will have functions like “ProcessAll”. The “ProcessAll”
function is where all the entities with the component the system cares about
will be processed.
A component is kind of similar to a component system because
it too inherits from a base class. This base class can be very small. In fact,
in the base implementation it only needs one thing. The only thing it needs is
a pure virtual function. The function will return the type of the component.
The second level to a component is where all the component
specific data is held. For example a Renderer component might look like this:
class Renderer : public Component
{
struct SpriteQuad{//quad
data };
public:
std::string
GetType() { return "Renderer"; }
SpriteQuad
m_quad;
};
Last thing is the entity. The entity
in its perfect form will be just an id but for this simple implementation it
will be an id and a list of its attached components. The entity will also have
a private constructor. The constructor is private because the only thing that
should be able to construct an entity is the entity manager. This is so that
any component systems need to know about the entities creation the entity
manager has already let them know.
That is the basics for a component
system. I would love to develop one and might actually implement one in a
project. As far as my GDW game it’s too far down the production chain to change
it now.
No comments:
Post a Comment