MOVE PLAYER OUT OF OTHER ENTIY !!!!!!!!

This commit is contained in:
Ulysse Cura 2024-10-17 19:11:23 +02:00
parent 3917b4c966
commit d7c3a55068
1 changed files with 79 additions and 28 deletions

View File

@ -18,6 +18,7 @@
#include <vector>
#include "AnimationSystem.hpp"
#include "ECS.hpp"
#include "HitboxComponent.hpp"
#include "InteractableComponent.hpp"
#include "SpriteComponent.hpp"
#include "TransformComponent.hpp"
@ -29,7 +30,7 @@
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 {
public:
@ -55,7 +56,12 @@ class PlayerSystem : public Component {
#ifdef DEBUG_MODE
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_RenderDrawRect(Game::renderer, &futureHitboxR);
for(auto it {Game::entityManager.getEntities().begin() + 1}; it < Game::entityManager.getEntities().end(); it++)
{
@ -80,16 +86,23 @@ class PlayerSystem : public Component {
interactableR.w += (m_interactionRange * 2);
interactableR.h += (m_interactionRange * 2);
SDL_SetRenderDrawColor(Game::renderer, 200, 20, 18, 255);
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);
}
#endif // DEBUG_MODE
@ -106,8 +119,8 @@ class PlayerSystem : public Component {
for(int i {0}; i < 2; i++)
{
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 == 1) futureHitboxR.y += static_cast<int>(m_transform->velocity.y * 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 * m_transform->speed);
SDL_Rect hitboxTilesR;
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.h = (futureHitboxR.h + futureHitboxR.y) / (TILE_SIZE * TILEMAP_SCALE);
vector<int> touchedHitboxes {};
vector<int> touchedHitboxes;
for(int tileY = hitboxTilesR.y; tileY <= hitboxTilesR.h; tileY++)
{
@ -129,24 +142,26 @@ class PlayerSystem : public Component {
for(const auto &touchedHitbox : touchedHitboxes)
{
if(touchedHitbox == 1)
if(touchedHitbox != 0)
{
if(i == 0) m_transform->velocity.x = 0;
if(i == 1) m_transform->velocity.y = 0;
break;
}
else if(touchedHitbox != 0)
{
Game::tileMap.LoadNextMap(touchedHitbox);
m_transform->position = Game::tileMap.getPlayerInitPos();
if(touchedHitbox == 1)
{
if(i == 0) m_transform->velocity.x = 0;
if(i == 1) m_transform->velocity.y = 0;
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)));
break;
}
else
{
Game::tileMap.LoadNextMap(touchedHitbox);
m_transform->position = Game::tileMap.getPlayerInitPos();
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));
playerState = State::None;
break;
playerState = State::None;
break;
}
}
}
@ -156,7 +171,47 @@ class PlayerSystem : public Component {
{
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))
{
@ -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.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};
@ -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.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()