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;
}

View File

@@ -31,6 +31,23 @@ class Item {
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
static constexpr float LIFETIME_DURATION_MS = 10000.0f; // Duración de vida del ítem (600 frames a 60fps)
// Velocidades base (pixels/ms) - Coffee Machine
static constexpr float COFFEE_MACHINE_VEL_X_FACTOR = 0.03F; // Factor para velocidad X de máquina de café
static constexpr float COFFEE_MACHINE_VEL_Y = -0.006F; // Velocidad Y inicial de máquina de café
static constexpr float COFFEE_MACHINE_ACCEL_Y = 0.00036F; // Aceleración Y de máquina de café (pixels/ms²)
// Velocidades base (pixels/ms) - Items normales (del plan: vel_x: ±1.0F→±0.06F, vel_y: -4.0F→-0.24F, accel_y: 0.2F→0.012F)
static constexpr float ITEM_VEL_X_BASE = 0.06F; // Velocidad X base para items (1.0F/16.67)
static constexpr float ITEM_VEL_X_STEP = 0.02F; // Incremento de velocidad X (0.33F/16.67)
static constexpr float ITEM_VEL_Y = -0.24F; // Velocidad Y inicial de items (-4.0F/16.67)
static constexpr float ITEM_ACCEL_Y = 0.00072F; // Aceleración Y de items (pixels/ms²) - 0.2F * (60/1000)²
// Constantes de física de rebote
static constexpr float BOUNCE_VEL_THRESHOLD = 0.06F; // Umbral de velocidad para parar (1.0F/16.67)
static constexpr float COFFEE_BOUNCE_DAMPING = -0.20F; // Factor de rebote Y para máquina de café
static constexpr float ITEM_BOUNCE_DAMPING = -0.5F; // Factor de rebote Y para items normales
static constexpr float HORIZONTAL_DAMPING = 0.75F; // Factor de amortiguación horizontal
// --- Constructor y destructor ---
Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation); // Constructor principal
~Item() = default; // Destructor