Create a new system
Step by step to create 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;
}
}
};
}
}Last updated