magic numbers: game.cpp i player.cpp

This commit is contained in:
2025-09-18 14:17:54 +02:00
parent 0c0518adac
commit 5c82916650
9 changed files with 46 additions and 333 deletions

View File

@@ -60,17 +60,14 @@ void Player::init() {
power_up_counter_ = POWERUP_COUNTER;
extra_hit_ = false;
coffees_ = 0;
continue_ticks_ = 0;
continue_counter_ = 10;
name_entry_ticks_ = 0;
name_entry_idle_counter_ = 0;
name_entry_total_counter_ = 0;
name_entry_idle_time_accumulator_ = 0.0f;
name_entry_total_time_accumulator_ = 0.0f;
shiftColliders();
vel_x_ = 0;
vel_y_ = 0;
score_ = 0;
score_multiplier_ = 1.0F;
cant_fire_counter_ = 10;
enter_name_->init(last_enter_name_);
// Establece la posición del sprite
@@ -149,7 +146,7 @@ void Player::setInputEnteringName(Input::Action action) {
default:
break;
}
name_entry_idle_counter_ = 0;
name_entry_idle_time_accumulator_ = 0.0f;
}
// Mueve el jugador a la posición y animación que le corresponde
@@ -634,189 +631,18 @@ void Player::setAnimation(float deltaTime) {
power_sprite_->update(deltaTime);
}
// Actualiza el valor de la variable (frame-based)
void Player::updateCooldown() {
if (playing_state_ != State::PLAYING) {
return;
}
if (cant_fire_counter_ > 0) {
handleFiringCooldown();
} else {
handleRecoilAndCooling();
}
}
// Fase 2: Actualiza el cooldown de disparo (time-based)
void Player::updateCooldown(float deltaTime) {
if (playing_state_ != State::PLAYING) {
return;
}
if (cant_fire_time_accumulator_ > 0) {
handleFiringCooldown(deltaTime);
} else {
handleRecoilAndCooling(deltaTime);
}
}
void Player::handleFiringCooldown() {
cooling_state_counter_ = COOLING_DURATION;
// Transition to recoiling state at halfway point
if (cant_fire_counter_ == recoiling_state_duration_ / 2) {
transitionToRecoiling();
}
--cant_fire_counter_;
if (cant_fire_counter_ == 0) {
recoiling_state_counter_ = recoiling_state_duration_;
}
}
// Fase 2: Manejo de cooldown de disparo (time-based)
void Player::handleFiringCooldown(float deltaTime) {
// Convertir frames a tiempo en milisegundos
float halfwayTime = static_cast<float>(recoiling_state_duration_) / 2.0f / 60.0f * 1000.0f;
// Reducir tiempo acumulado
cant_fire_time_accumulator_ -= deltaTime;
// Transition to recoiling state at halfway point
if (cant_fire_time_accumulator_ <= halfwayTime && !recoiling_transition_done_) {
transitionToRecoiling();
recoiling_transition_done_ = true;
}
if (cant_fire_time_accumulator_ <= 0) {
cant_fire_time_accumulator_ = 0;
recoiling_time_accumulator_ = static_cast<float>(recoiling_state_duration_) / 60.0f * 1000.0f; // Convert to milliseconds
// Solo inicializar cooling si no está ya activo (para permitir disparos consecutivos)
if (cooling_time_accumulator_ <= 0) {
cooling_time_accumulator_ = static_cast<float>(COOLING_DURATION) / 60.0f * 1000.0f; // Convert to milliseconds
}
recoiling_transition_done_ = false; // Reset flag
}
}
void Player::handleRecoilAndCooling() {
if (recoiling_state_counter_ > 0) {
--recoiling_state_counter_;
return;
}
handleCoolingState();
}
// Fase 2: Manejo de retroceso y enfriamiento (time-based)
void Player::handleRecoilAndCooling(float deltaTime) {
if (recoiling_time_accumulator_ > 0) {
recoiling_time_accumulator_ -= deltaTime;
return;
}
handleCoolingState(deltaTime);
}
void Player::handleCoolingState() {
if (cooling_state_counter_ > COOLING_COMPLETE) {
if (cooling_state_counter_ == COOLING_DURATION) {
transitionToCooling();
}
--cooling_state_counter_;
}
if (cooling_state_counter_ == COOLING_COMPLETE) {
completeCooling();
}
}
// Fase 2: Manejo del estado de enfriamiento (time-based)
void Player::handleCoolingState(float deltaTime) {
float coolingCompleteTime = static_cast<float>(COOLING_COMPLETE); // 0
float coolingDurationTime = static_cast<float>(COOLING_DURATION) / 60.0f * 1000.0f; // Convert to milliseconds
if (cooling_time_accumulator_ > coolingCompleteTime) {
// Transición a cooling cuando empezamos (equivalente a == COOLING_DURATION)
if (!cooling_transition_done_ && cooling_time_accumulator_ >= coolingDurationTime - 16.67f) { // ~1 frame tolerance in ms
transitionToCooling();
cooling_transition_done_ = true;
}
cooling_time_accumulator_ -= deltaTime;
}
if (cooling_time_accumulator_ <= coolingCompleteTime) {
cooling_time_accumulator_ = coolingCompleteTime;
completeCooling();
cooling_transition_done_ = false; // Reset flag
}
}
void Player::transitionToRecoiling() {
switch (firing_state_) {
case State::FIRING_LEFT:
setFiringState(State::RECOILING_LEFT);
break;
case State::FIRING_RIGHT:
setFiringState(State::RECOILING_RIGHT);
break;
case State::FIRING_UP:
setFiringState(State::RECOILING_UP);
break;
default:
break;
}
}
void Player::transitionToCooling() {
switch (firing_state_) {
case State::RECOILING_LEFT:
setFiringState(State::COOLING_LEFT);
break;
case State::RECOILING_RIGHT:
setFiringState(State::COOLING_RIGHT);
break;
case State::RECOILING_UP:
setFiringState(State::COOLING_UP);
break;
default:
break;
}
}
void Player::completeCooling() {
setFiringState(State::FIRING_NONE);
cooling_state_counter_ = -1;
}
// Actualiza al jugador a su posicion, animación y controla los contadores (frame-based)
void Player::update() {
move();
setAnimation();
shiftColliders();
updateFireSystem(16.67f); // Usar nuevo sistema con deltaTime fijo (16.67ms ≈ 1/60s)
updatePowerUp();
updateInvulnerable();
updateScoreboard();
updateContinueCounter();
updateEnterNameCounter();
updateShowingName();
}
// Fase 1-4: Método deltaTime completo
// Actualiza al jugador con deltaTime (time-based)
void Player::update(float deltaTime) {
move(deltaTime); // Sistema de movimiento time-based
setAnimation(deltaTime); // Animaciones time-based
shiftColliders(); // Sin cambios (posicional)
updateFireSystem(deltaTime); // NUEVO: Sistema de disparo de dos líneas
updatePowerUp(deltaTime); // Fase 3: Sistema de power-up time-based
updateInvulnerable(deltaTime); // Fase 3: Sistema de invulnerabilidad time-based
updateFireSystem(deltaTime); // Sistema de disparo de dos líneas
updatePowerUp(deltaTime); // Sistema de power-up time-based
updateInvulnerable(deltaTime); // Sistema de invulnerabilidad time-based
updateScoreboard(); // Sin cambios (no temporal)
updateContinueCounter(deltaTime); // Fase 4: Sistema de continue time-based
updateEnterNameCounter(deltaTime); // Fase 4: Sistema de name entry time-based
updateShowingName(); // Sin cambios (no temporal)
updateContinueCounter(deltaTime); // Sistema de continue time-based
updateEnterNameCounter(deltaTime); // Sistema de name entry time-based
updateShowingName(deltaTime); // Sistema de showing name time-based
}
void Player::passShowingName() {
@@ -886,7 +712,6 @@ void Player::setPlayingState(State state) {
}
case State::CONTINUE: {
// Inicializa el contador de continuar
continue_ticks_ = SDL_GetTicks();
continue_counter_ = 9;
continue_time_accumulator_ = 0.0f; // Initialize time accumulator
playSound("continue_clock.wav");
@@ -919,7 +744,7 @@ void Player::setPlayingState(State state) {
break;
}
case State::SHOWING_NAME: {
showing_name_ticks_ = SDL_GetTicks();
showing_name_time_accumulator_ = 0.0f; // Inicializar acumulador time-based
setScoreboardMode(Scoreboard::Mode::SHOW_NAME);
Scoreboard::get()->setRecordName(scoreboard_panel_, last_enter_name_);
addScoreToScoreBoard();
@@ -1026,37 +851,7 @@ void Player::setInvulnerable(bool value) {
invulnerable_time_accumulator_ = invulnerable_ ? static_cast<float>(INVULNERABLE_COUNTER) / 60.0f * 1000.0f : 0.0f; // Convert frames to milliseconds
}
// Monitoriza el estado (frame-based)
void Player::updateInvulnerable() {
if (playing_state_ == State::PLAYING && invulnerable_) {
if (invulnerable_counter_ > 0) {
--invulnerable_counter_;
// Frecuencia fija de parpadeo (como el original)
constexpr int blink_speed = 8;
// Calcula proporción decreciente: menos textura blanca hacia el final
// Al inicio: 50-50, hacia el final: 70-30 (menos blanco)
float progress = 1.0f - (static_cast<float>(invulnerable_counter_) / INVULNERABLE_COUNTER);
int white_frames = static_cast<int>((0.5f - progress * 0.2f) * blink_speed);
// Alterna entre texturas con proporción variable
bool should_show_invulnerable = (invulnerable_counter_ % blink_speed) < white_frames;
size_t target_texture = should_show_invulnerable ? INVULNERABLE_TEXTURE : coffees_;
// Solo cambia textura si es diferente (optimización)
if (player_sprite_->getActiveTexture() != target_texture) {
player_sprite_->setActiveTexture(target_texture);
}
} else {
// Fin de invulnerabilidad
setInvulnerable(false);
player_sprite_->setActiveTexture(coffees_);
}
}
}
// Fase 3: Monitoriza el estado (time-based)
// Monitoriza el estado (time-based)
void Player::updateInvulnerable(float deltaTime) {
if (playing_state_ == State::PLAYING && invulnerable_) {
if (invulnerable_time_accumulator_ > 0) {
@@ -1095,17 +890,7 @@ void Player::setPowerUp() {
power_up_time_accumulator_ = static_cast<float>(POWERUP_COUNTER) / 60.0f * 1000.0f; // Convert frames to milliseconds
}
// Actualiza el valor de la variable (frame-based)
void Player::updatePowerUp() {
if (playing_state_ == State::PLAYING) {
if (power_up_) {
--power_up_counter_;
power_up_ = power_up_counter_ > 0;
}
}
}
// Fase 3: Actualiza el valor de la variable (time-based)
// Actualiza el valor de la variable (time-based)
void Player::updatePowerUp(float deltaTime) {
if (playing_state_ == State::PLAYING) {
if (power_up_) {
@@ -1150,17 +935,7 @@ void Player::setPlayerTextures(const std::vector<std::shared_ptr<Texture>> &text
power_sprite_->setTexture(texture[1]);
}
// Actualiza el contador de continue (frame-based)
void Player::updateContinueCounter() {
if (playing_state_ == State::CONTINUE) {
constexpr int TICKS_SPEED = 1000;
if (SDL_GetTicks() - continue_ticks_ > TICKS_SPEED) {
decContinueCounter();
}
}
}
// Fase 4: Actualiza el contador de continue (time-based)
// Actualiza el contador de continue (time-based)
void Player::updateContinueCounter(float deltaTime) {
if (playing_state_ == State::CONTINUE) {
continue_time_accumulator_ += deltaTime;
@@ -1172,17 +947,7 @@ void Player::updateContinueCounter(float deltaTime) {
}
}
// Actualiza el contador de entrar nombre (frame-based)
void Player::updateEnterNameCounter() {
if (playing_state_ == State::ENTERING_NAME || playing_state_ == State::ENTERING_NAME_GAME_COMPLETED) {
constexpr int TICKS_SPEED = 1000;
if (SDL_GetTicks() - name_entry_ticks_ > TICKS_SPEED) {
decNameEntryCounter();
}
}
}
// Fase 4: Actualiza el contador de entrar nombre (time-based)
// Actualiza el contador de entrar nombre (time-based)
void Player::updateEnterNameCounter(float deltaTime) {
if (playing_state_ == State::ENTERING_NAME || playing_state_ == State::ENTERING_NAME_GAME_COMPLETED) {
name_entry_time_accumulator_ += deltaTime;
@@ -1194,11 +959,12 @@ void Player::updateEnterNameCounter(float deltaTime) {
}
}
// Actualiza el estado de SHOWING_NAME
void Player::updateShowingName() {
// Actualiza el estado de SHOWING_NAME (time-based)
void Player::updateShowingName(float deltaTime) {
if (playing_state_ == State::SHOWING_NAME) {
constexpr int TICKS_SPEED = 5000;
if (SDL_GetTicks() - name_entry_ticks_ > TICKS_SPEED) {
showing_name_time_accumulator_ += deltaTime;
constexpr float SHOWING_NAME_DURATION = 5000.0f; // 5 segundos en milisegundos
if (showing_name_time_accumulator_ >= SHOWING_NAME_DURATION) {
game_completed_ ? setPlayingState(State::LEAVING_SCREEN) : setPlayingState(State::CONTINUE);
}
}
@@ -1206,7 +972,7 @@ void Player::updateShowingName() {
// Decrementa el contador de continuar
void Player::decContinueCounter() {
continue_ticks_ = SDL_GetTicks();
continue_time_accumulator_ = 0.0f; // Reset time accumulator
--continue_counter_;
if (continue_counter_ < 0) {
setPlayingState(State::CONTINUE_TIME_OUT);
@@ -1217,18 +983,17 @@ void Player::decContinueCounter() {
// Decrementa el contador de entrar nombre
void Player::decNameEntryCounter() {
name_entry_ticks_ = SDL_GetTicks();
name_entry_time_accumulator_ = 0.0f; // Reset time accumulator
// Actualiza contadores
++name_entry_idle_counter_;
++name_entry_total_counter_;
// Incrementa acumuladores de tiempo (1 segundo = 1000ms)
name_entry_idle_time_accumulator_ += 1000.0f;
name_entry_total_time_accumulator_ += 1000.0f;
// Comprueba los contadores
if ((name_entry_total_counter_ >= param.game.name_entry_total_time) ||
(name_entry_idle_counter_ >= param.game.name_entry_idle_time)) {
name_entry_total_counter_ = 0;
name_entry_idle_counter_ = 0;
// Comprueba los acumuladores directamente contra los límites en milisegundos
if ((name_entry_total_time_accumulator_ >= param.game.name_entry_total_time) ||
(name_entry_idle_time_accumulator_ >= param.game.name_entry_idle_time)) {
name_entry_total_time_accumulator_ = 0.0f;
name_entry_idle_time_accumulator_ = 0.0f;
if (playing_state_ == State::ENTERING_NAME) {
last_enter_name_ = getRecordName();
setPlayingState(State::SHOWING_NAME);
@@ -1291,10 +1056,10 @@ void Player::addCredit() {
}
// ========================================
// IMPLEMENTACIÓN DEL NUEVO SISTEMA DE DOS LÍNEAS
// SISTEMA DE DISPARO DE DOS LÍNEAS
// ========================================
// Método principal del nuevo sistema de disparo
// Método principal del sistema de disparo
void Player::updateFireSystem(float deltaTime) {
updateFunctionalLine(deltaTime); // Línea 1: CanFire
updateVisualLine(deltaTime); // Línea 2: Animaciones
@@ -1344,8 +1109,8 @@ void Player::updateVisualLine(float deltaTime) {
}
}
// Inicia un nuevo disparo en ambas líneas
void Player::startFiringNewSystem(int cooldown_frames) {
// Inicia un disparo en ambas líneas
void Player::startFiringSystem(int cooldown_frames) {
// LÍNEA 1: Inicia cooldown funcional
fire_cooldown_timer_ = static_cast<float>(cooldown_frames) / 60.0f * 1000.0f; // Convertir frames a ms
can_fire_new_system_ = false;