implementada la logica de so (time based) en Player per a imitar la anterior frame based (amb els seus fallos)
This commit is contained in:
@@ -35,7 +35,7 @@ void Player::render() {
|
||||
void Player::update(float delta_time) {
|
||||
if (!is_paused_) {
|
||||
handleInput();
|
||||
updateState();
|
||||
updateState(delta_time);
|
||||
move(delta_time);
|
||||
animate(delta_time);
|
||||
handleBorders();
|
||||
@@ -122,23 +122,29 @@ void Player::transitionToState(State state) {
|
||||
case State::STANDING:
|
||||
vy_ = 0;
|
||||
handleDeathByFalling();
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.reset();
|
||||
break;
|
||||
case State::JUMPING:
|
||||
if (previous_state_ == State::STANDING) {
|
||||
vy_ = -MAX_VY;
|
||||
last_grounded_position_ = y_;
|
||||
updateVelocity();
|
||||
jump_sound_ctrl_.start();
|
||||
}
|
||||
break;
|
||||
case State::FALLING:
|
||||
last_grounded_position_ = y_;
|
||||
fall_start_position_ = static_cast<int>(y_);
|
||||
last_grounded_position_ = static_cast<int>(y_);
|
||||
vy_ = MAX_VY;
|
||||
vx_ = 0.0F;
|
||||
jump_sound_ctrl_.reset();
|
||||
fall_sound_ctrl_.start(y_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateState() {
|
||||
void Player::updateState(float delta_time) {
|
||||
switch (state_) {
|
||||
case State::STANDING:
|
||||
handleConveyorBelts();
|
||||
@@ -149,12 +155,12 @@ void Player::updateState() {
|
||||
break;
|
||||
case State::JUMPING:
|
||||
auto_movement_ = false;
|
||||
// playJumpSound();
|
||||
playJumpSound(delta_time);
|
||||
handleJumpEnd();
|
||||
break;
|
||||
case State::FALLING:
|
||||
auto_movement_ = false;
|
||||
// playFallSound();
|
||||
playFallSound(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -188,20 +194,18 @@ void Player::handleBorders() {
|
||||
|
||||
// Comprueba el estado del jugador
|
||||
void Player::handleState(float delta_time) {
|
||||
(void)delta_time; // No usado actualmente
|
||||
|
||||
// Reproduce sonidos según el estado
|
||||
if (state_ == State::FALLING) {
|
||||
playFallSound();
|
||||
playFallSound(delta_time);
|
||||
} else if (state_ == State::STANDING) {
|
||||
// Si no tiene suelo debajo y no está en rampa descendente -> FALLING
|
||||
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();
|
||||
playFallSound(delta_time);
|
||||
}
|
||||
} else if (state_ == State::JUMPING) {
|
||||
playJumpSound();
|
||||
playJumpSound(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -438,35 +442,26 @@ void Player::handleJumpEnd() {
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula y reproduce el sonido de salto basado en distancia vertical recorrida
|
||||
void Player::playJumpSound() {
|
||||
// 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);
|
||||
|
||||
// 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())) {
|
||||
Audio::get()->playSound(jumping_sound_[SOUND_INDEX], Audio::Group::GAME);
|
||||
// Calcula y reproduce el sonido de salto basado en tiempo transcurrido
|
||||
void Player::playJumpSound(float delta_time) {
|
||||
size_t sound_index;
|
||||
if (jump_sound_ctrl_.shouldPlay(delta_time, sound_index)) {
|
||||
if (sound_index < jumping_sound_.size()) {
|
||||
Audio::get()->playSound(jumping_sound_[sound_index], Audio::Group::GAME);
|
||||
std::cout << sound_index << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Calcula y reproduce el sonido de caída basado en distancia vertical recorrida
|
||||
void Player::playFallSound() {
|
||||
// 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);
|
||||
|
||||
// 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 cuando cambia de índice (nuevo hito alcanzado)
|
||||
if (SOUND_INDEX != PREVIOUS_INDEX && SOUND_INDEX < static_cast<int>(falling_sound_.size())) {
|
||||
Audio::get()->playSound(falling_sound_[SOUND_INDEX], Audio::Group::GAME);
|
||||
void Player::playFallSound(float delta_time) {
|
||||
size_t sound_index;
|
||||
if (fall_sound_ctrl_.shouldPlay(delta_time, y_, sound_index)) {
|
||||
if (sound_index < falling_sound_.size()) {
|
||||
Audio::get()->playSound(falling_sound_[sound_index], Audio::Group::GAME);
|
||||
std::cout << sound_index << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,19 +568,99 @@ void Player::updateFeet() {
|
||||
|
||||
// Inicializa los sonidos de salto y caida
|
||||
void Player::initSounds() {
|
||||
jumping_sound_.clear();
|
||||
falling_sound_.clear();
|
||||
for (int i = 0; i < 24; ++i) {
|
||||
std::string sound_file = "jump" + std::to_string(i + 1) + ".wav";
|
||||
jumping_sound_[i] = Resource::get()->getSound(sound_file);
|
||||
|
||||
for (int i = 1; i <= 24; ++i) {
|
||||
std::string sound_file = "jump" + std::to_string(i) + ".wav";
|
||||
jumping_sound_.push_back(Resource::get()->getSound(sound_file));
|
||||
|
||||
if (i >= 11) {
|
||||
falling_sound_.push_back(Resource::get()->getSound(sound_file));
|
||||
if (i >= 10) { // i+1 >= 11
|
||||
falling_sound_[i - 10] = Resource::get()->getSound(sound_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::start
|
||||
void Player::JumpSoundController::start() {
|
||||
current_index_ = 0;
|
||||
elapsed_time_ = 0.0F;
|
||||
active_ = true;
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::reset
|
||||
void Player::JumpSoundController::reset() {
|
||||
active_ = false;
|
||||
current_index_ = 0;
|
||||
elapsed_time_ = 0.0F;
|
||||
}
|
||||
|
||||
// Implementación de JumpSoundController::shouldPlay
|
||||
auto Player::JumpSoundController::shouldPlay(float delta_time, size_t& out_index) -> bool {
|
||||
if (!active_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Acumula el tiempo transcurrido durante el salto
|
||||
elapsed_time_ += delta_time;
|
||||
|
||||
// Calcula qué sonido debería estar sonando según el tiempo
|
||||
size_t target_index = FIRST_SOUND + static_cast<size_t>(elapsed_time_ / SECONDS_PER_SOUND);
|
||||
target_index = std::min(target_index, LAST_SOUND);
|
||||
|
||||
// Reproduce si hemos avanzado a un nuevo sonido
|
||||
if (target_index > current_index_) {
|
||||
current_index_ = target_index;
|
||||
out_index = current_index_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::start
|
||||
void Player::FallSoundController::start(float start_y) {
|
||||
current_index_ = 0;
|
||||
distance_traveled_ = 0.0F;
|
||||
last_y_ = start_y;
|
||||
active_ = true;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::reset
|
||||
void Player::FallSoundController::reset() {
|
||||
active_ = false;
|
||||
current_index_ = 0;
|
||||
distance_traveled_ = 0.0F;
|
||||
}
|
||||
|
||||
// Implementación de FallSoundController::shouldPlay
|
||||
auto Player::FallSoundController::shouldPlay(float delta_time, float current_y, size_t& out_index) -> bool {
|
||||
(void)delta_time; // No usado actualmente, pero recibido por consistencia
|
||||
|
||||
if (!active_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Acumula la distancia recorrida (solo hacia abajo)
|
||||
if (current_y > last_y_) {
|
||||
distance_traveled_ += (current_y - last_y_);
|
||||
}
|
||||
last_y_ = current_y;
|
||||
|
||||
// Calcula qué sonido debería estar sonando según el intervalo
|
||||
size_t target_index = FIRST_SOUND + static_cast<size_t>(distance_traveled_ / PIXELS_PER_SOUND);
|
||||
|
||||
// El sonido a reproducir se limita a LAST_SOUND (13), pero el índice interno sigue creciendo
|
||||
size_t sound_to_play = std::min(target_index, LAST_SOUND);
|
||||
|
||||
// Reproduce si hemos avanzado a un nuevo índice (permite repetición de sonido 13)
|
||||
if (target_index > current_index_) {
|
||||
current_index_ = target_index; // Guardamos el índice real (puede ser > LAST_SOUND)
|
||||
out_index = sound_to_play; // Pero reproducimos LAST_SOUND cuando corresponde
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Aplica los valores de spawn al jugador
|
||||
void Player::applySpawnValues(const SpawnData& spawn) {
|
||||
x_ = spawn.x;
|
||||
|
||||
Reference in New Issue
Block a user