magic numbers: game.cpp i player.cpp (en progres)
This commit is contained in:
@@ -473,8 +473,22 @@ void Player::updateStepCounter(float deltaTime) {
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render() {
|
||||
if (power_up_ && isPlaying()) {
|
||||
if (power_up_counter_ > (POWERUP_COUNTER / 4) || power_up_counter_ % 20 > 4) {
|
||||
// Convertir lógica de parpadeo a deltaTime
|
||||
float total_powerup_time_ms = static_cast<float>(POWERUP_COUNTER) / 60.0f * 1000.0f; // Total time in ms
|
||||
float quarter_time_ms = total_powerup_time_ms / 4.0f; // 25% del tiempo total
|
||||
|
||||
if (power_up_time_accumulator_ > quarter_time_ms) {
|
||||
// En los primeros 75% del tiempo, siempre visible
|
||||
power_sprite_->render();
|
||||
} else {
|
||||
// En el último 25%, parpadea cada 20 frames (333ms aprox)
|
||||
constexpr float blink_period_ms = 20.0f / 60.0f * 1000.0f; // 20 frames in ms
|
||||
constexpr float visible_proportion = 4.0f / 20.0f; // 4 frames visible de 20 total
|
||||
|
||||
float cycle_position = fmod(power_up_time_accumulator_, blink_period_ms) / blink_period_ms;
|
||||
if (cycle_position >= visible_proportion) {
|
||||
power_sprite_->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,23 +676,28 @@ void Player::handleFiringCooldown() {
|
||||
|
||||
// Fase 2: Manejo de cooldown de disparo (time-based)
|
||||
void Player::handleFiringCooldown(float deltaTime) {
|
||||
cooling_time_accumulator_ = static_cast<float>(COOLING_DURATION) / 60.0f;
|
||||
// Convertir frames a tiempo en milisegundos
|
||||
float halfwayTime = static_cast<float>(recoiling_state_duration_) / 2.0f / 60.0f * 1000.0f;
|
||||
|
||||
// Convertir frames a tiempo: 1 frame = 1/60 segundos
|
||||
float frameTime = 1.0f / 60.0f;
|
||||
float halfwayTime = static_cast<float>(recoiling_state_duration_) / 2.0f / 60.0f;
|
||||
|
||||
// Reducir tiempo acumulado
|
||||
cant_fire_time_accumulator_ -= deltaTime;
|
||||
|
||||
|
||||
// Transition to recoiling state at halfway point
|
||||
if (cant_fire_time_accumulator_ <= halfwayTime && cant_fire_time_accumulator_ > halfwayTime - frameTime) {
|
||||
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;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -716,14 +735,14 @@ void Player::handleCoolingState() {
|
||||
|
||||
// Fase 2: Manejo del estado de enfriamiento (time-based)
|
||||
void Player::handleCoolingState(float deltaTime) {
|
||||
float coolingCompleteTime = static_cast<float>(COOLING_COMPLETE) / 60.0f;
|
||||
float coolingDurationTime = static_cast<float>(COOLING_DURATION) / 60.0f;
|
||||
float frameTime = 1.0f / 60.0f;
|
||||
|
||||
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_time_accumulator_ <= coolingDurationTime && cooling_time_accumulator_ > coolingDurationTime - frameTime) {
|
||||
if (!cooling_transition_done_ && cooling_time_accumulator_ >= coolingDurationTime - 16.67f) { // ~1 frame tolerance in ms
|
||||
transitionToCooling();
|
||||
cooling_transition_done_ = true;
|
||||
}
|
||||
cooling_time_accumulator_ -= deltaTime;
|
||||
}
|
||||
@@ -731,6 +750,7 @@ void Player::handleCoolingState(float deltaTime) {
|
||||
if (cooling_time_accumulator_ <= coolingCompleteTime) {
|
||||
cooling_time_accumulator_ = coolingCompleteTime;
|
||||
completeCooling();
|
||||
cooling_transition_done_ = false; // Reset flag
|
||||
}
|
||||
}
|
||||
|
||||
@@ -771,12 +791,12 @@ void Player::completeCooling() {
|
||||
cooling_state_counter_ = -1;
|
||||
}
|
||||
|
||||
// Actualiza al jugador a su posicion, animación y controla los contadores
|
||||
// Actualiza al jugador a su posicion, animación y controla los contadores (frame-based)
|
||||
void Player::update() {
|
||||
move();
|
||||
setAnimation();
|
||||
shiftColliders();
|
||||
updateCooldown();
|
||||
updateFireSystem(16.67f); // Usar nuevo sistema con deltaTime fijo (16.67ms ≈ 1/60s)
|
||||
updatePowerUp();
|
||||
updateInvulnerable();
|
||||
updateScoreboard();
|
||||
@@ -788,9 +808,9 @@ void Player::update() {
|
||||
// Fase 1-4: Método deltaTime completo
|
||||
void Player::update(float deltaTime) {
|
||||
move(deltaTime); // Sistema de movimiento time-based
|
||||
setAnimation(deltaTime); // Animaciones time-based
|
||||
setAnimation(deltaTime); // Animaciones time-based
|
||||
shiftColliders(); // Sin cambios (posicional)
|
||||
updateCooldown(deltaTime); // Fase 2: Sistema de disparo time-based
|
||||
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
|
||||
updateScoreboard(); // Sin cambios (no temporal)
|
||||
@@ -1003,7 +1023,7 @@ void Player::decScoreMultiplier() {
|
||||
void Player::setInvulnerable(bool value) {
|
||||
invulnerable_ = value;
|
||||
invulnerable_counter_ = invulnerable_ ? INVULNERABLE_COUNTER : 0;
|
||||
invulnerable_time_accumulator_ = invulnerable_ ? static_cast<float>(INVULNERABLE_COUNTER) / 60.0f : 0.0f; // Initialize time accumulator
|
||||
invulnerable_time_accumulator_ = invulnerable_ ? static_cast<float>(INVULNERABLE_COUNTER) / 60.0f * 1000.0f : 0.0f; // Convert frames to milliseconds
|
||||
}
|
||||
|
||||
// Monitoriza el estado (frame-based)
|
||||
@@ -1042,16 +1062,16 @@ void Player::updateInvulnerable(float deltaTime) {
|
||||
if (invulnerable_time_accumulator_ > 0) {
|
||||
invulnerable_time_accumulator_ -= deltaTime;
|
||||
|
||||
// Frecuencia fija de parpadeo adaptada a deltaTime
|
||||
constexpr float blink_period = 8.0f / 60.0f; // 8 frames convertidos a segundos
|
||||
// Frecuencia fija de parpadeo adaptada a deltaTime (en milisegundos)
|
||||
constexpr float blink_period_ms = 8.0f / 60.0f * 1000.0f; // 8 frames convertidos a ms
|
||||
|
||||
// Calcula proporción decreciente basada en tiempo restante
|
||||
float total_invulnerable_time = static_cast<float>(INVULNERABLE_COUNTER) / 60.0f;
|
||||
float progress = 1.0f - (invulnerable_time_accumulator_ / total_invulnerable_time);
|
||||
float total_invulnerable_time_ms = static_cast<float>(INVULNERABLE_COUNTER) / 60.0f * 1000.0f;
|
||||
float progress = 1.0f - (invulnerable_time_accumulator_ / total_invulnerable_time_ms);
|
||||
float white_proportion = 0.5f - progress * 0.2f; // Menos blanco hacia el final
|
||||
|
||||
// Calcula si debe mostrar textura de invulnerabilidad basado en el ciclo temporal
|
||||
float cycle_position = fmod(invulnerable_time_accumulator_, blink_period) / blink_period;
|
||||
float cycle_position = fmod(invulnerable_time_accumulator_, blink_period_ms) / blink_period_ms;
|
||||
bool should_show_invulnerable = cycle_position < white_proportion;
|
||||
size_t target_texture = should_show_invulnerable ? INVULNERABLE_TEXTURE : coffees_;
|
||||
|
||||
@@ -1072,7 +1092,7 @@ void Player::updateInvulnerable(float deltaTime) {
|
||||
void Player::setPowerUp() {
|
||||
power_up_ = true;
|
||||
power_up_counter_ = POWERUP_COUNTER;
|
||||
power_up_time_accumulator_ = static_cast<float>(POWERUP_COUNTER) / 60.0f; // Initialize time accumulator
|
||||
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)
|
||||
@@ -1144,7 +1164,7 @@ void Player::updateContinueCounter() {
|
||||
void Player::updateContinueCounter(float deltaTime) {
|
||||
if (playing_state_ == State::CONTINUE) {
|
||||
continue_time_accumulator_ += deltaTime;
|
||||
constexpr float CONTINUE_INTERVAL = 1.0f; // 1 segundo
|
||||
constexpr float CONTINUE_INTERVAL = 1000.0f; // 1 segundo en milisegundos
|
||||
if (continue_time_accumulator_ >= CONTINUE_INTERVAL) {
|
||||
continue_time_accumulator_ -= CONTINUE_INTERVAL;
|
||||
decContinueCounter();
|
||||
@@ -1166,7 +1186,7 @@ void Player::updateEnterNameCounter() {
|
||||
void Player::updateEnterNameCounter(float deltaTime) {
|
||||
if (playing_state_ == State::ENTERING_NAME || playing_state_ == State::ENTERING_NAME_GAME_COMPLETED) {
|
||||
name_entry_time_accumulator_ += deltaTime;
|
||||
constexpr float NAME_ENTRY_INTERVAL = 1.0f; // 1 segundo
|
||||
constexpr float NAME_ENTRY_INTERVAL = 1000.0f; // 1 segundo en milisegundos
|
||||
if (name_entry_time_accumulator_ >= NAME_ENTRY_INTERVAL) {
|
||||
name_entry_time_accumulator_ -= NAME_ENTRY_INTERVAL;
|
||||
decNameEntryCounter();
|
||||
@@ -1268,4 +1288,145 @@ void Player::addScoreToScoreBoard() const {
|
||||
void Player::addCredit() {
|
||||
++credits_used_;
|
||||
playSound("credit.wav");
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// IMPLEMENTACIÓN DEL NUEVO SISTEMA DE DOS LÍNEAS
|
||||
// ========================================
|
||||
|
||||
// Método principal del nuevo sistema de disparo
|
||||
void Player::updateFireSystem(float deltaTime) {
|
||||
updateFunctionalLine(deltaTime); // Línea 1: CanFire
|
||||
updateVisualLine(deltaTime); // Línea 2: Animaciones
|
||||
}
|
||||
|
||||
// LÍNEA 1: Sistema Funcional (CanFire)
|
||||
void Player::updateFunctionalLine(float deltaTime) {
|
||||
if (fire_cooldown_timer_ > 0) {
|
||||
fire_cooldown_timer_ -= deltaTime;
|
||||
can_fire_new_system_ = false;
|
||||
} else {
|
||||
fire_cooldown_timer_ = 0; // Evitar valores negativos
|
||||
can_fire_new_system_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// LÍNEA 2: Sistema Visual (Animaciones)
|
||||
void Player::updateVisualLine(float deltaTime) {
|
||||
if (visual_fire_state_ == VisualFireState::NORMAL) {
|
||||
return; // No hay temporizador activo en estado NORMAL
|
||||
}
|
||||
|
||||
visual_state_timer_ -= deltaTime;
|
||||
|
||||
switch (visual_fire_state_) {
|
||||
case VisualFireState::AIMING:
|
||||
if (visual_state_timer_ <= 0) {
|
||||
transitionToRecoilingNew();
|
||||
}
|
||||
break;
|
||||
|
||||
case VisualFireState::RECOILING:
|
||||
if (visual_state_timer_ <= 0) {
|
||||
transitionToThreatPose();
|
||||
}
|
||||
break;
|
||||
|
||||
case VisualFireState::THREAT_POSE:
|
||||
if (visual_state_timer_ <= 0) {
|
||||
transitionToNormalNew();
|
||||
}
|
||||
break;
|
||||
|
||||
case VisualFireState::NORMAL:
|
||||
// Ya manejado arriba
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Inicia un nuevo disparo en ambas líneas
|
||||
void Player::startFiringNewSystem(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;
|
||||
|
||||
// LÍNEA 2: Resetea completamente el estado visual
|
||||
aiming_duration_ = fire_cooldown_timer_ * AIMING_DURATION_FACTOR; // 50% del cooldown
|
||||
recoiling_duration_ = aiming_duration_ * RECOILING_DURATION_MULTIPLIER; // 4 veces la duración de aiming
|
||||
|
||||
visual_fire_state_ = VisualFireState::AIMING;
|
||||
visual_state_timer_ = aiming_duration_;
|
||||
|
||||
updateFiringStateFromVisual(); // Sincroniza firing_state_ para animaciones
|
||||
}
|
||||
|
||||
// Sincroniza firing_state_ con visual_fire_state_
|
||||
void Player::updateFiringStateFromVisual() {
|
||||
// Mantener la dirección actual del disparo
|
||||
State base_state = State::FIRING_NONE;
|
||||
|
||||
if (firing_state_ == State::FIRING_LEFT || firing_state_ == State::RECOILING_LEFT || firing_state_ == State::COOLING_LEFT) {
|
||||
base_state = State::FIRING_LEFT;
|
||||
} else if (firing_state_ == State::FIRING_RIGHT || firing_state_ == State::RECOILING_RIGHT || firing_state_ == State::COOLING_RIGHT) {
|
||||
base_state = State::FIRING_RIGHT;
|
||||
} else if (firing_state_ == State::FIRING_UP || firing_state_ == State::RECOILING_UP || firing_state_ == State::COOLING_UP) {
|
||||
base_state = State::FIRING_UP;
|
||||
}
|
||||
|
||||
switch (visual_fire_state_) {
|
||||
case VisualFireState::NORMAL:
|
||||
firing_state_ = State::FIRING_NONE;
|
||||
break;
|
||||
|
||||
case VisualFireState::AIMING:
|
||||
firing_state_ = base_state; // FIRING_LEFT/RIGHT/UP
|
||||
break;
|
||||
|
||||
case VisualFireState::RECOILING:
|
||||
switch (base_state) {
|
||||
case State::FIRING_LEFT: firing_state_ = State::RECOILING_LEFT; break;
|
||||
case State::FIRING_RIGHT: firing_state_ = State::RECOILING_RIGHT; break;
|
||||
case State::FIRING_UP: firing_state_ = State::RECOILING_UP; break;
|
||||
default: firing_state_ = State::RECOILING_UP; break;
|
||||
}
|
||||
break;
|
||||
|
||||
case VisualFireState::THREAT_POSE:
|
||||
switch (base_state) {
|
||||
case State::FIRING_LEFT: firing_state_ = State::COOLING_LEFT; break;
|
||||
case State::FIRING_RIGHT: firing_state_ = State::COOLING_RIGHT; break;
|
||||
case State::FIRING_UP: firing_state_ = State::COOLING_UP; break;
|
||||
default: firing_state_ = State::COOLING_UP; break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Transiciones del sistema visual
|
||||
void Player::transitionToRecoilingNew() {
|
||||
visual_fire_state_ = VisualFireState::RECOILING;
|
||||
visual_state_timer_ = recoiling_duration_;
|
||||
updateFiringStateFromVisual();
|
||||
}
|
||||
|
||||
void Player::transitionToThreatPose() {
|
||||
visual_fire_state_ = VisualFireState::THREAT_POSE;
|
||||
|
||||
// Calcular threat_pose_duration ajustada:
|
||||
// Duración original (833ms) menos el tiempo extra que ahora dura recoiling
|
||||
float original_recoiling_duration = fire_cooldown_timer_; // Era 100% del cooldown
|
||||
float new_recoiling_duration = aiming_duration_ * RECOILING_DURATION_MULTIPLIER; // Ahora es más del cooldown
|
||||
float extra_recoiling_time = new_recoiling_duration - original_recoiling_duration;
|
||||
float adjusted_threat_duration = THREAT_POSE_DURATION - extra_recoiling_time;
|
||||
|
||||
// Asegurar que no sea negativo
|
||||
visual_state_timer_ = std::max(adjusted_threat_duration, MIN_THREAT_POSE_DURATION);
|
||||
|
||||
updateFiringStateFromVisual();
|
||||
}
|
||||
|
||||
void Player::transitionToNormalNew() {
|
||||
visual_fire_state_ = VisualFireState::NORMAL;
|
||||
visual_state_timer_ = 0;
|
||||
updateFiringStateFromVisual();
|
||||
}
|
||||
Reference in New Issue
Block a user