forked from jaildesigner-jailgames/jaildoctors_dilemma
refactor(player): Simplificar structs y agregar inicialización en clase
- Eliminar constructores parametrizados redundantes de SpawnData y Data structs - Agregar inicialización en declaración para collider_box_, jumping_sound_, falling_sound_ - Renombrar parámetro de transitionToState() para consistencia (value → state) - Actualizar game.cpp para usar aggregate initialization de Player::Data - Refactorizar sistema de estados del jugador con métodos por estado Archivos modificados: - source/game/entities/player.hpp: Simplificación de structs e inicialización - source/game/entities/player.cpp: Refactoring del sistema de estados - source/game/scenes/game.cpp: Actualización de construcción del Player 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -55,14 +55,37 @@ void Player::handleInput() {
|
||||
wannaJump = Input::get()->checkAction(InputAction::JUMP);
|
||||
}
|
||||
|
||||
// ANTIGUO move() - Comentado durante migración a paradigma de estados
|
||||
/*
|
||||
void Player::move(float delta_time) {
|
||||
handleHorizontalMovement(delta_time);
|
||||
handleVerticalMovement(delta_time);
|
||||
updateColliderGeometry();
|
||||
}
|
||||
*/
|
||||
|
||||
// NUEVO: La lógica de movimiento está distribuida en move*() y se llama desde update*()
|
||||
void Player::move(float delta_time) {
|
||||
// Este método ahora es un dispatcher que llama al método de movimiento correspondiente
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
moveOnGround(delta_time);
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
moveOnSlope(delta_time);
|
||||
break;
|
||||
case State::JUMPING:
|
||||
moveJumping(delta_time);
|
||||
break;
|
||||
case State::FALLING:
|
||||
moveFalling(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::handleHorizontalMovement(float delta_time) {
|
||||
if (state_ == State::STANDING) {
|
||||
// TODO: Verificar si esto debe aplicar solo a ON_GROUND o también ON_SLOPE
|
||||
if (state_ == State::ON_GROUND || state_ == State::ON_SLOPE) {
|
||||
// Determinama cuál debe ser la velocidad a partir de automovement o de wannaGo
|
||||
updateVelocity();
|
||||
}
|
||||
@@ -78,7 +101,8 @@ void Player::handleHorizontalMovement(float delta_time) {
|
||||
}
|
||||
|
||||
void Player::handleVerticalMovement(float delta_time) {
|
||||
if (state_ == State::STANDING) {
|
||||
// No hay movimiento vertical en estados terrestres
|
||||
if (state_ == State::ON_GROUND || state_ == State::ON_SLOPE) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -105,7 +129,8 @@ void Player::handleConveyorBelts() {
|
||||
}
|
||||
|
||||
void Player::handleShouldFall() {
|
||||
if (!isOnFloor() and state_ == State::STANDING) {
|
||||
// TODO: También verificar ON_SLOPE
|
||||
if (!isOnFloor() and (state_ == State::ON_GROUND || state_ == State::ON_SLOPE)) {
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
}
|
||||
@@ -115,14 +140,21 @@ void Player::transitionToState(State state) {
|
||||
state_ = state;
|
||||
|
||||
switch (state) {
|
||||
case State::STANDING:
|
||||
case State::ON_GROUND:
|
||||
vy_ = 0;
|
||||
handleDeathByFalling();
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.reset();
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
vy_ = 0;
|
||||
handleDeathByFalling();
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.reset();
|
||||
break;
|
||||
case State::JUMPING:
|
||||
if (previous_state_ == State::STANDING) {
|
||||
// Puede saltar desde ON_GROUND o ON_SLOPE
|
||||
if (previous_state_ == State::ON_GROUND || previous_state_ == State::ON_SLOPE) {
|
||||
vy_ = -MAX_VY;
|
||||
last_grounded_position_ = y_;
|
||||
updateVelocity();
|
||||
@@ -140,6 +172,29 @@ void Player::transitionToState(State state) {
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::ON_GROUND:
|
||||
updateOnGround(delta_time);
|
||||
break;
|
||||
case State::ON_SLOPE:
|
||||
updateOnSlope(delta_time);
|
||||
break;
|
||||
case State::JUMPING:
|
||||
updateJumping(delta_time);
|
||||
break;
|
||||
case State::FALLING:
|
||||
updateFalling(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// NUEVO PARADIGMA: Métodos de actualización por estado
|
||||
// ============================================================================
|
||||
|
||||
// ANTIGUO updateState() - Comentado durante migración
|
||||
/*
|
||||
void Player::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::STANDING:
|
||||
@@ -160,6 +215,101 @@ void Player::updateState(float delta_time) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Actualización lógica del estado ON_GROUND
|
||||
void Player::updateOnGround(float delta_time) {
|
||||
(void)delta_time; // No usado en este método, pero se mantiene por consistencia
|
||||
|
||||
// Gestiona las cintas transportadoras
|
||||
handleConveyorBelts();
|
||||
|
||||
// Verifica si debe caer (no tiene suelo)
|
||||
handleShouldFall();
|
||||
|
||||
// Verifica si el jugador quiere saltar
|
||||
if (wannaJump) {
|
||||
transitionToState(State::JUMPING);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualización lógica del estado ON_SLOPE
|
||||
void Player::updateOnSlope(float delta_time) {
|
||||
(void)delta_time; // No usado en este método, pero se mantiene por consistencia
|
||||
|
||||
// Verifica si debe caer (no tiene suelo)
|
||||
handleShouldFall();
|
||||
|
||||
// Verifica si el jugador quiere saltar
|
||||
if (wannaJump) {
|
||||
transitionToState(State::JUMPING);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualización lógica del estado JUMPING
|
||||
void Player::updateJumping(float delta_time) {
|
||||
// Desactiva el movimiento automático durante el salto
|
||||
auto_movement_ = false;
|
||||
|
||||
// Reproduce los sonidos de salto
|
||||
playJumpSound(delta_time);
|
||||
|
||||
// Verifica si el salto ha terminado (alcanzó la altura inicial)
|
||||
handleJumpEnd();
|
||||
}
|
||||
|
||||
// Actualización lógica del estado FALLING
|
||||
void Player::updateFalling(float delta_time) {
|
||||
// Desactiva el movimiento automático durante la caída
|
||||
auto_movement_ = false;
|
||||
|
||||
// Reproduce los sonidos de caída
|
||||
playFallSound(delta_time);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// NUEVO PARADIGMA: Métodos de movimiento por estado
|
||||
// ============================================================================
|
||||
|
||||
// Movimiento físico del estado ON_GROUND
|
||||
void Player::moveOnGround(float delta_time) {
|
||||
// Movimiento horizontal en suelo plano (migrado de handleHorizontalMovement)
|
||||
handleHorizontalMovement(delta_time);
|
||||
|
||||
// Actualiza geometría de colisión
|
||||
updateColliderGeometry();
|
||||
}
|
||||
|
||||
// Movimiento físico del estado ON_SLOPE
|
||||
void Player::moveOnSlope(float delta_time) {
|
||||
// Movimiento horizontal en rampa (migrado de handleHorizontalMovement)
|
||||
// handleSlopeMovement() ya se llama desde dentro de moveHorizontal()
|
||||
handleHorizontalMovement(delta_time);
|
||||
|
||||
// Actualiza geometría de colisión
|
||||
updateColliderGeometry();
|
||||
}
|
||||
|
||||
// Movimiento físico del estado JUMPING
|
||||
void Player::moveJumping(float delta_time) {
|
||||
// Movimiento horizontal (migrado de handleHorizontalMovement)
|
||||
handleHorizontalMovement(delta_time);
|
||||
|
||||
// Movimiento vertical (migrado de handleVerticalMovement)
|
||||
handleVerticalMovement(delta_time);
|
||||
|
||||
// Actualiza geometría de colisión
|
||||
updateColliderGeometry();
|
||||
}
|
||||
|
||||
// Movimiento físico del estado FALLING
|
||||
void Player::moveFalling(float delta_time) {
|
||||
// Movimiento vertical (migrado de handleVerticalMovement)
|
||||
handleVerticalMovement(delta_time);
|
||||
|
||||
// Actualiza geometría de colisión
|
||||
updateColliderGeometry();
|
||||
}
|
||||
|
||||
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||
void Player::handleBorders() {
|
||||
@@ -193,7 +343,7 @@ void Player::handleState(float delta_time) {
|
||||
// Reproduce sonidos según el estado
|
||||
if (state_ == State::FALLING) {
|
||||
playFallSound(delta_time);
|
||||
} else if (state_ == State::STANDING) {
|
||||
} else if (state_ == State::ON_GROUND || state_ == State::ON_SLOPE) {
|
||||
// Si no tiene suelo debajo y no está en rampa descendente -> FALLING
|
||||
if (!isOnFloor() && !isOnConveyorBelt() && !isOnDownSlope()) {
|
||||
transitionToState(State::FALLING); // setState() establece vx_=0, vy_=MAX_VY
|
||||
@@ -208,12 +358,12 @@ void Player::switchBorders() {
|
||||
switch (border_) {
|
||||
case Room::Border::TOP:
|
||||
y_ = PLAY_AREA_BOTTOM - HEIGHT - TILE_SIZE;
|
||||
transitionToState(State::STANDING);
|
||||
transitionToState(State::ON_GROUND); // TODO: Detectar si debe ser ON_SLOPE
|
||||
break;
|
||||
|
||||
case Room::Border::BOTTOM:
|
||||
y_ = PLAY_AREA_TOP;
|
||||
transitionToState(State::STANDING);
|
||||
transitionToState(State::ON_GROUND); // TODO: Detectar si debe ser ON_SLOPE
|
||||
break;
|
||||
|
||||
case Room::Border::RIGHT:
|
||||
@@ -379,7 +529,7 @@ void Player::moveVerticalDown(float delta_time) {
|
||||
if (POS != Collision::NONE) {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||
y_ = POS - HEIGHT;
|
||||
transitionToState(State::STANDING);
|
||||
transitionToState(State::ON_GROUND); // Aterrizó en superficie plana o conveyor belt
|
||||
} 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
|
||||
@@ -393,7 +543,7 @@ void Player::moveVerticalDown(float delta_time) {
|
||||
// No está saltando y hay colisión con una rampa
|
||||
// Calcula la nueva posición
|
||||
y_ = POINT - HEIGHT;
|
||||
transitionToState(State::STANDING);
|
||||
transitionToState(State::ON_SLOPE); // Aterrizó en rampa
|
||||
} else {
|
||||
// No está saltando y no hay colisón con una rampa
|
||||
// Calcula la nueva posición
|
||||
|
||||
Reference in New Issue
Block a user