r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Collision.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "ECS/Component.hpp"
7#include "ECS/Registry.hpp"
9
10namespace gme
11{
12 class CollisionSystem final : public ecs::ASystem
13 {
14 public:
15 explicit CollisionSystem(const std::shared_ptr<eng::IRenderer> &renderer, bool &showDebug)
16 : m_renderer(renderer), m_showDebug(showDebug)
17 {
18 }
19 ~CollisionSystem() override = default;
20
25
26 bool isEnable() override { return true; }
27 void setEnable(bool enable) override { (void)enable; }
28
29 void update(ecs::Registry &registry, float dt) override
30 {
31 std::optional<float> ceilingBottomY;
32 std::optional<float> floorTopY;
33
34 for (auto &pair : registry.getAll<ecs::Ceiling>())
35 {
36 const auto entity = pair.first;
37 const auto *t = registry.getComponent<ecs::Transform>(entity);
38 const auto *s = registry.getComponent<ecs::Scale>(entity);
39 const auto *scroll = registry.getComponent<ecs::Scrolling>(entity);
40 if ((t == nullptr) || (scroll == nullptr))
41 {
42 continue;
43 }
44 const float scaledHeight = (s ? s->y : 1.0f) * scroll->original_height;
45 ceilingBottomY = t->y + scaledHeight;
46 break; // Un seul suffit
47 }
48
49 for (auto &pair : registry.getAll<ecs::Floor>())
50 {
51 const auto entity = pair.first;
52 const auto *t = registry.getComponent<ecs::Transform>(entity);
53 if (t == nullptr)
54 {
55 continue;
56 }
57 floorTopY = t->y;
58 break; // Un seul suffit
59 }
60
61 if (ceilingBottomY.has_value() || floorTopY.has_value())
62 {
63 // Optimisation: itération directe
64 for (auto &pair : registry.getAll<ecs::Player>())
65 {
66 const auto playerEntity = pair.first;
67 auto *t = registry.getComponent<ecs::Transform>(playerEntity);
68 auto *hb = registry.getComponent<ecs::Hitbox>(playerEntity);
69 auto *vel = registry.getComponent<ecs::Velocity>(playerEntity);
70 if (!t || !hb)
71 {
72 continue;
73 }
74
75 float hitboxY = t->y + hb->offsetY;
76
77 if (ceilingBottomY.has_value() && (hitboxY - hb->radius < ceilingBottomY.value()))
78 {
79 t->y = ceilingBottomY.value() + hb->radius - hb->offsetY;
80 if (vel != nullptr)
81 {
82 vel->y = std::max(0.0f, vel->y);
83 }
84 }
85
86 if (floorTopY.has_value() && (hitboxY + hb->radius > floorTopY.value()))
87 {
88 t->y = floorTopY.value() - hb->radius - hb->offsetY;
89 if (vel != nullptr)
90 {
91 vel->y = std::min(0.0f, vel->y);
92 }
93 }
94 }
95 }
96 }
97
98 private:
99 const std::shared_ptr<eng::IRenderer> &m_renderer;
101 };
102} // namespace gme
This file contains the component definitions.
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
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
Server-authoritative collision detection and response system.
CollisionSystem(CollisionSystem &&)=delete
void update(ecs::Registry &registry, float dt) override
Definition Collision.hpp:29
~CollisionSystem() override=default
CollisionSystem(const std::shared_ptr< eng::IRenderer > &renderer, bool &showDebug)
Definition Collision.hpp:15
bool isEnable() override
Definition Collision.hpp:26
const std::shared_ptr< eng::IRenderer > & m_renderer
Definition Collision.hpp:99
void setEnable(bool enable) override
Definition Collision.hpp:27
CollisionSystem & operator=(const CollisionSystem &)=delete
CollisionSystem & operator=(const CollisionSystem &&)=delete
CollisionSystem(const CollisionSystem &)=delete