posss.. mes merdes que no van a cap lloc

This commit is contained in:
2025-11-09 21:51:55 +01:00
parent 1f01268dcf
commit 5dd463ad5a
6 changed files with 135 additions and 155 deletions

View File

@@ -334,7 +334,7 @@ auto Director::run() -> int {
break;
case SceneManager::Scene::LOADING_SCREEN:
runLogo();
runLoadingScreen();
break;
case SceneManager::Scene::TITLE:

View File

@@ -63,19 +63,18 @@ void Player::move(float delta_time) {
void Player::handleHorizontalMovement(float delta_time) {
if (state_ == State::STANDING) {
// 1. Primero, determinamos cuál debe ser la velocidad (vx_)
// Determinama cuál debe ser la velocidad a partir de automovement o de wannaGo
updateVelocity();
}
// 2. Ahora, aplicamos el movimiento y el flip basado en la velocidad resultante
// Aplica el movimiento y el flip basado en la velocidad resultante
if (vx_ < 0.0F) {
moveHorizontal(delta_time, -1);
moveHorizontal(delta_time, Direction::LEFT);
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
} else if (vx_ > 0.0F) {
moveHorizontal(delta_time, 1);
moveHorizontal(delta_time, Direction::RIGHT);
sprite_->setFlip(SDL_FLIP_NONE);
}
// Si vx_ es 0.0F, no se llama a moveHorizontal, lo cual es correcto.
}
void Player::handleVerticalMovement(float delta_time) {
@@ -95,9 +94,6 @@ void Player::handleVerticalMovement(float delta_time) {
}
}
void Player::moveAndCollide(float delta_time) {
}
void Player::handleConveyorBelts() {
if (!auto_movement_ and isOnConveyorBelt() and wannaGo == Direction::STAY) {
auto_movement_ = true;
@@ -200,9 +196,7 @@ void Player::handleState(float delta_time) {
} else if (state_ == State::STANDING) {
// Si no tiene suelo debajo y no está en rampa descendente -> FALLING
if (!isOnFloor() && !isOnConveyorBelt() && !isOnDownSlope()) {
last_grounded_position_ = static_cast<int>(y_); // Guarda Y actual al SALIR de STANDING
transitionToState(State::FALLING); // setState() establece vx_=0, vy_=MAX_VY
playFallSound(delta_time);
}
} else if (state_ == State::JUMPING) {
playJumpSound(delta_time);
@@ -251,8 +245,7 @@ void Player::applyGravity(float delta_time) {
}
// Maneja el movimiento sobre rampas
// direction: -1 para izquierda, 1 para derecha
void Player::handleSlopeMovement(int direction) {
void Player::handleSlopeMovement(Direction direction) {
// No procesa rampas durante el salto (permite atravesarlas cuando salta con movimiento horizontal)
// Pero SÍ procesa en STANDING y FALLING (para pegarse a las rampas)
if (state_ == State::JUMPING) {
@@ -267,16 +260,16 @@ void Player::handleSlopeMovement(int direction) {
// Regla: Si está STANDING y tropieza lateralmente con una Slope, se pega a la slope
// Comprueba si hay rampa en contacto lateral (solo los dos pixels inferiores)
const int SIDE_X = direction < 0 ? static_cast<int>(x_) : static_cast<int>(x_) + WIDTH - 1;
const int SIDE_X = direction == Direction::LEFT ? static_cast<int>(x_) : static_cast<int>(x_) + WIDTH - 1;
const LineVertical SIDE = {
.x = SIDE_X,
.y1 = static_cast<int>(y_) + HEIGHT - 2,
.y2 = static_cast<int>(y_) + HEIGHT - 1};
// Comprueba la rampa correspondiente según la dirección
const int SLOPE_Y = direction < 0 ? room_->checkLeftSlopes(&SIDE) : room_->checkRightSlopes(&SIDE);
const int SLOPE_Y = direction == Direction::LEFT ? room_->checkLeftSlopes(SIDE) : room_->checkRightSlopes(SIDE);
if (SLOPE_Y > -1) {
if (SLOPE_Y != Collision::NONE) {
// Hay rampa: sube al jugador para pegarlo a la rampa
// --- INICIO DE LA CORRECCIÓN ---
@@ -300,38 +293,44 @@ void Player::handleSlopeMovement(int direction) {
}
// Maneja el movimiento horizontal
// direction: -1 para izquierda, 1 para derecha
void Player::moveHorizontal(float delta_time, int direction) {
void Player::moveHorizontal(float delta_time, Direction direction) {
const float DISPLACEMENT = vx_ * delta_time;
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_FRect proj;
if (direction < 0) {
switch (direction) {
case Direction::LEFT:
// Movimiento a la izquierda
proj = {
.x = x_ + DISPLACEMENT,
.y = y_,
.w = std::ceil(std::fabs(DISPLACEMENT)),
.h = HEIGHT};
} else {
break;
case Direction::RIGHT:
// Movimiento a la derecha
proj = {
.x = x_ + WIDTH,
.y = y_,
.w = std::ceil(DISPLACEMENT),
.h = HEIGHT};
break;
default:
break;
}
// Comprueba la colisión con las superficies
const int POS = direction < 0 ? room_->checkRightSurfaces(&proj) : room_->checkLeftSurfaces(&proj);
const int POS = direction == Direction::LEFT ? room_->checkRightSurfaces(proj) : room_->checkLeftSurfaces(proj);
// Calcula la nueva posición
if (POS == -1) {
if (POS == Collision::NONE) {
// No hay colisión: mueve al jugador
x_ += DISPLACEMENT;
} else {
// Hay colisión: reposiciona al jugador en el punto de colisión
x_ = direction < 0 ? POS + 1 : POS - WIDTH;
x_ = direction == Direction::LEFT ? POS + 1 : POS - WIDTH;
}
// Maneja el movimiento sobre rampas
@@ -350,10 +349,10 @@ void Player::moveVerticalUp(float delta_time) {
};
// Comprueba la colisión
const int POS = room_->checkBottomSurfaces(&proj);
const int POS = room_->checkBottomSurfaces(proj);
// Calcula la nueva posición
if (POS == -1) {
if (POS == Collision::NONE) {
// Si no hay colisión
y_ += DISPLACEMENT;
} else {
@@ -376,21 +375,11 @@ void Player::moveVerticalDown(float delta_time) {
};
// 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) {
const float POS = std::max(room_->checkTopSurfaces(proj), room_->checkAutoSurfaces(proj));
if (POS != Collision::NONE) {
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
y_ = POS - HEIGHT;
// VERIFICAR MUERTE ANTES de cambiar de estado (PLAYER_MECHANICS.md línea 1268-1274)
const int FALL_DISTANCE = static_cast<int>(y_) - last_grounded_position_;
if (previous_state_ == State::FALLING && FALL_DISTANCE > MAX_FALLING_HEIGHT) {
is_alive_ = false; // Muere si cae más de 32 píxeles
}
transitionToState(State::STANDING);
last_grounded_position_ = static_cast<int>(y_); // Actualizar AL ENTRAR en 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
// CORRECCIÓN: FALLING siempre se pega a rampas, JUMPING se pega solo si vx_ == 0
@@ -399,20 +388,12 @@ void Player::moveVerticalDown(float delta_time) {
auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h};
const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h};
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) {
const float POINT = std::max(room_->checkRightSlopes(RIGHT_SIDE), room_->checkLeftSlopes(LEFT_SIDE));
if (POINT != Collision::NONE) {
// No está saltando y hay colisión con una rampa
// Calcula la nueva posición
y_ = POINT - HEIGHT;
// VERIFICAR MUERTE ANTES de cambiar de estado (PLAYER_MECHANICS.md línea 1268-1274)
const int FALL_DISTANCE = static_cast<int>(y_) - last_grounded_position_;
if (previous_state_ == State::FALLING && FALL_DISTANCE > MAX_FALLING_HEIGHT) {
is_alive_ = false; // Muere si cae más de 32 píxeles
}
transitionToState(State::STANDING);
last_grounded_position_ = static_cast<int>(y_); // Actualizar AL ENTRAR en STANDING
} else {
// No está saltando y no hay colisón con una rampa
// Calcula la nueva posición
@@ -452,7 +433,6 @@ void Player::playJumpSound(float delta_time) {
}
}
// Calcula y reproduce el sonido de caída basado en distancia vertical recorrida
void Player::playFallSound(float delta_time) {
size_t sound_index;
@@ -466,18 +446,19 @@ void Player::playFallSound(float delta_time) {
// Comprueba si el jugador tiene suelo debajo de los pies
auto Player::isOnFloor() -> bool {
bool on_floor = false;
updateFeet();
// Comprueba las superficies
for (auto f : under_feet_) {
on_floor |= room_->checkTopSurfaces(&f);
on_floor |= room_->checkConveyorBelts(&f);
}
on_floor |= room_->checkTopSurfaces(under_left_foot_);
on_floor |= room_->checkTopSurfaces(under_right_foot_);
// Comprueba las cintas transportadoras
on_floor |= room_->checkConveyorBelts(under_left_foot_);
on_floor |= room_->checkConveyorBelts(under_right_foot_);
// Comprueba las rampas
auto on_slope_l = room_->checkLeftSlopes(under_feet_.data());
auto on_slope_r = room_->checkRightSlopes(&under_feet_[1]);
auto on_slope_l = room_->checkLeftSlopes(under_left_foot_);
auto on_slope_r = room_->checkRightSlopes(under_right_foot_);
return on_floor || on_slope_l || on_slope_r;
}
@@ -485,13 +466,11 @@ auto Player::isOnFloor() -> bool {
// Comprueba si el jugador esta sobre una superficie automática
auto Player::isOnConveyorBelt() -> bool {
bool on_conveyor_belt = false;
updateFeet();
// Comprueba las superficies
for (auto f : under_feet_) {
on_conveyor_belt |= room_->checkConveyorBelts(&f);
}
on_conveyor_belt |= room_->checkConveyorBelts(under_left_foot_);
on_conveyor_belt |= room_->checkConveyorBelts(under_right_foot_);
return on_conveyor_belt;
}
@@ -499,19 +478,18 @@ auto Player::isOnConveyorBelt() -> bool {
// Comprueba si el jugador está sobre una rampa hacia abajo
auto Player::isOnDownSlope() -> bool {
bool on_slope = false;
updateFeet();
// Cuando el jugador baja una escalera, se queda volando
// Hay que mirar otro pixel más por debajo
SDL_FPoint foot0 = under_feet_[0];
SDL_FPoint foot1 = under_feet_[1];
foot0.y += 1.0F;
foot1.y += 1.0F;
SDL_FPoint left_foot = under_left_foot_;
SDL_FPoint right_foot = under_right_foot_;
left_foot.y += 1.0F;
right_foot.y += 1.0F;
// Comprueba las rampas
on_slope |= room_->checkLeftSlopes(&foot0);
on_slope |= room_->checkRightSlopes(&foot1);
on_slope |= room_->checkLeftSlopes(left_foot);
on_slope |= room_->checkRightSlopes(right_foot);
return on_slope;
}
@@ -555,13 +533,12 @@ void Player::updateColliderPoints() {
// Actualiza los puntos de los pies
void Player::updateFeet() {
const SDL_FPoint P = {x_, y_};
under_feet_[0] = {.x = P.x, .y = P.y + HEIGHT};
under_feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT};
feet_[0] = {.x = P.x, .y = P.y + HEIGHT - 1};
feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT - 1};
under_left_foot_ = {
.x = x_,
.y = y_ + HEIGHT};
under_right_foot_ = {
.x = x_ + WIDTH - 1,
.y = y_ + HEIGHT};
}
// Inicializa los sonidos de salto y caida
@@ -658,7 +635,6 @@ auto Player::FallSoundController::shouldPlay(float delta_time, float current_y,
return false;
}
// Aplica los valores de spawn al jugador
void Player::applySpawnValues(const SpawnData& spawn) {
x_ = spawn.x;

View File

@@ -149,8 +149,8 @@ class Player {
// --- Variables de colisión ---
SDL_FRect collider_box_; // Caja de colisión con los enemigos u objetos
std::array<SDL_FPoint, 8> collider_points_{}; // Puntos de colisión con el mapa
std::array<SDL_FPoint, 2> under_feet_{}; // Contiene los puntos que hay bajo cada pie del jugador
std::array<SDL_FPoint, 2> feet_{}; // Contiene los puntos que hay en el pie del jugador
SDL_FPoint under_left_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
SDL_FPoint under_right_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior derecha del jugador
// --- Variables de juego ---
bool is_on_border_ = false; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
@@ -173,7 +173,6 @@ class Player {
void handleConveyorBelts();
void handleShouldFall();
void updateState(float delta_time);
void moveAndCollide(float delta_time);
// --- Funciones de inicialización ---
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
@@ -192,10 +191,10 @@ class Player {
// --- Funciones de movimiento y colisión ---
void move(float delta_time); // Orquesta el movimiento del jugador
void moveHorizontal(float delta_time, int direction); // Maneja el movimiento horizontal (-1: izq, 1: der)
void moveHorizontal(float delta_time, Direction direction); // Maneja el movimiento horizontal (-1: izq, 1: der)
void moveVerticalUp(float delta_time); // Maneja el movimiento vertical hacia arriba
void moveVerticalDown(float delta_time); // Maneja el movimiento vertical hacia abajo
void handleSlopeMovement(int direction); // Maneja el movimiento sobre rampas
void handleSlopeMovement(Direction direction); // Maneja el movimiento sobre rampas
// --- Funciones de detección de superficies ---
auto isOnFloor() -> bool; // Comprueba si el jugador tiene suelo debajo de los pies

View File

@@ -681,109 +681,109 @@ void Room::renderAnimatedTiles() {
}
// Comprueba las colisiones
auto Room::checkRightSurfaces(SDL_FRect* rect) -> int {
auto Room::checkRightSurfaces(SDL_FRect& rect) -> int {
for (const auto& s : right_walls_) {
if (checkCollision(s, *rect)) {
if (checkCollision(s, rect)) {
return s.x;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkLeftSurfaces(SDL_FRect* rect) -> int {
auto Room::checkLeftSurfaces(SDL_FRect& rect) -> int {
for (const auto& s : left_walls_) {
if (checkCollision(s, *rect)) {
if (checkCollision(s, rect)) {
return s.x;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkTopSurfaces(SDL_FRect* rect) -> int {
auto Room::checkTopSurfaces(SDL_FRect& rect) -> int {
for (const auto& s : top_floors_) {
if (checkCollision(s, *rect)) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkBottomSurfaces(SDL_FRect* rect) -> int {
auto Room::checkBottomSurfaces(SDL_FRect& rect) -> int {
for (const auto& s : bottom_floors_) {
if (checkCollision(s, *rect)) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkAutoSurfaces(SDL_FRect* rect) -> int {
auto Room::checkAutoSurfaces(SDL_FRect& rect) -> int {
for (const auto& s : conveyor_belt_floors_) {
if (checkCollision(s, *rect)) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkTopSurfaces(SDL_FPoint* p) -> bool {
auto Room::checkTopSurfaces(SDL_FPoint& p) -> bool {
return std::ranges::any_of(top_floors_, [&](const auto& s) {
return checkCollision(s, *p);
return checkCollision(s, p);
});
}
// Comprueba las colisiones
auto Room::checkConveyorBelts(SDL_FPoint* p) -> bool {
auto Room::checkConveyorBelts(SDL_FPoint& p) -> bool {
return std::ranges::any_of(conveyor_belt_floors_, [&](const auto& s) {
return checkCollision(s, *p);
return checkCollision(s, p);
});
}
// Comprueba las colisiones
auto Room::checkLeftSlopes(const LineVertical* line) -> int {
auto Room::checkLeftSlopes(const LineVertical& line) -> int {
for (const auto& slope : left_slopes_) {
const auto P = checkCollision(slope, *line);
const auto P = checkCollision(slope, line);
if (P.x != -1) {
return P.y;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkLeftSlopes(SDL_FPoint* p) -> bool {
auto Room::checkLeftSlopes(SDL_FPoint& p) -> bool {
return std::ranges::any_of(left_slopes_, [&](const auto& slope) {
return checkCollision(*p, slope);
return checkCollision(p, slope);
});
}
// Comprueba las colisiones
auto Room::checkRightSlopes(const LineVertical* line) -> int {
auto Room::checkRightSlopes(const LineVertical& line) -> int {
for (const auto& slope : right_slopes_) {
const auto P = checkCollision(slope, *line);
const auto P = checkCollision(slope, line);
if (P.x != -1) {
return P.y;
}
}
return -1;
return Collision::NONE;
}
// Comprueba las colisiones
auto Room::checkRightSlopes(SDL_FPoint* p) -> bool {
auto Room::checkRightSlopes(SDL_FPoint& p) -> bool {
return std::ranges::any_of(right_slopes_, [&](const auto& slope) {
return checkCollision(*p, slope);
return checkCollision(p, slope);
});
}

View File

@@ -78,17 +78,17 @@ class Room {
auto itemCollision(SDL_FRect& rect) -> bool; // Indica si hay colision con un objeto a partir de un rectangulo
static auto getTileSize() -> int { return TILE_SIZE; } // Obten el tamaño del tile
static auto getSlopeHeight(SDL_FPoint p, Tile slope) -> int; // Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
auto checkRightSurfaces(SDL_FRect* rect) -> int; // Comprueba las colisiones
auto checkLeftSurfaces(SDL_FRect* rect) -> int; // Comprueba las colisiones
auto checkTopSurfaces(SDL_FRect* rect) -> int; // Comprueba las colisiones
auto checkBottomSurfaces(SDL_FRect* rect) -> int; // Comprueba las colisiones
auto checkAutoSurfaces(SDL_FRect* rect) -> int; // Comprueba las colisiones
auto checkTopSurfaces(SDL_FPoint* p) -> bool; // Comprueba las colisiones
auto checkConveyorBelts(SDL_FPoint* p) -> bool; // Comprueba las colisiones
auto checkLeftSlopes(const LineVertical* line) -> int; // Comprueba las colisiones
auto checkLeftSlopes(SDL_FPoint* p) -> bool; // Comprueba las colisiones
auto checkRightSlopes(const LineVertical* line) -> int; // Comprueba las colisiones
auto checkRightSlopes(SDL_FPoint* p) -> bool; // Comprueba las colisiones
auto checkRightSurfaces(SDL_FRect& rect) -> int; // Comprueba las colisiones
auto checkLeftSurfaces(SDL_FRect& rect) -> int; // Comprueba las colisiones
auto checkTopSurfaces(SDL_FRect& rect) -> int; // Comprueba las colisiones
auto checkBottomSurfaces(SDL_FRect& rect) -> int; // Comprueba las colisiones
auto checkAutoSurfaces(SDL_FRect& rect) -> int; // Comprueba las colisiones
auto checkTopSurfaces(SDL_FPoint& p) -> bool; // Comprueba las colisiones
auto checkConveyorBelts(SDL_FPoint& p) -> bool; // Comprueba las colisiones
auto checkLeftSlopes(const LineVertical& line) -> int; // Comprueba las colisiones
auto checkLeftSlopes(SDL_FPoint& p) -> bool; // Comprueba las colisiones
auto checkRightSlopes(const LineVertical& line) -> int; // Comprueba las colisiones
auto checkRightSlopes(SDL_FPoint& p) -> bool; // Comprueba las colisiones
void setPaused(bool value) { is_paused_ = value; }; // Pone el mapa en modo pausa
[[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; } // Obten la direccion de las superficies automaticas
static auto loadRoomFile(const std::string& file_path, bool verbose = false) -> Data; // Carga las variables desde un fichero de mapa

View File

@@ -42,3 +42,8 @@ constexpr int GAMECANVAS_THIRD_QUARTER_X = (GAMECANVAS_WIDTH / 4) * 3;
constexpr int GAMECANVAS_CENTER_Y = GAMECANVAS_HEIGHT / 2;
constexpr int GAMECANVAS_FIRST_QUARTER_Y = GAMECANVAS_HEIGHT / 4;
constexpr int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3;
namespace Collision
{
constexpr int NONE = -1;
}