forked from jaildesigner-jailgames/jaildoctors_dilemma
treballant en la classe Player
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user