MOVE PLAYER OUT OF OTHER ENTIY !!!!!!!!
This commit is contained in:
		
							parent
							
								
									3917b4c966
								
							
						
					
					
						commit
						d7c3a55068
					
				|  | @ -18,6 +18,7 @@ | ||||||
| #include <vector> | #include <vector> | ||||||
| #include "AnimationSystem.hpp" | #include "AnimationSystem.hpp" | ||||||
| #include "ECS.hpp" | #include "ECS.hpp" | ||||||
|  | #include "HitboxComponent.hpp" | ||||||
| #include "InteractableComponent.hpp" | #include "InteractableComponent.hpp" | ||||||
| #include "SpriteComponent.hpp" | #include "SpriteComponent.hpp" | ||||||
| #include "TransformComponent.hpp" | #include "TransformComponent.hpp" | ||||||
|  | @ -29,7 +30,7 @@ | ||||||
| 
 | 
 | ||||||
| using std::lerp, std::abs, std::vector, std::sort; | using std::lerp, std::abs, std::vector, std::sort; | ||||||
| 
 | 
 | ||||||
| constexpr const float EPSILON = 1e-6f;  // Tolérance
 | constexpr const float EPSILON = 1e-6f; | ||||||
| 
 | 
 | ||||||
| class PlayerSystem : public Component { | class PlayerSystem : public Component { | ||||||
|   public: |   public: | ||||||
|  | @ -55,7 +56,12 @@ class PlayerSystem : public Component { | ||||||
| #ifdef DEBUG_MODE | #ifdef DEBUG_MODE | ||||||
|     void draw() override |     void draw() override | ||||||
|     { |     { | ||||||
|  |         SDL_Rect futureHitboxR = m_hitbox->hitboxR; | ||||||
|  |         futureHitboxR.x += static_cast<int>(m_transform->velocity.x * m_transform->speed); | ||||||
|  |         futureHitboxR.y += static_cast<int>(m_transform->velocity.y * m_transform->speed); | ||||||
|  | 
 | ||||||
|         SDL_SetRenderDrawColor(Game::renderer, 200, 20, 18, 255); |         SDL_SetRenderDrawColor(Game::renderer, 200, 20, 18, 255); | ||||||
|  |         SDL_RenderDrawRect(Game::renderer, &futureHitboxR); | ||||||
| 
 | 
 | ||||||
|         for(auto it {Game::entityManager.getEntities().begin() + 1}; it < Game::entityManager.getEntities().end(); it++) |         for(auto it {Game::entityManager.getEntities().begin() + 1}; it < Game::entityManager.getEntities().end(); it++) | ||||||
|         { |         { | ||||||
|  | @ -80,15 +86,22 @@ class PlayerSystem : public Component { | ||||||
|                 interactableR.w += (m_interactionRange * 2); |                 interactableR.w += (m_interactionRange * 2); | ||||||
|                 interactableR.h += (m_interactionRange * 2); |                 interactableR.h += (m_interactionRange * 2); | ||||||
| 
 | 
 | ||||||
|  |                 SDL_SetRenderDrawColor(Game::renderer, 200, 20, 18, 255); | ||||||
|  | 
 | ||||||
|                 SDL_RenderDrawRect(Game::renderer, &interactableR); |                 SDL_RenderDrawRect(Game::renderer, &interactableR); | ||||||
|             } |             } | ||||||
|  | 
 | ||||||
|  |             if(it->get()->hasComponent<HitboxComponent>()) | ||||||
|  |             { | ||||||
|  |                 SDL_Rect intersectR; | ||||||
|  | 
 | ||||||
|  |                 if(SDL_IntersectRect(&it->get()->getComponent<HitboxComponent>().hitboxR, &m_hitbox->hitboxR, &intersectR)) | ||||||
|  |                 { | ||||||
|  |                     SDL_SetRenderDrawColor(Game::renderer, 20, 20, 200, 255); | ||||||
|  |                     SDL_RenderDrawRect(Game::renderer, &intersectR); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|         SDL_Rect futureHitboxR = m_hitbox->hitboxR; |  | ||||||
|         futureHitboxR.x += static_cast<int>(m_transform->velocity.x * static_cast<float>(m_transform->speed)); |  | ||||||
|         futureHitboxR.y += static_cast<int>(m_transform->velocity.y * static_cast<float>(m_transform->speed)); |  | ||||||
| 
 |  | ||||||
|         SDL_RenderDrawRect(Game::renderer, &futureHitboxR); |  | ||||||
| 
 | 
 | ||||||
|         SDL_SetRenderDrawColor(Game::renderer, 20, 20, 18, 255); |         SDL_SetRenderDrawColor(Game::renderer, 20, 20, 18, 255); | ||||||
|     } |     } | ||||||
|  | @ -106,8 +119,8 @@ class PlayerSystem : public Component { | ||||||
|         for(int i {0}; i < 2; i++) |         for(int i {0}; i < 2; i++) | ||||||
|         { |         { | ||||||
|             SDL_Rect futureHitboxR = m_hitbox->hitboxR; |             SDL_Rect futureHitboxR = m_hitbox->hitboxR; | ||||||
|             if(i == 0) futureHitboxR.x += static_cast<int>(m_transform->velocity.x * static_cast<float>(m_transform->speed)); |             if(i == 0) futureHitboxR.x += static_cast<int>(m_transform->velocity.x * m_transform->speed); | ||||||
|             if(i == 1) futureHitboxR.y += static_cast<int>(m_transform->velocity.y * static_cast<float>(m_transform->speed)); |             if(i == 1) futureHitboxR.y += static_cast<int>(m_transform->velocity.y * m_transform->speed); | ||||||
| 
 | 
 | ||||||
|             SDL_Rect hitboxTilesR; |             SDL_Rect hitboxTilesR; | ||||||
|             hitboxTilesR.x = futureHitboxR.x / (TILE_SIZE * TILEMAP_SCALE); |             hitboxTilesR.x = futureHitboxR.x / (TILE_SIZE * TILEMAP_SCALE); | ||||||
|  | @ -115,7 +128,7 @@ class PlayerSystem : public Component { | ||||||
|             hitboxTilesR.w = (futureHitboxR.w + futureHitboxR.x) / (TILE_SIZE * TILEMAP_SCALE); |             hitboxTilesR.w = (futureHitboxR.w + futureHitboxR.x) / (TILE_SIZE * TILEMAP_SCALE); | ||||||
|             hitboxTilesR.h = (futureHitboxR.h + futureHitboxR.y) / (TILE_SIZE * TILEMAP_SCALE); |             hitboxTilesR.h = (futureHitboxR.h + futureHitboxR.y) / (TILE_SIZE * TILEMAP_SCALE); | ||||||
| 
 | 
 | ||||||
|             vector<int> touchedHitboxes {}; |             vector<int> touchedHitboxes; | ||||||
| 
 | 
 | ||||||
|             for(int tileY = hitboxTilesR.y; tileY <= hitboxTilesR.h; tileY++) |             for(int tileY = hitboxTilesR.y; tileY <= hitboxTilesR.h; tileY++) | ||||||
|             { |             { | ||||||
|  | @ -128,27 +141,29 @@ class PlayerSystem : public Component { | ||||||
|             sort(touchedHitboxes.begin(), touchedHitboxes.end()); |             sort(touchedHitboxes.begin(), touchedHitboxes.end()); | ||||||
| 
 | 
 | ||||||
|             for(const auto &touchedHitbox : touchedHitboxes) |             for(const auto &touchedHitbox : touchedHitboxes) | ||||||
|  |             { | ||||||
|  |                 if(touchedHitbox != 0) | ||||||
|                 { |                 { | ||||||
|                     if(touchedHitbox == 1) |                     if(touchedHitbox == 1) | ||||||
|                     { |                     { | ||||||
|                         if(i == 0) m_transform->velocity.x = 0; |                         if(i == 0) m_transform->velocity.x = 0; | ||||||
|                         if(i == 1) m_transform->velocity.y = 0; |                         if(i == 1) m_transform->velocity.y = 0; | ||||||
|  | 
 | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                 else if(touchedHitbox != 0) |                     else | ||||||
|                     { |                     { | ||||||
|                         Game::tileMap.LoadNextMap(touchedHitbox); |                         Game::tileMap.LoadNextMap(touchedHitbox); | ||||||
|                         m_transform->position = Game::tileMap.getPlayerInitPos(); |                         m_transform->position = Game::tileMap.getPlayerInitPos(); | ||||||
| 
 | 
 | ||||||
|                     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))); |  | ||||||
| 
 |  | ||||||
|                         Game::camera.centerPos.x = static_cast<int>(m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 1)); |                         Game::camera.centerPos.x = static_cast<int>(m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 1)); | ||||||
|                         Game::camera.centerPos.y = static_cast<int>(m_transform->position.y + static_cast<float>((m_transform->dimension.y * m_transform->scale) >> 1)); |                         Game::camera.centerPos.y = static_cast<int>(m_transform->position.y + static_cast<float>((m_transform->dimension.y * m_transform->scale) >> 1)); | ||||||
|  | 
 | ||||||
|                         playerState = State::None; |                         playerState = State::None; | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             for(auto it {Game::entityManager.getEntities().begin() + 1}; it < Game::entityManager.getEntities().end(); it++) |             for(auto it {Game::entityManager.getEntities().begin() + 1}; it < Game::entityManager.getEntities().end(); it++) | ||||||
|             { |             { | ||||||
|  | @ -156,7 +171,47 @@ class PlayerSystem : public Component { | ||||||
|                 { |                 { | ||||||
|                     if(it->get()->getComponent<HitboxComponent>().hitboxActivated) |                     if(it->get()->getComponent<HitboxComponent>().hitboxActivated) | ||||||
|                     { |                     { | ||||||
|                         SDL_Rect entityHitboxR {it->get()->getComponent<HitboxComponent>().hitboxR}; |                         SDL_Rect &entityHitboxR {it->get()->getComponent<HitboxComponent>().hitboxR}; | ||||||
|  | 
 | ||||||
|  |                         SDL_Rect intersectR; | ||||||
|  | 
 | ||||||
|  |                         if(SDL_IntersectRect(&m_hitbox->hitboxR, &entityHitboxR, &intersectR)) | ||||||
|  |                         { | ||||||
|  |                             Vector2D<int> overlap; | ||||||
|  |                             overlap.x = intersectR.w; | ||||||
|  |                             overlap.y = intersectR.h; | ||||||
|  | 
 | ||||||
|  |                             if(overlap.x < overlap.y) | ||||||
|  |                             { | ||||||
|  |                                 int leftDistance = abs(entityHitboxR.x + entityHitboxR.w / 2 - m_hitbox->hitboxR.x); | ||||||
|  |                                 int rightDistance = abs(entityHitboxR.x + entityHitboxR.w / 2 - (m_hitbox->hitboxR.x + m_hitbox->hitboxR.w)); | ||||||
|  | 
 | ||||||
|  |                                 if(leftDistance > rightDistance) | ||||||
|  |                                 { | ||||||
|  |                                     m_transform->position.x -= m_transform->speed; | ||||||
|  |                                 } | ||||||
|  |                                 else | ||||||
|  |                                 { | ||||||
|  |                                     m_transform->position.x += m_transform->speed; | ||||||
|  |                                 } | ||||||
|  |                             } | ||||||
|  |                             else | ||||||
|  |                             { | ||||||
|  |                                 int upDistance = abs(entityHitboxR.y + entityHitboxR.h / 2 - m_hitbox->hitboxR.y); | ||||||
|  |                                 int downDistance = abs(entityHitboxR.y + entityHitboxR.h / 2 - (m_hitbox->hitboxR.y + m_hitbox->hitboxR.h)); | ||||||
|  | 
 | ||||||
|  |                                 if(upDistance > downDistance) | ||||||
|  |                                 { | ||||||
|  |                                     m_transform->position.y -= m_transform->speed; | ||||||
|  |                                 } | ||||||
|  |                                 else | ||||||
|  |                                 { | ||||||
|  |                                     m_transform->position.y += m_transform->speed; | ||||||
|  |                                 } | ||||||
|  |                             } | ||||||
|  | 
 | ||||||
|  |                             break; | ||||||
|  |                         } | ||||||
| 
 | 
 | ||||||
|                         if(SDL_HasIntersection(&futureHitboxR, &entityHitboxR)) |                         if(SDL_HasIntersection(&futureHitboxR, &entityHitboxR)) | ||||||
|                         { |                         { | ||||||
|  | @ -208,8 +263,6 @@ class PlayerSystem : public Component { | ||||||
| 
 | 
 | ||||||
|         target.x = m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 2); |         target.x = m_transform->position.x + static_cast<float>((m_transform->dimension.x * m_transform->scale) >> 2); | ||||||
|         target.y = m_transform->position.y + static_cast<float>((m_transform->dimension.y * m_transform->scale) >> 2); |         target.y = m_transform->position.y + static_cast<float>((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}; |         float smoothingFactor {0.08f}; | ||||||
| 
 | 
 | ||||||
|  | @ -217,8 +270,6 @@ class PlayerSystem : public Component { | ||||||
| 
 | 
 | ||||||
|         Game::camera.centerPos.x = static_cast<int>(lerp(Game::camera.centerPos.x, target.x, smoothingFactor)); |         Game::camera.centerPos.x = static_cast<int>(lerp(Game::camera.centerPos.x, target.x, smoothingFactor)); | ||||||
|         Game::camera.centerPos.y = static_cast<int>(lerp(Game::camera.centerPos.y, target.y, smoothingFactor)); |         Game::camera.centerPos.y = static_cast<int>(lerp(Game::camera.centerPos.y, target.y, smoothingFactor)); | ||||||
|         Game::camera.camR.x = static_cast<int>(lerp(Game::camera.camR.x, targetX, smoothingFactor)); |  | ||||||
|         Game::camera.camR.y = static_cast<int>(lerp(Game::camera.camR.y, targetY, smoothingFactor)); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void m_checkInteractions() |     void m_checkInteractions() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue