magic numbers: item.cpp

This commit is contained in:
2025-09-22 13:14:23 +02:00
parent 91b26631c6
commit 9edfe6877f
2 changed files with 36 additions and 21 deletions

View File

@@ -19,9 +19,9 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
height_ = COFFEE_MACHINE_HEIGHT;
pos_x_ = getCoffeeMachineSpawn(x, width_, play_area_.w);
pos_y_ = y;
vel_x_ = ((rand() % 3) - 1) * 0.5F;
vel_y_ = -0.1F;
accel_y_ = 0.1F;
vel_x_ = ((rand() % 3) - 1) * COFFEE_MACHINE_VEL_X_FACTOR;
vel_y_ = COFFEE_MACHINE_VEL_Y;
accel_y_ = COFFEE_MACHINE_ACCEL_Y;
collider_.r = 10;
break;
}
@@ -34,13 +34,13 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
const int direction = rand() % 6;
if (direction < 3) {
// Velocidades negativas: -1.0, -0.66, -0.33
vel_x_ = -1.0F + (direction * 0.33F);
vel_x_ = -ITEM_VEL_X_BASE + (direction * ITEM_VEL_X_STEP);
} else {
// Velocidades positivas: 0.33, 0.66, 1.0
vel_x_ = 0.33F + ((direction - 3) * 0.33F);
vel_x_ = ITEM_VEL_X_STEP + ((direction - 3) * ITEM_VEL_X_STEP);
}
vel_y_ = -4.0F;
accel_y_ = 0.2F;
vel_y_ = ITEM_VEL_Y;
accel_y_ = ITEM_ACCEL_Y;
collider_.r = width_ / 2;
break;
}
@@ -86,17 +86,15 @@ void Item::render() {
}
void Item::move(float deltaTime) {
// Convertir deltaTime a factor de frame para compatibilidad (asumiendo 60fps)
const float frameFactor = deltaTime / (1000.0f / 60.0f);
floor_collision_ = false;
// Calcula la nueva posición (time-based)
pos_x_ += vel_x_ * frameFactor;
pos_y_ += vel_y_ * frameFactor;
// Calcula la nueva posición usando deltaTime puro (velocidad en pixels/ms)
pos_x_ += vel_x_ * deltaTime;
pos_y_ += vel_y_ * deltaTime;
// Aplica las aceleraciones a la velocidad (time-based)
vel_x_ += accel_x_ * frameFactor;
vel_y_ += accel_y_ * frameFactor;
// Aplica las aceleraciones a la velocidad usando deltaTime puro (aceleración en pixels/ms²)
vel_x_ += accel_x_ * deltaTime;
vel_y_ += accel_y_ * deltaTime;
// Comprueba los laterales de la zona de juego
const float MIN_X = param.game.play_area.rect.x;
@@ -126,24 +124,24 @@ void Item::move(float deltaTime) {
case ItemType::COFFEE_MACHINE:
// La máquina de café es mas pesada y tiene una fisica diferente, ademas hace ruido
floor_collision_ = true;
if (vel_y_ < 1.0F) {
if (std::abs(vel_y_) < BOUNCE_VEL_THRESHOLD) {
// Si la velocidad vertical es baja, detiene el objeto
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
} else {
// Si la velocidad vertical es alta, el objeto rebota y pierde velocidad
vel_y_ *= -0.20F;
vel_x_ *= 0.75F;
vel_y_ *= COFFEE_BOUNCE_DAMPING;
vel_x_ *= HORIZONTAL_DAMPING;
}
break;
default:
// Si no es una máquina de café
if (vel_y_ < 1.0F) {
if (std::abs(vel_y_) < BOUNCE_VEL_THRESHOLD) {
// Si la velocidad vertical es baja, detiene el objeto
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
} else {
// Si la velocidad vertical es alta, el objeto rebota y pierde velocidad
vel_y_ *= -0.5F;
vel_x_ *= 0.75F;
vel_y_ *= ITEM_BOUNCE_DAMPING;
vel_x_ *= HORIZONTAL_DAMPING;
}
break;
}