treballant en la classe Player

This commit is contained in:
2025-10-30 22:01:20 +01:00
parent 1041aaf385
commit c5cfb518a2
4 changed files with 293 additions and 206 deletions

View File

@@ -46,7 +46,7 @@ constexpr int BORDER_HEIGHT = 24; // Alto del borde por defecto
// AUDIO
// =============================================================================
constexpr int AUDIO_VOLUME = 100; // Volumen por defecto
constexpr bool AUDIO_ENABLED = false; // Audio por defecto
constexpr bool AUDIO_ENABLED = true; // Audio por defecto
// MUSIC
constexpr int MUSIC_VOLUME = 80; // Volumen por defecto de la musica

View File

@@ -35,42 +35,66 @@ void Player::render() {
// Actualiza las variables del objeto
void Player::update(float delta_time) {
if (!is_paused_) {
// 1. Procesamiento de entrada: captura las intenciones del jugador
checkInput();
// 2. Física: aplica gravedad y actualiza velocidades
applyGravity(delta_time);
updateVelocity();
// 3. Movimiento: ejecuta movimiento, actualiza estado durante movimiento, resuelve colisiones
move(delta_time);
// 4. Finalización: animación, comprobaciones y efectos
animate(delta_time);
checkBorders();
checkJumpEnd();
checkKillingTiles();
setColor();
checkInput(delta_time); // Comprueba las entradas y modifica variables
move(delta_time); // Recalcula la posición del jugador
animate(delta_time); // Establece la animación del jugador
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
checkJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
checkKillingTiles(); // Comprueba que el jugador no toque ningun tile de los que matan
setColor(); // Establece el color del jugador
}
}
// Comprueba las entradas y establece las banderas de intención
void Player::checkInput() {
// Resetea las banderas de intención
want_to_jump_ = false;
want_to_move_left_ = false;
want_to_move_right_ = false;
// Comprueba las entradas y modifica variables
void Player::checkInput(float delta_time) {
(void)delta_time; // No usado en este método, pero mantenido para consistencia
// Captura las intenciones de movimiento
if (Input::get()->checkInput(InputAction::LEFT)) {
want_to_move_left_ = true;
} else if (Input::get()->checkInput(InputAction::RIGHT)) {
want_to_move_right_ = true;
// Solo comprueba las entradas de dirección cuando está sobre una superficie
if (state_ != State::STANDING) {
return;
}
if (!auto_movement_) {
// Comprueba las entradas de desplazamiento lateral solo en el caso de no estar enganchado a una superficie automatica
if (Input::get()->checkInput(InputAction::LEFT)) {
vx_ = -HORIZONTAL_VELOCITY;
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
}
else if (Input::get()->checkInput(InputAction::RIGHT)) {
vx_ = HORIZONTAL_VELOCITY;
sprite_->setFlip(SDL_FLIP_NONE);
}
else {
// No se pulsa ninguna dirección
vx_ = 0.0F;
if (isOnAutoSurface()) {
// Si deja de moverse sobre una superficie se engancha
auto_movement_ = true;
}
}
} else { // El movimiento lo proporciona la superficie
vx_ = HORIZONTAL_VELOCITY * room_->getAutoSurfaceDirection();
if (vx_ > 0.0F) {
sprite_->setFlip(SDL_FLIP_NONE);
} else {
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
}
}
// Captura la intención de salto
if (Input::get()->checkInput(InputAction::JUMP)) {
want_to_jump_ = true;
// Solo puede saltar si ademas de estar (state == STANDING)
// Esta sobre el suelo, rampa o suelo que se mueve
// Esto es para evitar el salto desde el vacio al cambiar de pantalla verticalmente
// Ya que se coloca el estado STANDING al cambiar de pantalla
if (isOnFloor() || isOnAutoSurface()) {
setState(State::JUMPING);
vy_ = JUMP_VELOCITY;
last_grounded_position_ = static_cast<int>(y_);
}
}
}
@@ -101,125 +125,24 @@ void Player::checkBorders() {
}
}
// Actualiza el estado del jugador basado en física e intenciones
void Player::updateState(float delta_time) {
// Guarda el estado anterior para detectar transiciones
previous_state_ = state_;
// Comprueba el estado del jugador
void Player::checkState(float delta_time) {
(void)delta_time; // No usado actualmente
// Máquina de estados: determina transiciones basándose en PLAYER_RULES.md
switch (state_) {
case State::STANDING: {
// Comprueba muerte por caída desde altura
if (previous_state_ == State::FALLING) {
const int FALLING_DISTANCE = static_cast<int>(y_) - last_grounded_position_;
if (FALLING_DISTANCE > MAX_FALLING_HEIGHT) {
is_alive_ = false;
}
}
// Actualiza la posición de tierra
last_grounded_position_ = static_cast<int>(y_);
jumping_time_ = 0.0F;
// Regla: Si no tiene suelo debajo y no está JUMPING -> FALLING
if (shouldFall()) {
setState(State::FALLING);
return;
}
// Regla: Puede saltar si está sobre suelo o superficie automática
if (want_to_jump_ && canJump()) {
setState(State::JUMPING);
return;
}
// Nota: auto_movement_ se gestiona en updateVelocity() basado en el input
break;
}
case State::JUMPING: {
// Actualiza el tiempo de salto
jumping_time_ += delta_time;
playJumpSound();
// Regla: Si durante el salto Y > jump_init_pos -> FALLING
if (static_cast<int>(y_) >= jump_init_pos_ && vy_ > 0.0F) {
setState(State::FALLING);
return;
}
break;
}
case State::FALLING: {
// Reproduce sonido de caída
playFallSound();
break;
}
default:
break;
// Reproduce sonidos según el estado
if (state_ == State::FALLING) {
playFallSound();
}
}
// Comprueba si el jugador puede saltar
auto Player::canJump() -> bool {
// Solo puede saltar si está STANDING y sobre suelo o superficie automática
return state_ == State::STANDING && (isOnFloor() || isOnAutoSurface());
}
// Comprueba si el jugador debe caer
auto Player::shouldFall() -> bool {
// Cae si no tiene suelo, no está en superficie automática, y no está en rampa descendente
return !isOnFloor() && !isOnAutoSurface() && !isOnDownSlope();
}
// Actualiza velocidad basada en estado e intenciones
void Player::updateVelocity() {
switch (state_) {
case State::STANDING: {
// Regla: Si está STANDING -> vy_ = 0
vy_ = 0.0F;
if (!auto_movement_) {
// Movimiento normal: el jugador controla la dirección
if (want_to_move_left_) {
vx_ = -HORIZONTAL_VELOCITY;
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
} else if (want_to_move_right_) {
vx_ = HORIZONTAL_VELOCITY;
sprite_->setFlip(SDL_FLIP_NONE);
} else {
// No se pulsa ninguna dirección
vx_ = 0.0F;
// Regla conveyor belt: cuando el jugador deja de pulsar, se acopla al movimiento
if (isOnAutoSurface()) {
auto_movement_ = true;
}
}
} else {
// Movimiento automático: conveyor belt controla la dirección
// Regla conveyor belt: el jugador no puede cambiar de dirección
vx_ = HORIZONTAL_VELOCITY * room_->getAutoSurfaceDirection();
sprite_->setFlip(vx_ > 0.0F ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL);
}
break;
else if (state_ == State::STANDING) {
// Si no tiene suelo debajo y no está en rampa descendente -> FALLING
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope()) {
last_grounded_position_ = static_cast<int>(y_); // Guarda Y actual al SALIR de STANDING
setState(State::FALLING); // setState() establece vx_=0, vy_=MAX_VY
playFallSound();
}
case State::JUMPING: {
// Durante el salto, mantiene la velocidad horizontal
// La velocidad vertical la controla applyGravity()
break;
}
case State::FALLING: {
// Regla: Si está FALLING -> vx_ = 0 (no puede cambiar dirección en el aire)
vx_ = 0.0F;
// La velocidad vertical es MAX_VY (ya configurada por setState)
break;
}
default:
break;
}
else if (state_ == State::JUMPING) {
playJumpSound();
}
}
@@ -248,6 +171,8 @@ void Player::switchBorders() {
break;
}
// CRÍTICO: Resetear last_grounded_position_ para evitar muerte falsa por diferencia de Y entre pantallas
last_grounded_position_ = static_cast<int>(y_);
is_on_border_ = false;
placeSprite();
}
@@ -378,27 +303,47 @@ void Player::moveVerticalDown(float delta_time) {
if (POS > -1) {
// 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
}
setState(State::STANDING);
auto_movement_ = false; // Desactiva conveyor belt al aterrizar
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
// Regla: La unica forma de atravesar una Slope es en estado JUMPING y con vx_ != 0
if (state_ != State::JUMPING || vx_ == 0.0F) {
// No está saltando o salta recto: se pega a las rampas
// CORRECCIÓN: FALLING siempre se pega a rampas, JUMPING se pega solo si vx_ == 0
if (state_ == State::FALLING || (state_ == State::JUMPING && vx_ == 0.0F)) {
// No está saltando O salta recto: se pega a las rampas
auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {.x = rect.x, .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 - 1};
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) {
// Hay colisión con una rampa: se pega a ella
// 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
}
setState(State::STANDING);
last_grounded_position_ = static_cast<int>(y_); // Actualizar AL ENTRAR en STANDING
} else {
// No hay colisión con rampa: continúa cayendo
// No está saltando y no hay colisón con una rampa
// Calcula la nueva posición
y_ += DISPLACEMENT;
}
} else {
// Está saltando con movimiento horizontal: atraviesa las rampas
// Esta saltando con movimiento horizontal y no hay colisión con los muros
// Calcula la nueva posición (atraviesa rampas)
y_ += DISPLACEMENT;
}
}
@@ -406,6 +351,9 @@ void Player::moveVerticalDown(float delta_time) {
// Orquesta el movimiento del jugador
void Player::move(float delta_time) {
applyGravity(delta_time); // Aplica gravedad al jugador
checkState(delta_time); // Comprueba el estado del jugador
// Movimiento horizontal
if (vx_ < 0.0F) {
moveHorizontal(delta_time, -1); // Izquierda
@@ -413,9 +361,16 @@ void Player::move(float delta_time) {
moveHorizontal(delta_time, 1); // Derecha
}
// Actualización de estado DURANTE el movimiento (después de horizontal, antes de vertical)
// Esto asegura que el estado se actualice con la posición correcta
updateState(delta_time);
// Si ha salido del suelo, el jugador cae
if (state_ == State::STANDING && !isOnFloor()) {
setState(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) {
@@ -424,6 +379,8 @@ void Player::move(float delta_time) {
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();
}
@@ -437,40 +394,44 @@ void Player::animate(float delta_time) {
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
void Player::checkJumpEnd() {
if (state_ == State::JUMPING && vy_ > 0.0F && static_cast<int>(y_) >= jump_init_pos_) {
// Si alcanza la altura de salto inicial, pasa al estado de caída
// CORRECCIÓN: Usar > (mayor) en lugar de >= (mayor o igual)
// Si el jugador vuelve EXACTAMENTE a la altura inicial, debe CONTINUAR en JUMPING
// Solo cuando la SUPERA (desciende más allá) cambia a FALLING
if (state_ == State::JUMPING && vy_ > 0.0F && static_cast<int>(y_) > last_grounded_position_) {
setState(State::FALLING);
}
}
// Calcula y reproduce el sonido de salto
// Calcula y reproduce el sonido de salto basado en distancia vertical recorrida
void Player::playJumpSound() {
const int SOUND_INDEX = static_cast<int>(jumping_time_ / SOUND_INTERVAL);
const int PREVIOUS_INDEX = static_cast<int>((jumping_time_ - SOUND_INTERVAL) / SOUND_INTERVAL);
// Sistema basado en distancia vertical, no en tiempo (PLAYER_MECHANICS.md línea 107-120)
const float DISTANCE_FROM_START = std::abs(y_ - static_cast<float>(last_grounded_position_));
const int SOUND_INDEX = static_cast<int>(DISTANCE_FROM_START / SOUND_DISTANCE_INTERVAL);
// Solo reproduce el sonido cuando cambia de índice
// Calcular índice previo (frame anterior)
const float PREV_DISTANCE = std::abs(y_prev_ - static_cast<float>(last_grounded_position_));
const int PREVIOUS_INDEX = static_cast<int>(PREV_DISTANCE / SOUND_DISTANCE_INTERVAL);
// Solo reproduce cuando cambia de índice (nuevo hito alcanzado)
if (SOUND_INDEX != PREVIOUS_INDEX && SOUND_INDEX < static_cast<int>(jumping_sound_.size())) {
JA_PlaySound(jumping_sound_[SOUND_INDEX]);
}
}
// Calcula y reproduce el sonido de caer
// Calcula y reproduce el sonido de caída basado en distancia vertical recorrida
void Player::playFallSound() {
return;
// Sistema basado en distancia vertical, no en tiempo (PLAYER_MECHANICS.md línea 193-206)
const float DISTANCE_FALLEN = y_ - static_cast<float>(last_grounded_position_);
const int SOUND_INDEX = static_cast<int>(DISTANCE_FALLEN / SOUND_DISTANCE_INTERVAL);
/*
const int SOUND_INDEX = static_cast<int>(falling_time_ / SOUND_INTERVAL);
const int PREVIOUS_INDEX = static_cast<int>((falling_time_ - SOUND_INTERVAL) / SOUND_INTERVAL);
// Calcular índice previo (frame anterior)
const float PREV_DISTANCE = y_prev_ - static_cast<float>(last_grounded_position_);
const int PREVIOUS_INDEX = static_cast<int>(PREV_DISTANCE / SOUND_DISTANCE_INTERVAL);
// Solo reproduce el sonido cuando cambia de índice
if (SOUND_INDEX != PREVIOUS_INDEX) {
const int CLAMPED_INDEX = std::min(SOUND_INDEX, static_cast<int>(falling_sound_.size()) - 1);
JA_PlaySound(falling_sound_[CLAMPED_INDEX]);
#ifdef _DEBUG
Debug::get()->add("FALL: " + std::to_string(CLAMPED_INDEX));
#endif
// Solo reproduce cuando cambia de índice (nuevo hito alcanzado)
if (SOUND_INDEX != PREVIOUS_INDEX && SOUND_INDEX < static_cast<int>(falling_sound_.size())) {
JA_PlaySound(falling_sound_[SOUND_INDEX]);
}
*/
}
// Comprueba si el jugador tiene suelo debajo de los pies
@@ -597,32 +558,25 @@ void Player::updateFeet() {
// Cambia el estado del jugador
void Player::setState(State value) {
// Solo actualiza el estado y configura las variables iniciales
// NO llama a updateState() para evitar recursión circular
previous_state_ = state_;
state_ = value;
// Establecer velocidades INMEDIATAMENTE al cambiar de estado
switch (state_) {
case State::STANDING:
// Se establecerá vy_ = 0 en updateVelocity()
vx_ = 0.0F;
vy_ = 0.0F;
break;
case State::JUMPING:
// Configura el salto
// vx_ mantiene su valor actual (heredado de STANDING)
vy_ = JUMP_VELOCITY;
jump_init_pos_ = y_;
jumping_time_ = 0.0F;
break;
case State::FALLING:
// Configura la caída
vx_ = 0.0F; // CRÍTICO para pegarse a rampas
vy_ = MAX_VY;
// vx_ = 0 se establecerá en updateVelocity()
if (previous_state_ == State::STANDING) {
last_grounded_position_ = static_cast<int>(y_);
}
auto_movement_ = false;
jumping_time_ = 0.0F;
break;
default:
@@ -649,9 +603,10 @@ void Player::initSounds() {
void Player::applySpawnValues(const SpawnData& spawn) {
x_ = spawn.x;
y_ = spawn.y;
y_prev_ = spawn.y; // Inicializar y_prev_ igual a y_ para evitar saltos en primer frame
vx_ = spawn.vx;
vy_ = spawn.vy;
jump_init_pos_ = spawn.jump_init_pos;
last_grounded_position_ = spawn.last_grounded_position;
state_ = spawn.state;
sprite_->setFlip(spawn.flip);
}

View File

@@ -28,7 +28,7 @@ class Player {
float y = 0;
float vx = 0;
float vy = 0;
int jump_init_pos = 0;
int last_grounded_position = 0;
State state = State::STANDING;
SDL_FlipMode flip = SDL_FLIP_NONE;
@@ -36,12 +36,12 @@ class Player {
SpawnData() = default;
// Constructor con parámetros
SpawnData(float x, float y, float vx, float vy, int jump_init_pos, State state, SDL_FlipMode flip)
SpawnData(float x, float y, float vx, float vy, int last_grounded_position, State state, SDL_FlipMode flip)
: x(x),
y(y),
vx(vx),
vy(vy),
jump_init_pos(jump_init_pos),
last_grounded_position(last_grounded_position),
state(state),
flip(flip) {}
};
@@ -73,7 +73,7 @@ class Player {
void switchBorders(); // Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
auto getRect() -> SDL_FRect { return {x_, y_, WIDTH, HEIGHT}; } // Obtiene el rectangulo que delimita al jugador
auto getCollider() -> SDL_FRect& { return collider_box_; } // Obtiene el rectangulo de colision del jugador
auto getSpawnParams() -> SpawnData { return {x_, y_, vx_, vy_, jump_init_pos_, state_, sprite_->getFlip()}; } // Obtiene el estado de reaparición del jugador
auto getSpawnParams() -> SpawnData { return {x_, y_, vx_, vy_, last_grounded_position_, state_, sprite_->getFlip()}; } // Obtiene el estado de reaparición del jugador
void setColor(); // Establece el color del jugador
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); } // Establece la habitación en la que se encuentra el jugador
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; } // Comprueba si el jugador esta vivo
@@ -92,7 +92,7 @@ class Player {
static constexpr float GRAVITY_FORCE = 155.6F; // Fuerza de gravedad en pixels/segundo² (0.035 * 66.67²)
// --- Constantes de sonido ---
static constexpr float SOUND_INTERVAL = 0.06F; // Intervalo entre sonidos de salto/caída en segundos (4 frames a 66.67fps)
static constexpr float SOUND_DISTANCE_INTERVAL = 3.0F; // Distancia en píxeles entre cada sonido de salto/caída
// --- --- Objetos y punteros --- ---
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
@@ -101,6 +101,7 @@ class Player {
// --- Variables de posición y física ---
float x_ = 0.0F; // Posición del jugador en el eje X
float y_ = 0.0F; // Posición del jugador en el eje Y
float y_prev_ = 0.0F; // Posición Y del frame anterior (para detectar hitos de distancia en sonidos)
float vx_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje X
float vy_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje Y
@@ -108,11 +109,6 @@ class Player {
State state_ = State::STANDING; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
State previous_state_ = State::STANDING; // Estado previo en el que se encontraba el jugador
// --- Variables de entrada (input intent) ---
bool want_to_jump_ = false; // Indica si el jugador quiere saltar
bool want_to_move_left_ = false; // Indica si el jugador quiere moverse a la izquierda
bool want_to_move_right_ = false; // Indica si el jugador quiere moverse a la derecha
// --- 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
@@ -125,14 +121,12 @@ class Player {
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
int jump_init_pos_ = 0; // Valor del eje Y en el que se inicia el salto
int last_grounded_position_ = 0; // Ultima posición en Y en la que se estaba en contacto con el suelo
int last_grounded_position_ = 0; // Ultima posición en Y en la que se estaba en contacto con el suelo (hace doble función: tracking de caída + altura inicial del salto)
// --- Variables de renderizado y sonido ---
Uint8 color_ = 0; // Color del jugador
std::vector<JA_Sound_t*> jumping_sound_; // Vecor con todos los sonidos del salto
std::vector<JA_Sound_t*> falling_sound_; // Vecor con todos los sonidos de la caída
float jumping_time_ = 0.0F; // Tiempo acumulado de salto en segundos
// --- Funciones de inicialización ---
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
@@ -140,17 +134,14 @@ class Player {
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al jugador
// --- Funciones de procesamiento de entrada ---
void checkInput(); // Comprueba las entradas y establece las banderas de intención
void checkInput(float delta_time); // Comprueba las entradas y modifica variables
// --- Funciones de gestión de estado ---
void updateState(float delta_time); // Actualiza el estado del jugador basado en física e intenciones
void checkState(float delta_time); // Comprueba el estado del jugador y actualiza variables
void setState(State value); // Cambia el estado del jugador
auto canJump() -> bool; // Comprueba si el jugador puede saltar
auto shouldFall() -> bool; // Comprueba si el jugador debe caer
// --- Funciones de física ---
void applyGravity(float delta_time); // Aplica gravedad al jugador
void updateVelocity(); // Actualiza velocidad basada en estado e intenciones
// --- Funciones de movimiento y colisión ---
void move(float delta_time); // Orquesta el movimiento del jugador