r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Scrolling.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ECS/Component.hpp"
5#include "ECS/Registry.hpp"
8
9namespace gme
10{
11 class ScrollingSystem final : public ecs::ASystem
12 {
13 public:
14 explicit ScrollingSystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
15 ~ScrollingSystem() override = default;
16
21
22 void update(ecs::Registry &registry, float dt) override
23 {
24 const auto [width, height] = m_renderer->getWindowSize();
25
26 for (auto &[entity, scrolling] : registry.getAll<ecs::Scrolling>())
27 {
28 auto *transform = registry.getComponent<ecs::Transform>(entity);
29 auto *scale = registry.getComponent<ecs::Scale>(entity);
30 const bool isFloor = registry.hasComponent<ecs::Floor>(entity);
31 const bool isCeiling = registry.hasComponent<ecs::Ceiling>(entity);
32
33 if (transform == nullptr)
34 {
35 continue;
36 }
37
38 if (scrolling.fit_width && (scale != nullptr))
39 {
40 const auto targetWidth = static_cast<float>(width);
41 const float scaleX = targetWidth / std::max(1.0f, scrolling.original_width);
42 scale->x = scaleX;
43 scale->y = scaleX;
44 }
45
46 const float scaledHeight = ((scale != nullptr) ? scale->y : 1.0f) * scrolling.original_height;
47 if (isCeiling)
48 {
50 }
51 else if (isFloor)
52 {
53 transform->y =
54 static_cast<float>(height) - scaledHeight - utl::GameConfig::Stage::FLOOR_OFFSET_Y;
55 }
56
57 transform->x += scrolling.speed_x * dt;
58
59 if (const float scaledWidth = ((scale != nullptr) ? scale->x : 1.0f) * scrolling.original_width;
60 transform->x + scaledWidth < 0.0f)
61 {
62 transform->x += scaledWidth + static_cast<float>(width);
63 }
64 }
65 }
66
67 private:
68 const std::shared_ptr<eng::IRenderer> &m_renderer;
69 };
70} // namespace gme
This file contains the component definitions.
Configuration constants for the multiplayer game.
This file contains the IRenderer class declaration.
This file contains the interface for systems.
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
Class for managing entities and their components.
Definition Registry.hpp:25
bool hasComponent(Entity e)
Definition Registry.hpp:79
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
virtual WindowSize getWindowSize()=0
void update(ecs::Registry &registry, float dt) override
Definition Scrolling.hpp:22
~ScrollingSystem() override=default
ScrollingSystem(const ScrollingSystem &)=delete
ScrollingSystem(ScrollingSystem &&)=delete
ScrollingSystem(const std::shared_ptr< eng::IRenderer > &renderer)
Definition Scrolling.hpp:14
ScrollingSystem & operator=(const ScrollingSystem &&)=delete
ScrollingSystem & operator=(const ScrollingSystem &)=delete
const std::shared_ptr< eng::IRenderer > & m_renderer
Definition Scrolling.hpp:68
constexpr float FLOOR_OFFSET_Y
constexpr float CEILING_OFFSET_Y