treballant en la nova clase Player
This commit is contained in:
@@ -35,66 +35,154 @@ void Player::render() {
|
||||
// Actualiza las variables del objeto
|
||||
void Player::update(float delta_time) {
|
||||
if (!is_paused_) {
|
||||
handleInput(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
|
||||
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(); // 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();
|
||||
updateState();
|
||||
move(delta_time);
|
||||
animate(delta_time);
|
||||
handleBorders();
|
||||
setColor();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::handleInput(float delta_time) {
|
||||
(void)delta_time; // No usado en este método, pero mantenido para consistencia
|
||||
void Player::handleInput() {
|
||||
if (Input::get()->checkAction(InputAction::LEFT)) {
|
||||
wannaGo = Direction::LEFT;
|
||||
} else if (Input::get()->checkAction(InputAction::RIGHT)) {
|
||||
wannaGo = Direction::RIGHT;
|
||||
} else {
|
||||
wannaGo = Direction::STAY;
|
||||
}
|
||||
|
||||
// Solo comprueba las entradas de dirección cuando está sobre una superficie
|
||||
if (state_ != State::STANDING) {
|
||||
wannaJump = Input::get()->checkAction(InputAction::JUMP);
|
||||
}
|
||||
|
||||
void Player::move(float delta_time) {
|
||||
handleHorizontalMovement(delta_time);
|
||||
handleVerticalMovement(delta_time);
|
||||
updateColliderGeometry();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Ahora, aplicamos el movimiento y el flip basado en la velocidad resultante
|
||||
if (vx_ < 0.0F) {
|
||||
moveHorizontal(delta_time, -1);
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
} else if (vx_ > 0.0F) {
|
||||
moveHorizontal(delta_time, 1);
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
}
|
||||
// Si vx_ es 0.0F, no se llama a moveHorizontal, lo cual es correcto.
|
||||
}
|
||||
|
||||
void Player::handleVerticalMovement(float delta_time) {
|
||||
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()->checkAction(InputAction::LEFT)) {
|
||||
vx_ = -HORIZONTAL_VELOCITY;
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
}
|
||||
|
||||
else if (Input::get()->checkAction(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);
|
||||
}
|
||||
if (state_ == State::JUMPING) {
|
||||
applyGravity(delta_time);
|
||||
}
|
||||
|
||||
if (Input::get()->checkAction(InputAction::JUMP)) {
|
||||
// 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
|
||||
// Movimiento vertical
|
||||
if (vy_ < 0.0F) {
|
||||
moveVerticalUp(delta_time);
|
||||
} else if (vy_ > 0.0F) {
|
||||
moveVerticalDown(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
if (isOnFloor() || isOnAutoSurface()) {
|
||||
transitionToState(State::JUMPING);
|
||||
vy_ = JUMP_VELOCITY;
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
}
|
||||
void Player::moveAndCollide(float delta_time) {
|
||||
}
|
||||
|
||||
void Player::handleConveyorBelts() {
|
||||
if (!auto_movement_ and isOnConveyorBelt() and wannaGo == Direction::STAY) {
|
||||
auto_movement_ = true;
|
||||
}
|
||||
|
||||
if (auto_movement_ and !isOnConveyorBelt()) {
|
||||
auto_movement_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::handleShouldFall() {
|
||||
if (!isOnFloor() and state_ == State::STANDING) {
|
||||
transitionToState(State::FALLING);
|
||||
}
|
||||
}
|
||||
|
||||
void Player::transitionToState(State state) {
|
||||
previous_state_ = state_;
|
||||
state_ = state;
|
||||
|
||||
switch (state) {
|
||||
case State::STANDING:
|
||||
vy_ = 0;
|
||||
break;
|
||||
case State::JUMPING:
|
||||
if (previous_state_ == State::STANDING) {
|
||||
vy_ = -MAX_VY;
|
||||
last_grounded_position_ = y_;
|
||||
}
|
||||
break;
|
||||
case State::FALLING:
|
||||
last_grounded_position_ = y_;
|
||||
vy_ = MAX_VY;
|
||||
vx_ = 0.0F;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateState() {
|
||||
switch (state_) {
|
||||
case State::STANDING:
|
||||
handleConveyorBelts();
|
||||
handleShouldFall();
|
||||
if (wannaJump) {
|
||||
transitionToState(State::JUMPING);
|
||||
}
|
||||
break;
|
||||
case State::JUMPING:
|
||||
auto_movement_ = false;
|
||||
// playJumpSound();
|
||||
handleJumpEnd();
|
||||
break;
|
||||
case State::FALLING:
|
||||
auto_movement_ = false;
|
||||
// playFallSound();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +222,7 @@ void Player::handleState(float delta_time) {
|
||||
playFallSound();
|
||||
} else if (state_ == State::STANDING) {
|
||||
// Si no tiene suelo debajo y no está en rampa descendente -> FALLING
|
||||
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope()) {
|
||||
if (!isOnFloor() && !isOnConveyorBelt() && !isOnDownSlope()) {
|
||||
last_grounded_position_ = static_cast<int>(y_); // Guarda Y actual al SALIR de STANDING
|
||||
transitionToState(State::FALLING); // setState() establece vx_=0, vy_=MAX_VY
|
||||
playFallSound();
|
||||
@@ -345,6 +433,7 @@ 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
|
||||
@@ -379,6 +468,7 @@ void Player::move(float delta_time) {
|
||||
// Actualiza la geometría del collider y sprite
|
||||
updateColliderGeometry();
|
||||
}
|
||||
*/
|
||||
|
||||
// Establece la animación del jugador
|
||||
void Player::animate(float delta_time) {
|
||||
@@ -439,7 +529,7 @@ auto Player::isOnFloor() -> bool {
|
||||
// Comprueba las superficies
|
||||
for (auto f : under_feet_) {
|
||||
on_floor |= room_->checkTopSurfaces(&f);
|
||||
on_floor |= room_->checkAutoSurfaces(&f);
|
||||
on_floor |= room_->checkConveyorBelts(&f);
|
||||
}
|
||||
|
||||
// Comprueba las rampas
|
||||
@@ -450,17 +540,17 @@ auto Player::isOnFloor() -> bool {
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta sobre una superficie automática
|
||||
auto Player::isOnAutoSurface() -> bool {
|
||||
bool on_auto_surface = false;
|
||||
auto Player::isOnConveyorBelt() -> bool {
|
||||
bool on_conveyor_belt = false;
|
||||
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
for (auto f : under_feet_) {
|
||||
on_auto_surface |= room_->checkAutoSurfaces(&f);
|
||||
on_conveyor_belt |= room_->checkConveyorBelts(&f);
|
||||
}
|
||||
|
||||
return on_auto_surface;
|
||||
return on_conveyor_belt;
|
||||
}
|
||||
|
||||
// Comprueba si el jugador está sobre una rampa hacia abajo
|
||||
@@ -551,6 +641,7 @@ void Player::updateFeet() {
|
||||
}
|
||||
|
||||
// Cambia el estado del jugador
|
||||
/*
|
||||
void Player::transitionToState(State value) {
|
||||
previous_state_ = state_;
|
||||
state_ = value;
|
||||
@@ -577,6 +668,7 @@ void Player::transitionToState(State value) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Inicializa los sonidos de salto y caida
|
||||
void Player::initSounds() {
|
||||
|
||||
Reference in New Issue
Block a user