This commit is contained in:
2025-10-27 17:39:23 +01:00
parent aacb14149f
commit b1dca32a5b
18 changed files with 774 additions and 565 deletions

View File

@@ -3,6 +3,7 @@
#include <algorithm> // Para max, min
#include <cmath> // Para ceil, abs
#include <ranges> // Para std::ranges::any_of
#include "core/input/input.hpp" // Para Input, InputAction
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
@@ -213,6 +214,167 @@ void Player::applyGravity() {
}
}
// Maneja el movimiento horizontal hacia la izquierda
void Player::moveHorizontalLeft() {
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_FRect proj;
proj.x = static_cast<int>(x_ + vx_);
proj.y = static_cast<int>(y_);
proj.h = HEIGHT;
proj.w = static_cast<int>(std::ceil(std::fabs(vx_))); // Para evitar que tenga un ancho de 0 pixels
#ifdef _DEBUG
debug_rect_x_ = proj;
#endif
// Comprueba la colisión con las superficies
const int POS = room_->checkRightSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
x_ += vx_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona
x_ = POS + 1;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
if (LY > -1) {
y_ = LY - HEIGHT;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
y_ += 1;
}
}
// Maneja el movimiento horizontal hacia la derecha
void Player::moveHorizontalRight() {
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_FRect proj;
proj.x = x_ + WIDTH;
proj.y = y_;
proj.h = HEIGHT;
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
#ifdef _DEBUG
debug_rect_x_ = proj;
#endif
// Comprueba la colisión
const int POS = room_->checkLeftSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
x_ += vx_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona
x_ = POS - WIDTH;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
if (RY > -1) {
y_ = RY - HEIGHT;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
y_ += 1;
}
}
// Maneja el movimiento vertical hacia arriba
void Player::moveVerticalUp() {
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_FRect proj;
proj.x = static_cast<int>(x_);
proj.y = static_cast<int>(y_ + vy_);
proj.h = static_cast<int>(std::ceil(std::fabs(vy_))); // Para evitar que tenga una altura de 0 pixels
proj.w = WIDTH;
#ifdef _DEBUG
debug_rect_y_ = proj;
#endif
// Comprueba la colisión
const int POS = room_->checkBottomSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
y_ += vy_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
y_ = POS + 1;
setState(PlayerState::FALLING);
}
}
// Maneja el movimiento vertical hacia abajo
void Player::moveVerticalDown() {
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_FRect proj;
proj.x = x_;
proj.y = y_ + HEIGHT;
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
proj.w = WIDTH;
#ifdef _DEBUG
debug_rect_y_ = proj;
#endif
// Comprueba la colisión con las superficies normales y las automáticas
const float POS = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
if (POS > -1) {
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
y_ = POS - HEIGHT;
setState(PlayerState::STANDING);
// Deja de estar enganchado a la superficie automatica
auto_movement_ = false;
} else {
// Si no hay colisión con los muros, comprueba la colisión con las rampas
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {rect.x, rect.y, rect.y + rect.h - 1};
const LineVertical RIGHT_SIDE = {rect.x + rect.w - 1, rect.y, rect.y + rect.h - 1};
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) {
// No está saltando y hay colisión con una rampa
// Calcula la nueva posición
y_ = POINT - HEIGHT;
setState(PlayerState::STANDING);
#ifdef _DEBUG
debug_color_ = static_cast<Uint8>(PaletteColor::YELLOW);
debug_point_ = {x_ + (WIDTH / 2), POINT};
#endif
} else {
// No está saltando y no hay colisón con una rampa
// Calcula la nueva posición
y_ += vy_;
#ifdef _DEBUG
debug_color_ = static_cast<Uint8>(PaletteColor::RED);
#endif
}
} else {
// Esta saltando y no hay colisión con los muros
// Calcula la nueva posición
y_ += vy_;
}
}
}
// Recalcula la posición del jugador y su animación
void Player::move() {
last_position_ = {x_, y_}; // Guarda la posicion actual antes de modificarla
@@ -223,179 +385,29 @@ void Player::move() {
debug_color_ = static_cast<Uint8>(PaletteColor::GREEN);
#endif
// Se mueve hacia la izquierda
// Movimiento horizontal
if (vx_ < 0.0F) {
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_FRect proj;
proj.x = static_cast<int>(x_ + vx_);
proj.y = static_cast<int>(y_);
proj.h = HEIGHT;
proj.w = static_cast<int>(std::ceil(std::fabs(vx_))); // Para evitar que tenga un ancho de 0 pixels
#ifdef _DEBUG
debug_rect_x_ = proj;
#endif
// Comprueba la colisión con las superficies
const int POS = room_->checkRightSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
x_ += vx_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona
x_ = POS + 1;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
if (LY > -1) {
y_ = LY - HEIGHT;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
y_ += 1;
}
}
// Se mueve hacia la derecha
else if (vx_ > 0.0F) {
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_FRect proj;
proj.x = x_ + WIDTH;
proj.y = y_;
proj.h = HEIGHT;
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
#ifdef _DEBUG
debug_rect_x_ = proj;
#endif
// Comprueba la colisión
const int POS = room_->checkLeftSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
x_ += vx_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona
x_ = POS - WIDTH;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
if (RY > -1) {
y_ = RY - HEIGHT;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
y_ += 1;
}
moveHorizontalLeft();
} else if (vx_ > 0.0F) {
moveHorizontalRight();
}
// Si ha salido del suelo, el jugador cae
if (state_ == PlayerState::STANDING && !isOnFloor()) {
setState(PlayerState::FALLING);
// Deja de estar enganchado a la superficie automatica
auto_movement_ = false;
}
// Si ha salido de una superficie automatica, detiene el movimiento automatico
if (state_ == PlayerState::STANDING && isOnFloor() && !isOnAutoSurface()) {
// Deja de estar enganchado a la superficie automatica
auto_movement_ = false;
}
// Se mueve hacia arriba
// Movimiento vertical
if (vy_ < 0.0F) {
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_FRect proj;
proj.x = static_cast<int>(x_);
proj.y = static_cast<int>(y_ + vy_);
proj.h = static_cast<int>(std::ceil(std::fabs(vy_))); // Para evitar que tenga una altura de 0 pixels
proj.w = WIDTH;
#ifdef _DEBUG
debug_rect_y_ = proj;
#endif
// Comprueba la colisión
const int POS = room_->checkBottomSurfaces(&proj);
// Calcula la nueva posición
if (POS == -1) {
// Si no hay colisión
y_ += vy_;
} else {
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
y_ = POS + 1;
setState(PlayerState::FALLING);
}
}
// Se mueve hacia abajo
else if (vy_ > 0.0F) {
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_FRect proj;
proj.x = x_;
proj.y = y_ + HEIGHT;
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
proj.w = WIDTH;
#ifdef _DEBUG
debug_rect_y_ = proj;
#endif
// Comprueba la colisión con las superficies normales y las automáticas
const float POS = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
if (POS > -1) {
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
y_ = POS - HEIGHT;
setState(PlayerState::STANDING);
// Deja de estar enganchado a la superficie automatica
auto_movement_ = false;
} else {
// Si no hay colisión con los muros, comprueba la colisión con las rampas
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {rect.x, rect.y, rect.y + rect.h - 1};
const LineVertical RIGHT_SIDE = {rect.x + rect.w - 1, rect.y, rect.y + rect.h - 1};
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) {
// No está saltando y hay colisión con una rampa
// Calcula la nueva posición
y_ = POINT - HEIGHT;
setState(PlayerState::STANDING);
#ifdef _DEBUG
debug_color_ = static_cast<Uint8>(PaletteColor::YELLOW);
debug_point_ = {x_ + (WIDTH / 2), POINT};
#endif
} else {
// No está saltando y no hay colisón con una rampa
// Calcula la nueva posición
y_ += vy_;
#ifdef _DEBUG
debug_color_ = static_cast<Uint8>(PaletteColor::RED);
#endif
}
} else {
// Esta saltando y no hay colisión con los muros
// Calcula la nueva posición
y_ += vy_;
}
}
moveVerticalUp();
} else if (vy_ > 0.0F) {
moveVerticalDown();
}
placeSprite(); // Coloca el sprite en la nueva posición
@@ -534,12 +546,12 @@ bool Player::checkKillingTiles() {
// Actualiza los puntos de colisión
updateColliderPoints();
// Comprueba si hay contacto y retorna en cuanto se encuentra colisión
for (const auto& c : collider_points_) {
if (room_->getTile(c) == TileType::KILL) {
is_alive_ = false; // Mata al jugador inmediatamente
return true; // Retorna en cuanto se detecta una colisión
}
// Comprueba si hay contacto con algún tile que mata
if (std::ranges::any_of(collider_points_, [this](const auto& c) {
return room_->getTile(c) == TileType::KILL;
})) {
is_alive_ = false; // Mata al jugador inmediatamente
return true; // Retorna en cuanto se detecta una colisión
}
return false; // No se encontró ninguna colisión