MODO REAL FULLSCREEN (F4): - F4: Cambia resolución interna a resolución nativa del escritorio - Pelotas usan dimensiones dinámicas del terreno de juego - Interfaz se adapta automáticamente (texto, debug, gradiente) - Reinicio automático de escena con nuevas dimensiones RESOLUCIÓN DINÁMICA: - Ball constructor acepta screen_width/height como parámetros - Colisiones usan dimensiones dinámicas en lugar de constantes - Spawn de pelotas usa margen configurable (BALL_SPAWN_MARGIN) - Toda la interfaz se adapta a resolución actual MODOS FULLSCREEN MUTUAMENTE EXCLUYENTES: - F3 (fullscreen normal) y F4 (real fullscreen) se desactivan mutuamente - F1/F2 (zoom) bloqueados durante cualquier modo fullscreen - Sin estados mixtos que rompan el renderizado - Transiciones seguras entre todos los modos MEJORAS DE CONFIGURACIÓN: - BALL_SPAWN_MARGIN: margen lateral configurable para spawn de pelotas - Resolución base actualizada a 640x360 (16:9) - Spawn margin reducido a 15% para mayor dispersión 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
252 lines
9.2 KiB
C++
252 lines
9.2 KiB
C++
#include "ball.h"
|
|
|
|
#include <stdlib.h> // for rand
|
|
|
|
#include <cmath> // for fabs
|
|
|
|
#include "defines.h" // for BALL_SIZE, Color, SCREEN_HEIGHT, GRAVITY_FORCE
|
|
class Texture;
|
|
|
|
// Función auxiliar para generar pérdida aleatoria en rebotes
|
|
float generateBounceVariation() {
|
|
// Genera un valor entre 0 y BOUNCE_RANDOM_LOSS_PERCENT (solo pérdida adicional)
|
|
float loss = (rand() % 1000) / 1000.0f * BOUNCE_RANDOM_LOSS_PERCENT;
|
|
return 1.0f - loss; // Retorna multiplicador (ej: 0.90 - 1.00 para 10% max pérdida)
|
|
}
|
|
|
|
// Función auxiliar para generar pérdida lateral aleatoria
|
|
float generateLateralLoss() {
|
|
// Genera un valor entre 0 y LATERAL_LOSS_PERCENT
|
|
float loss = (rand() % 1000) / 1000.0f * LATERAL_LOSS_PERCENT;
|
|
return 1.0f - loss; // Retorna multiplicador (ej: 0.98 - 1.0 para 0-2% pérdida)
|
|
}
|
|
|
|
// Constructor
|
|
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, GravityDirection gravity_dir, float mass_factor)
|
|
: sprite_(std::make_unique<Sprite>(texture)),
|
|
pos_({x, 0.0f, BALL_SIZE, BALL_SIZE}) {
|
|
// Convertir velocidades de píxeles/frame a píxeles/segundo (multiplicar por 60)
|
|
vx_ = vx * 60.0f;
|
|
vy_ = vy * 60.0f;
|
|
sprite_->setPos({pos_.x, pos_.y});
|
|
sprite_->setSize(BALL_SIZE, BALL_SIZE);
|
|
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
|
|
color_ = color;
|
|
// Convertir gravedad de píxeles/frame² a píxeles/segundo² (multiplicar por 60²)
|
|
gravity_force_ = GRAVITY_FORCE * 60.0f * 60.0f;
|
|
gravity_mass_factor_ = mass_factor; // Factor de masa individual para esta pelota
|
|
gravity_direction_ = gravity_dir;
|
|
screen_width_ = screen_width; // Dimensiones del terreno de juego
|
|
screen_height_ = screen_height;
|
|
on_surface_ = false;
|
|
stopped_ = false;
|
|
// Coeficiente base IGUAL para todas las pelotas (solo variación por rebote individual)
|
|
loss_ = BASE_BOUNCE_COEFFICIENT; // Coeficiente fijo para todas las pelotas
|
|
}
|
|
|
|
// Actualiza la lógica de la clase
|
|
void Ball::update(float deltaTime) {
|
|
if (stopped_) {
|
|
return;
|
|
}
|
|
|
|
// Aplica la gravedad según la dirección (píxeles/segundo²)
|
|
if (!on_surface_) {
|
|
// Aplicar gravedad multiplicada por factor de masa individual
|
|
float effective_gravity = gravity_force_ * gravity_mass_factor_ * deltaTime;
|
|
switch (gravity_direction_) {
|
|
case GravityDirection::DOWN:
|
|
vy_ += effective_gravity;
|
|
break;
|
|
case GravityDirection::UP:
|
|
vy_ -= effective_gravity;
|
|
break;
|
|
case GravityDirection::LEFT:
|
|
vx_ -= effective_gravity;
|
|
break;
|
|
case GravityDirection::RIGHT:
|
|
vx_ += effective_gravity;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Actualiza la posición en función de la velocidad (píxeles/segundo)
|
|
if (!on_surface_) {
|
|
pos_.x += vx_ * deltaTime;
|
|
pos_.y += vy_ * deltaTime;
|
|
} else {
|
|
// Si está en superficie, mantener posición según dirección de gravedad
|
|
switch (gravity_direction_) {
|
|
case GravityDirection::DOWN:
|
|
pos_.y = screen_height_ - pos_.h;
|
|
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
|
|
break;
|
|
case GravityDirection::UP:
|
|
pos_.y = 0;
|
|
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
|
|
break;
|
|
case GravityDirection::LEFT:
|
|
pos_.x = 0;
|
|
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
|
|
break;
|
|
case GravityDirection::RIGHT:
|
|
pos_.x = screen_width_ - pos_.w;
|
|
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Comprueba las colisiones con el lateral izquierdo
|
|
if (pos_.x < 0) {
|
|
pos_.x = 0;
|
|
if (gravity_direction_ == GravityDirection::LEFT) {
|
|
// Colisión con superficie de gravedad - aplicar variación aleatoria
|
|
vx_ = -vx_ * loss_ * generateBounceVariation();
|
|
if (std::fabs(vx_) < 6.0f) {
|
|
vx_ = 0.0f;
|
|
on_surface_ = true;
|
|
}
|
|
} else {
|
|
// Rebote normal - con pérdida lateral aleatoria
|
|
vx_ = -vx_ * generateLateralLoss();
|
|
}
|
|
// Pérdida lateral en velocidad vertical también
|
|
vy_ *= generateLateralLoss();
|
|
}
|
|
|
|
// Comprueba las colisiones con el lateral derecho
|
|
if (pos_.x + pos_.w > screen_width_) {
|
|
pos_.x = screen_width_ - pos_.w;
|
|
if (gravity_direction_ == GravityDirection::RIGHT) {
|
|
// Colisión con superficie de gravedad - aplicar variación aleatoria
|
|
vx_ = -vx_ * loss_ * generateBounceVariation();
|
|
if (std::fabs(vx_) < 6.0f) {
|
|
vx_ = 0.0f;
|
|
on_surface_ = true;
|
|
}
|
|
} else {
|
|
// Rebote normal - con pérdida lateral aleatoria
|
|
vx_ = -vx_ * generateLateralLoss();
|
|
}
|
|
// Pérdida lateral en velocidad vertical también
|
|
vy_ *= generateLateralLoss();
|
|
}
|
|
|
|
// Comprueba las colisiones con la parte superior
|
|
if (pos_.y < 0) {
|
|
pos_.y = 0;
|
|
if (gravity_direction_ == GravityDirection::UP) {
|
|
// Colisión con superficie de gravedad - aplicar variación aleatoria
|
|
vy_ = -vy_ * loss_ * generateBounceVariation();
|
|
if (std::fabs(vy_) < 6.0f) {
|
|
vy_ = 0.0f;
|
|
on_surface_ = true;
|
|
}
|
|
} else {
|
|
// Rebote normal - con pérdida lateral aleatoria
|
|
vy_ = -vy_ * generateLateralLoss();
|
|
}
|
|
// Pérdida lateral en velocidad horizontal también
|
|
vx_ *= generateLateralLoss();
|
|
}
|
|
|
|
// Comprueba las colisiones con la parte inferior
|
|
if (pos_.y + pos_.h > screen_height_) {
|
|
pos_.y = screen_height_ - pos_.h;
|
|
if (gravity_direction_ == GravityDirection::DOWN) {
|
|
// Colisión con superficie de gravedad - aplicar variación aleatoria
|
|
vy_ = -vy_ * loss_ * generateBounceVariation();
|
|
if (std::fabs(vy_) < 6.0f) {
|
|
vy_ = 0.0f;
|
|
on_surface_ = true;
|
|
}
|
|
} else {
|
|
// Rebote normal - con pérdida lateral aleatoria
|
|
vy_ = -vy_ * generateLateralLoss();
|
|
}
|
|
// Pérdida lateral en velocidad horizontal también
|
|
vx_ *= generateLateralLoss();
|
|
}
|
|
|
|
// Aplica rozamiento al estar en superficie
|
|
if (on_surface_) {
|
|
// Convertir rozamiento de frame-based a time-based
|
|
float friction_factor = pow(0.97f, 60.0f * deltaTime);
|
|
|
|
switch (gravity_direction_) {
|
|
case GravityDirection::DOWN:
|
|
case GravityDirection::UP:
|
|
// Fricción en X cuando gravedad es vertical
|
|
vx_ = vx_ * friction_factor;
|
|
if (std::fabs(vx_) < 6.0f) {
|
|
vx_ = 0.0f;
|
|
stopped_ = true;
|
|
}
|
|
break;
|
|
case GravityDirection::LEFT:
|
|
case GravityDirection::RIGHT:
|
|
// Fricción en Y cuando gravedad es horizontal
|
|
vy_ = vy_ * friction_factor;
|
|
if (std::fabs(vy_) < 6.0f) {
|
|
vy_ = 0.0f;
|
|
stopped_ = true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Actualiza la posición del sprite
|
|
sprite_->setPos({pos_.x, pos_.y});
|
|
}
|
|
|
|
// Pinta la clase
|
|
void Ball::render() {
|
|
sprite_->setColor(color_.r, color_.g, color_.b);
|
|
sprite_->render();
|
|
}
|
|
|
|
// Modifica la velocidad (convierte de frame-based a time-based)
|
|
void Ball::modVel(float vx, float vy) {
|
|
if (stopped_) {
|
|
vx_ = vx_ + (vx * 60.0f); // Convertir a píxeles/segundo
|
|
}
|
|
vy_ = vy_ + (vy * 60.0f); // Convertir a píxeles/segundo
|
|
on_surface_ = false;
|
|
stopped_ = false;
|
|
}
|
|
|
|
// Cambia la gravedad (usa la versión convertida)
|
|
void Ball::switchGravity() {
|
|
gravity_force_ = gravity_force_ == 0.0f ? (GRAVITY_FORCE * 60.0f * 60.0f) : 0.0f;
|
|
}
|
|
|
|
// Cambia la dirección de gravedad
|
|
void Ball::setGravityDirection(GravityDirection direction) {
|
|
gravity_direction_ = direction;
|
|
on_surface_ = false; // Ya no está en superficie al cambiar dirección
|
|
stopped_ = false; // Reactivar movimiento
|
|
}
|
|
|
|
// Aplica un pequeño empuje lateral aleatorio
|
|
void Ball::applyRandomLateralPush() {
|
|
// Generar velocidad lateral aleatoria (nunca 0)
|
|
float lateral_speed = GRAVITY_CHANGE_LATERAL_MIN + (rand() % 1000) / 1000.0f * (GRAVITY_CHANGE_LATERAL_MAX - GRAVITY_CHANGE_LATERAL_MIN);
|
|
|
|
// Signo aleatorio (+ o -)
|
|
int sign = ((rand() % 2) * 2) - 1;
|
|
lateral_speed *= sign;
|
|
|
|
// Aplicar según la dirección de gravedad actual
|
|
switch (gravity_direction_) {
|
|
case GravityDirection::UP:
|
|
case GravityDirection::DOWN:
|
|
// Gravedad vertical -> empuje lateral en X
|
|
vx_ += lateral_speed * 60.0f; // Convertir a píxeles/segundo
|
|
break;
|
|
case GravityDirection::LEFT:
|
|
case GravityDirection::RIGHT:
|
|
// Gravedad horizontal -> empuje lateral en Y
|
|
vy_ += lateral_speed * 60.0f; // Convertir a píxeles/segundo
|
|
break;
|
|
}
|
|
} |