I'm implementing a basic entity component system from http://entity-systems.wikidot.com/rdbms-with-code-in-systems#java in C++, but I don't quite understand how I could do multiple components (not the same component. I don't see a need to have 2 of the same components yet) in a system. In an inherited System, for example, MovementSystem, I want it to be able to act on all entities that have a position component and a velocity component. However, in my code, I would do
std::vector<Component *> positionComps = entityManager->GetAllComponentsOfType(POSITION_COMPONENT);
std::vector<Component *>::iterator it = components.begin();
for (; it != positionComps; it++) {
PositionComponent *pos = dynamic_cast<PositionComponent *>(it);
pos->x += velo->x;
}
I don't know how I could get the velocity component in there, as it is looping through the position components. I could get all of components of type velocity inside that loop and have a nested loop, but that would be bad on the performance. Also, how would you know which component goes with which? There is a function of GetAllEntitiesPossessingComponent which returns a list of integers so I can later then get the specific component of the entity and check if the entity has the two components. Also, if the component returned was NULL, I would have to check in the system before I do anything if it's NULL or not. But all of this doesn't seem right.
I COULD merge PositionComponent and VelocityComponent into one and then act on that in the system, but that wouldn't help me with future components.