R-Type 0.1
Loading...
Searching...
No Matches
ParallaxSystem.hpp
1#pragma once
2
3#include "Logger/Logger.hpp"
4#include "components/Parallax.hpp"
5#include "components/Transform.hpp"
6#include <ISystem.hpp>
7
8namespace rtk::systems {
17class ParallaxSystem final : public rtk::ecs::ISystem {
18public:
19 ParallaxSystem() = default;
20 ~ParallaxSystem() override = default;
21
25 void onStart(rtk::ecs::Registry &) override {
26 LOG_INFO("Load Parallax System");
27 }
28
35 void update(rtk::ecs::Registry &reg, float dt) override {
36 auto view = reg.view<component::Transform, component::Parallax>();
37
38 auto &transforms = reg.get_components<component::Transform>();
39 auto &parallaxes = reg.get_components<component::Parallax>();
40
41 for (auto entity : view) {
42 auto *transform = transforms.get(entity);
43 auto *parallax = parallaxes.get(entity);
44
45 if (!transform || !parallax)
46 continue;
47
48 transform->position.x -= parallax->speedX * dt;
49
50 if (transform->position.x <= -parallax->loopWidth) {
51 transform->position.x += parallax->loopWidth * 3.0f;
52 }
53 }
54 }
55
59 void onStop(rtk::ecs::Registry &) override {}
60};
61
63} // namespace rtk::systems
Scrolls background layers and wraps them to loop endlessly.
Definition ParallaxSystem.hpp:17
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition ParallaxSystem.hpp:59
void onStart(rtk::ecs::Registry &) override
Logs that the system is loaded.
Definition ParallaxSystem.hpp:25
void update(rtk::ecs::Registry &reg, float dt) override
Moves each layer left and jumps it three tiles right once off screen.
Definition ParallaxSystem.hpp:35
Background layer that scrolls left and wraps around.
Definition Parallax.hpp:16
Position, rotation and scale of an entity in world space.
Definition Transform.hpp:22
utils::vec2 position
Top left corner, in pixels.
Definition Transform.hpp:24
float x
Horizontal component.
Definition vec2.hpp:13