Move player out of hitboxes

This commit is contained in:
Ulysse Cura 2024-09-07 17:24:44 +02:00
parent 3fcfeecd43
commit e9975c4abe
1 changed files with 24 additions and 7 deletions

View File

@ -92,7 +92,7 @@ class PlayerSystem : public Component {
SDL_SetRenderDrawColor(Game::renderer, 20, 20, 18, 255);
}
#endif
#endif // DEBUG_MODE
private:
void m_getInputs()
@ -140,8 +140,8 @@ class PlayerSystem : public Component {
Game::tileMap.LoadNextMap(touchedHitbox);
m_transform->position = Game::tileMap.getPlayerInitPos();
Game::camera.camR.x = static_cast<int>(m_transform->position.x - static_cast<float>(Game::camera.camR.w / 2 - m_transform->dimension.x * m_transform->scale / 2));
Game::camera.camR.y = static_cast<int>(m_transform->position.y - static_cast<float>(Game::camera.camR.h / 2 - m_transform->dimension.y * m_transform->scale / 2));
Game::camera.camR.x = static_cast<int>(m_transform->position.x - static_cast<float>((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) / 2)));
Game::camera.camR.y = static_cast<int>(m_transform->position.y - static_cast<float>((Game::camera.camR.h >> 1) - ((m_transform->dimension.y * m_transform->scale) / 2)));
playerState = State::None;
break;
@ -154,10 +154,27 @@ class PlayerSystem : public Component {
{
if(it->get()->getComponent<HitboxComponent>().hitboxActivated)
{
if(SDL_HasIntersection(&futureHitboxR, &it->get()->getComponent<HitboxComponent>().hitboxR))
SDL_Rect entityHitboxR {it->get()->getComponent<HitboxComponent>().hitboxR};
if(SDL_HasIntersection(&futureHitboxR, &entityHitboxR))
{
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);
}
}
break;
}
}
@ -199,8 +216,8 @@ class PlayerSystem : public Component {
void m_setCamera()
{
float targetX = m_transform->position.x - static_cast<float>(Game::camera.camR.w / 2 - m_transform->dimension.x * m_transform->scale / 2);
float targetY = m_transform->position.y - static_cast<float>(Game::camera.camR.h / 2 - m_transform->dimension.y * m_transform->scale / 2);
float targetX = m_transform->position.x - static_cast<float>((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) >> 2));
float targetY = m_transform->position.y - static_cast<float>((Game::camera.camR.h >> 1) - ((m_transform->dimension.y * m_transform->scale) >> 2));
float smoothingFactor {0.08f};
@ -274,4 +291,4 @@ class PlayerSystem : public Component {
State playerState {State::None};
};
#endif
#endif // PLAYER_HPP