AHORA SI: Colisiones finalizadas

This commit is contained in:
2022-09-09 14:01:49 +02:00
parent 00b47a5910
commit 36fc848779
3 changed files with 10 additions and 41 deletions

View File

@@ -238,16 +238,10 @@ void Player::move()
{
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y;
proj.h = h;
proj.w = (int)vx;
// **new
proj.x = (int)(x + vx);
proj.y = (int)y;
proj.h = h - 1;
proj.w = (int)abs(vx);
proj.w = ceil(abs(vx)); // Para evitar que tenga un ancho de 0 pixels
// Comprueba la colisión
const int pos = room->checkRightSurfaces(&proj);
@@ -271,7 +265,7 @@ void Player::move()
proj.x = (int)x + w;
proj.y = (int)y;
proj.h = h - 1;
proj.w = (int)(vx);
proj.w = ceil(vx); // Para evitar que tenga un ancho de 0 pixels
// Comprueba la colisión
const int pos = room->checkLeftSurfaces(&proj);
@@ -299,14 +293,8 @@ void Player::move()
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y;
proj.h = (int)vy;
proj.w = w;
// **new
proj.x = (int)x;
proj.y = (int)(y + vy);
proj.h = (int)abs(vy);
proj.h = ceil(abs(vy)); // Para evitar que tenga una altura de 0 pixels
proj.w = w - 1;
// Comprueba la colisión
@@ -331,7 +319,7 @@ void Player::move()
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y + h;
proj.h = (int)vy;
proj.h = ceil(vy); // Para evitar que tenga una altura de 0 pixels
proj.w = w - 1;
// Comprueba la colisión

View File

@@ -107,13 +107,11 @@ bool Room::load(std::string _file_path)
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
else if (line == "[tilemap]")
{
// printf("Loading tilemap...\n");
do
{
std::getline(file, line);
if (line.find(".tmx") != std::string::npos)
{
// printf("Reading file %s\n", asset->get(line).c_str());
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
if (file2.good())
{
@@ -126,11 +124,7 @@ bool Room::load(std::string _file_path)
do
{
std::getline(file2, line);
// printf("parsing: %s\n", line.c_str());
pos = line.find("data encoding");
// printf("pos: %i\n", pos);
} while (pos == std::string::npos);
do
@@ -139,12 +133,10 @@ bool Room::load(std::string _file_path)
std::getline(file2, line);
if (line != "</data>")
{
// printf("data: %s\n", line.c_str());
std::stringstream ss(line);
std::string tmp;
while (getline(ss, tmp, ','))
{
// printf("tile: %s\n", tmp.c_str());
tilemap.push_back(std::stoi(tmp));
}
}
@@ -444,7 +436,7 @@ void Room::fillMapTexture()
{
clip.x = x * 8;
clip.y = y * 8;
SDL_SetRenderDrawColor(renderer, 48, 48, 48, 224);
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 224);
SDL_RenderFillRect(renderer, &clip);
}
}
@@ -702,25 +694,21 @@ int Room::getSlopeHeight(SDL_Point p, tile_e slope)
{
// Calcula la base del tile
int base = ((p.y / tileSize) * tileSize) + tileSize;
printf("base %i\n", base);
debug->add("BASE = " + std::to_string(base));
// Calcula cuanto se ha entrado en el tile horizontalmente
const int pos = (p.x % tileSize); // esto da un valor entre 0 y 7
printf("pos %i\n", base);
debug->add("POS = " + std::to_string(pos));
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa
if (slope == t_slope_r)
{
base -= pos + 1;
printf("base_R %i\n", base);
debug->add("BASE_R = " + std::to_string(base));
}
else
{
base -= (tileSize - pos);
printf("base_L %i\n", base);
debug->add("BASE_L = " + std::to_string(base));
}
@@ -876,6 +864,7 @@ void Room::setRightSurfaces()
}
line.y2 = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
rightSurfaces.push_back(line);
i++;
}
}

View File

@@ -152,13 +152,13 @@ bool checkCollision(h_line_t &l, SDL_Rect &r)
}
// Comprueba si la linea esta por debajo del rectangulo
if (l.y > r.y + r.h)
if (l.y >= r.y + r.h)
{
return false;
}
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
if (l.x1 > r.x + r.w)
if (l.x1 >= r.x + r.w)
{
return false;
}
@@ -178,27 +178,19 @@ bool checkCollision(v_line_t &l, SDL_Rect &r)
{
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x)
{
return false;
}
// Comprueba si la linea esta por la derecha del rectangulo
if (l.x > r.x + r.w)
{
if (l.x >= r.x + r.w)
return false;
}
// Comprueba si el inicio de la linea esta debajo del rectangulo
if (l.y1 > r.y + r.h)
{
if (l.y1 >= r.y + r.h)
return false;
}
// Comprueba si el final de la linea esta encima del rectangulo
if (l.y2 < r.y)
{
return false;
}
// Si ha llegado hasta aquí, hay colisión
return true;