#include "ball.h" #include "defines.h" // Constructor Ball::Ball(float x, float vx, float vy, color_t color, Texture *texture) { this->x = x; this->y = 0.0f; this->w = BALL_SIZE; this->h = BALL_SIZE; this->vx = vx; this->vy = vy; sprite = new Sprite(texture); sprite->setPos({(int)x, (int)y}); sprite->setSize(BALL_SIZE, BALL_SIZE); sprite->setClip({0, 0, BALL_SIZE, BALL_SIZE}); this->color = color; g = GRAVITY; onFloor = false; stopped = false; loss = ((rand() % 30) * 0.01f) + 0.6f; } // Destructor Ball::~Ball() { if (sprite) { delete sprite; } } // Actualiza la lógica de la clase void Ball::update() { if (stopped) { return; } // Aplica la gravedad a la velocidad if (!onFloor && (y - SCREEN_HEIGHT) < BALL_SIZE * 2) { vy += g; } // Actualiza la posición en función de la velocidad x += vx; y += vy; // Comprueba las colisiones con el lateral izquierdo if (x < 0) { x = 0; vx = -vx; } // Comprueba las colisiones con el lateral derecho if (x + w > SCREEN_WIDTH) { x = SCREEN_WIDTH - w; vx = -vx; } // Comprueba las colisiones con la parte superior if (y < 0) { y = 0; vy = -vy; //vy = -vy * loss; } // Comprueba las colisiones con la parte inferior if (y + h > SCREEN_HEIGHT) { y = SCREEN_HEIGHT - h; vy = -vy * loss; if (abs(vy) < 0.1f) { vy = 0.0f; onFloor = true; } } // Aplica rozamiento al rodar por el suelo if (onFloor) { vx = vx * 0.97f; if (abs(vx) < 0.1f) { vx = 0.0f; stopped = true; } } // Actualiza la posición del sprite sprite->setPos({(int)x, (int)y}); sprite->update(); } // Pinta la clase void Ball::render() { sprite->setColor(color.r, color.g, color.b); sprite->render(); } // Modifica la velocidad void Ball::modVel(float vx, float vy) { if (stopped) { this->vx = this->vx + vx; } this->vy = this->vy + vy; onFloor = false; stopped = false; }