From e9975c4abea73751f3fe72727825b906fc11acdd Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sat, 7 Sep 2024 17:24:44 +0200 Subject: [PATCH 1/3] Move player out of hitboxes --- ECS/PlayerSystem.hpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/ECS/PlayerSystem.hpp b/ECS/PlayerSystem.hpp index 7d41c88..f9e747f 100644 --- a/ECS/PlayerSystem.hpp +++ b/ECS/PlayerSystem.hpp @@ -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(m_transform->position.x - static_cast(Game::camera.camR.w / 2 - m_transform->dimension.x * m_transform->scale / 2)); - Game::camera.camR.y = static_cast(m_transform->position.y - static_cast(Game::camera.camR.h / 2 - m_transform->dimension.y * m_transform->scale / 2)); + Game::camera.camR.x = static_cast(m_transform->position.x - static_cast((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) / 2))); + Game::camera.camR.y = static_cast(m_transform->position.y - static_cast((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().hitboxActivated) { - if(SDL_HasIntersection(&futureHitboxR, &it->get()->getComponent().hitboxR)) + SDL_Rect entityHitboxR {it->get()->getComponent().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(intersectR.h); + } + else + { + m_transform->position.y -= static_cast(intersectR.h); + } + } + break; } } @@ -199,8 +216,8 @@ class PlayerSystem : public Component { void m_setCamera() { - float targetX = m_transform->position.x - static_cast(Game::camera.camR.w / 2 - m_transform->dimension.x * m_transform->scale / 2); - float targetY = m_transform->position.y - static_cast(Game::camera.camR.h / 2 - m_transform->dimension.y * m_transform->scale / 2); + float targetX = m_transform->position.x - static_cast((Game::camera.camR.w >> 1) - ((m_transform->dimension.x * m_transform->scale) >> 2)); + float targetY = m_transform->position.y - static_cast((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 From 90ceb9a86fa194384b3e4c94981a21561a7e07b0 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sun, 8 Sep 2024 13:27:35 +0200 Subject: [PATCH 2/3] Create a function for resolve hitboxes. --- ECS/HitboxComponent.hpp | 2 +- ECS/PlayerSystem.hpp | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) 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}; From 8f39b26b9d3591dfbd38ea1d5cf48d5fe4b1acf4 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sun, 8 Sep 2024 14:05:19 +0200 Subject: [PATCH 3/3] Undo hitbox gestion --- .gitignore | 34 ++-------------------------------- ECS/PlayerSystem.hpp | 21 --------------------- 2 files changed, 2 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index e257658..2f0cc37 100644 --- a/.gitignore +++ b/.gitignore @@ -1,34 +1,4 @@ # ---> C++ -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app +# Executable +2D_Engine \ No newline at end of file diff --git a/ECS/PlayerSystem.hpp b/ECS/PlayerSystem.hpp index d90b905..f3f679d 100644 --- a/ECS/PlayerSystem.hpp +++ b/ECS/PlayerSystem.hpp @@ -161,8 +161,6 @@ class PlayerSystem : public Component { if(i == 0) m_transform->velocity.x = 0; if(i == 1) m_transform->velocity.y = 0; - m_resolveCollisions(entityHitboxR); - break; } } @@ -246,25 +244,6 @@ 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};