System are the logic of component and entities.
They use component data to process and do something like modify component data or send signal to a server.
Components are store in the ECS/Systems with one component per file, in the namespace ECS/systems.
Here an example of a system:
namespace ECS {
namespace systems {
class ParallaxSystem {
public:
void update(Registry &ecs, float deltaTime) {
try {
auto ¶llaxComponents = ecs.get_components<components::ParallaxComponent>();
auto &positionComponents = ecs.get_components<components::PositionComponent>();
for (size_t i = 0; i < parallaxComponents.size() && i < positionComponents.size(); ++i) {
auto ¶llax = parallaxComponents[i];
auto &position = positionComponents[i];
if (parallax && position) {
float scrollSpeed = parallax->getScrollSpeed();
float newX = position->getX() + deltaTime * scrollSpeed;
float backgroundWidth = parallax->getBackgroundWidth() / 2;
if (newX < -backgroundWidth)
newX += backgroundWidth;
position->setX(newX);
}
}
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
}
}
};
}
}
It is compose of an update() function with argument:
void update(Registry &ecs, float deltaTime);
System class get all entity's components from ecs with ecs.get_components<components>(arguments). In the example, we get two components lists:
auto ¶llaxComponents = ecs.get_components<components::ParallaxComponent>();
auto &positionComponents = ecs.get_components<components::PositionComponent>();
It use to get the list of a specific component, and process all of them directly with a loop:
for (size_t i = 0; i < parallaxComponents.size() && i < positionComponents.size(); ++i) {
auto ¶llax = parallaxComponents[i];
auto &position = positionComponents[i];
if (parallax && position) {
float scrollSpeed = parallax->getScrollSpeed();
float newX = position->getX() + deltaTime * scrollSpeed;
float backgroundWidth = parallax->getBackgroundWidth() / 2;
if (newX < -backgroundWidth)
newX += backgroundWidth;
position->setX(newX);
}
}
On this example, we loop on every parallaxComponents and positionComponent, and process them.
Here, we move automatically the parallax in the X direction using parallax and position data in each component.