fix: Player atravessava rampes al saltar recte si el framerate era molt alt

This commit is contained in:
2025-11-07 20:01:07 +01:00
parent 2c92fe8372
commit 7d0e0e0d18

View File

@@ -17,7 +17,6 @@
// Constructor // Constructor
Player::Player(const Data& player) Player::Player(const Data& player)
: room_(player.room) { : room_(player.room) {
// Inicializa algunas variables
initSprite(player.animations_path); initSprite(player.animations_path);
setColor(); setColor();
applySpawnValues(player.spawn_data); applySpawnValues(player.spawn_data);
@@ -35,21 +34,11 @@ void Player::render() {
// Actualiza las variables del objeto // Actualiza las variables del objeto
void Player::update(float delta_time) { void Player::update(float delta_time) {
if (!is_paused_) { if (!is_paused_) {
/*
handleInput(); // Comprueba las entradas y modifica variables
move(delta_time); // Recalcula la posición del jugador
animate(delta_time); // Establece la animación del jugador
handleBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
handleJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
handleKillingTiles(); // Comprueba que el jugador no toque ningun tile de los que matan
setColor(); // Establece el color del jugador
*/
handleInput(); handleInput();
updateState(); updateState();
move(delta_time); move(delta_time);
animate(delta_time); animate(delta_time);
handleBorders(); handleBorders();
setColor();
} }
} }
@@ -131,12 +120,10 @@ void Player::transitionToState(State state) {
switch (state) { switch (state) {
case State::STANDING: case State::STANDING:
std::cout << "STANDING\n";
vy_ = 0; vy_ = 0;
handleDeathByFalling(); handleDeathByFalling();
break; break;
case State::JUMPING: case State::JUMPING:
std::cout << "JUMPING\n";
if (previous_state_ == State::STANDING) { if (previous_state_ == State::STANDING) {
vy_ = -MAX_VY; vy_ = -MAX_VY;
last_grounded_position_ = y_; last_grounded_position_ = y_;
@@ -144,7 +131,6 @@ void Player::transitionToState(State state) {
} }
break; break;
case State::FALLING: case State::FALLING:
std::cout << "FALLING\n";
last_grounded_position_ = y_; last_grounded_position_ = y_;
vy_ = MAX_VY; vy_ = MAX_VY;
vx_ = 0.0F; vx_ = 0.0F;
@@ -407,8 +393,8 @@ void Player::moveVerticalDown(float delta_time) {
if (state_ == State::FALLING || (state_ == State::JUMPING && vx_ == 0.0F)) { if (state_ == State::FALLING || (state_ == State::JUMPING && vx_ == 0.0F)) {
// No está saltando O salta recto: se pega a las rampas // No está saltando O salta recto: se pega a las rampas
auto rect = toSDLRect(proj); auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h - 1}; 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 - 1}; 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)); const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) { if (POINT > -1) {
// No está saltando y hay colisión con una rampa // No está saltando y hay colisión con una rampa
@@ -436,44 +422,6 @@ void Player::moveVerticalDown(float delta_time) {
} }
} }
// Orquesta el movimiento del jugador
/*
void Player::move(float delta_time) {
applyGravity(delta_time); // Aplica gravedad al jugador
handleState(delta_time); // Comprueba el estado del jugador
// Movimiento horizontal
if (vx_ < 0.0F) {
moveHorizontal(delta_time, -1); // Izquierda
} else if (vx_ > 0.0F) {
moveHorizontal(delta_time, 1); // Derecha
}
// Si ha salido del suelo, el jugador cae
if (state_ == State::STANDING && !isOnFloor()) {
transitionToState(State::FALLING);
auto_movement_ = false;
}
// Si ha salido de una superficie automatica, detiene el movimiento automatico
if (state_ == State::STANDING && isOnFloor() && !isOnAutoSurface()) {
auto_movement_ = false;
}
// Movimiento vertical
if (vy_ < 0.0F) {
moveVerticalUp(delta_time);
} else if (vy_ > 0.0F) {
moveVerticalDown(delta_time);
}
y_prev_ = y_; // Guarda Y DESPUÉS de todo el movimiento (para detectar hitos en sonidos)
// Actualiza la geometría del collider y sprite
updateColliderGeometry();
}
*/
// Establece la animación del jugador // Establece la animación del jugador
void Player::animate(float delta_time) { void Player::animate(float delta_time) {
if (vx_ != 0) { if (vx_ != 0) {
@@ -590,7 +538,6 @@ auto Player::handleKillingTiles() -> bool {
// Establece el color del jugador // Establece el color del jugador
void Player::setColor() { void Player::setColor() {
/*
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) { if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
color_ = static_cast<Uint8>(PaletteColor::CYAN); color_ = static_cast<Uint8>(PaletteColor::CYAN);
} else if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) { } else if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) {
@@ -598,24 +545,6 @@ void Player::setColor() {
} else { } else {
color_ = static_cast<Uint8>(PaletteColor::WHITE); color_ = static_cast<Uint8>(PaletteColor::WHITE);
} }
*/
switch (state_) {
case State::STANDING:
color_ = static_cast<Uint8>(PaletteColor::YELLOW);
break;
case State::JUMPING:
color_ = static_cast<Uint8>(PaletteColor::GREEN);
break;
case State::FALLING:
color_ = static_cast<Uint8>(PaletteColor::RED);
break;
default:
break;
}
} }
// Actualiza los puntos de colisión // Actualiza los puntos de colisión
@@ -642,36 +571,6 @@ void Player::updateFeet() {
feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT - 1}; feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT - 1};
} }
// Cambia el estado del jugador
/*
void Player::transitionToState(State value) {
previous_state_ = state_;
state_ = value;
// Establecer velocidades INMEDIATAMENTE al cambiar de estado
switch (state_) {
case State::STANDING:
vx_ = 0.0F;
vy_ = 0.0F;
break;
case State::JUMPING:
// vx_ mantiene su valor actual (heredado de STANDING)
vy_ = JUMP_VELOCITY;
break;
case State::FALLING:
vx_ = 0.0F; // CRÍTICO para pegarse a rampas
vy_ = MAX_VY;
auto_movement_ = false;
break;
default:
break;
}
}
*/
// Inicializa los sonidos de salto y caida // Inicializa los sonidos de salto y caida
void Player::initSounds() { void Player::initSounds() {
jumping_sound_.clear(); jumping_sound_.clear();
@@ -702,7 +601,6 @@ void Player::applySpawnValues(const SpawnData& spawn) {
// Inicializa el sprite del jugador // Inicializa el sprite del jugador
void Player::initSprite(const std::string& animations_path) { void Player::initSprite(const std::string& animations_path) {
auto animations = Resource::get()->getAnimations(animations_path); auto animations = Resource::get()->getAnimations(animations_path);
sprite_ = std::make_unique<SurfaceAnimatedSprite>(animations); sprite_ = std::make_unique<SurfaceAnimatedSprite>(animations);
sprite_->setWidth(WIDTH); sprite_->setWidth(WIDTH);
sprite_->setHeight(HEIGHT); sprite_->setHeight(HEIGHT);