refinant la classe Player

This commit is contained in:
2025-11-06 14:14:22 +01:00
parent 288e01e47f
commit d875a2706b
2 changed files with 57 additions and 20 deletions

View File

@@ -75,25 +75,7 @@ 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_)
if (auto_movement_) {
// La cinta transportadora tiene el control
vx_ = HORIZONTAL_VELOCITY * room_->getConveyorBeltDirection();
} else {
// El jugador tiene el control
switch (wannaGo) {
case Direction::LEFT:
vx_ = -HORIZONTAL_VELOCITY;
break;
case Direction::RIGHT:
vx_ = HORIZONTAL_VELOCITY;
break;
case Direction::STAY:
vx_ = 0.0F;
break;
default:
break;
}
}
updateVelocity();
}
// 2. Ahora, aplicamos el movimiento y el flip basado en la velocidad resultante
@@ -149,15 +131,20 @@ void Player::transitionToState(State state) {
switch (state) {
case State::STANDING:
std::cout << "STANDING\n";
vy_ = 0;
handleDeathByFalling();
break;
case State::JUMPING:
std::cout << "JUMPING\n";
if (previous_state_ == State::STANDING) {
vy_ = -MAX_VY;
last_grounded_position_ = y_;
updateVelocity();
}
break;
case State::FALLING:
std::cout << "FALLING\n";
last_grounded_position_ = y_;
vy_ = MAX_VY;
vx_ = 0.0F;
@@ -301,7 +288,24 @@ void Player::handleSlopeMovement(int direction) {
if (SLOPE_Y > -1) {
// Hay rampa: sube al jugador para pegarlo a la rampa
y_ = SLOPE_Y - HEIGHT;
// --- INICIO DE LA CORRECCIÓN ---
// Esta es la nueva posición "ideal" a la que nos queremos teletransportar
const float new_y = SLOPE_Y - HEIGHT;
// Solo aplicamos el "snap" si es para SUBIR (new_y < y_)
if (new_y < y_) {
// "y_ - 1.0F" es la posición máxima que podemos subir en 1 frame.
// std::max coge la posición más alta (más baja en pantalla):
// - O la posición ideal (new_y)
// - O la posición actual - 1 píxel
// Esto "suaviza" el salto de 2 píxeles (p.ej. 84 -> 82) en dos
// fotogramas (84 -> 83, y luego 83 -> 82)
y_ = std::max(new_y, y_ - 1.0F);
}
// --- FIN DE LA CORRECCIÓN ---
}
}
@@ -717,4 +721,35 @@ void Player::updateColliderGeometry() {
// Coloca el sprite en la posición del jugador
void Player::placeSprite() {
sprite_->setPos(x_, y_);
}
// Gestiona la muerta al ccaer desde muy alto
void Player::handleDeathByFalling() {
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
}
}
// Calcula la velocidad en x
void Player::updateVelocity() {
if (auto_movement_) {
// La cinta transportadora tiene el control
vx_ = HORIZONTAL_VELOCITY * room_->getConveyorBeltDirection();
} else {
// El jugador tiene el control
switch (wannaGo) {
case Direction::LEFT:
vx_ = -HORIZONTAL_VELOCITY;
break;
case Direction::RIGHT:
vx_ = HORIZONTAL_VELOCITY;
break;
case Direction::STAY:
vx_ = 0.0F;
break;
default:
break;
}
}
}

View File

@@ -184,4 +184,6 @@ class Player {
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
void playJumpSound(); // Calcula y reproduce el sonido de salto
void playFallSound(); // Calcula y reproduce el sonido de caer
void handleDeathByFalling(); // Gestiona la muerte al caer desde muy alto
void updateVelocity(); // Calcula la velocidad en x
};