Create a function for resolve hitboxes.

This commit is contained in:
Ulysse Cura 2024-09-08 13:27:35 +02:00
parent 947905c39a
commit 90ceb9a86f
2 changed files with 21 additions and 14 deletions

View File

@ -55,7 +55,7 @@ class HitboxComponent : public Component {
Vector2D<float> position{Vector2D<float>(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<float> scale{Vector2D<float>(1.0f, 1.0f)}; // Par rapport à la taille initiale (0 -> 1)
SDL_Rect hitboxR;
SDL_Rect hitboxR {0, 0, 0, 0};
bool hitboxActivated {true};

View File

@ -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<float>(intersectR.h);
}
else
{
m_transform->position.y -= static_cast<float>(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<float>(intersectR.h);
if(m_transform->position.y + m_transform->dimension.y * m_transform->scale < static_cast<float>(entityHitboxR.y))
{
m_transform->position.y -= overlapY;
}
else
{
m_transform->position.y += overlapY;
}
}
}
bool m_isInRange(const SDL_Rect &entityR)
{
SDL_Rect intersectR {entityR};