diff --git a/ECS/HitboxComponent.hpp b/ECS/HitboxComponent.hpp index 073d4d7..1025001 100644 --- a/ECS/HitboxComponent.hpp +++ b/ECS/HitboxComponent.hpp @@ -55,7 +55,7 @@ class HitboxComponent : public Component { Vector2D position{Vector2D(1.0f, 1.0f)}; // Dans l'entitée (0 -> 1) 0=>tout a gauche de l'entitée, 1=>tout a droite et pareil pour le Y Vector2D scale{Vector2D(1.0f, 1.0f)}; // Par rapport à la taille initiale (0 -> 1) - SDL_Rect hitboxR; + SDL_Rect hitboxR {0, 0, 0, 0}; bool hitboxActivated {true}; diff --git a/ECS/PlayerSystem.hpp b/ECS/PlayerSystem.hpp index f9e747f..d90b905 100644 --- a/ECS/PlayerSystem.hpp +++ b/ECS/PlayerSystem.hpp @@ -161,19 +161,7 @@ class PlayerSystem : public Component { if(i == 0) m_transform->velocity.x = 0; if(i == 1) m_transform->velocity.y = 0; - SDL_Rect intersectR; - - if(SDL_IntersectRect(&m_hitbox->hitboxR, &entityHitboxR, &intersectR)) - { - if(intersectR.y > entityHitboxR.y + (entityHitboxR.h >> 1)) - { - m_transform->position.y += static_cast(intersectR.h); - } - else - { - m_transform->position.y -= static_cast(intersectR.h); - } - } + m_resolveCollisions(entityHitboxR); break; } @@ -258,6 +246,25 @@ class PlayerSystem : public Component { } } + void m_resolveCollisions(SDL_Rect &entityHitboxR) + { + SDL_Rect intersectR; + + if (SDL_IntersectRect(&m_hitbox->hitboxR, &entityHitboxR, &intersectR)) + { + float overlapY = static_cast(intersectR.h); + + if(m_transform->position.y + m_transform->dimension.y * m_transform->scale < static_cast(entityHitboxR.y)) + { + m_transform->position.y -= overlapY; + } + else + { + m_transform->position.y += overlapY; + } + } + } + bool m_isInRange(const SDL_Rect &entityR) { SDL_Rect intersectR {entityR};