#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; } // Destructor Ball::~Ball() { if (sprite) { delete sprite; } } // Actualiza la lógica de la clase void Ball::update() { if (stopped) { return; } // Actualiza la posición x += vx; y += vy; // Aplica la gravedad if (!onFloor) { vy += g; } // 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; } // Comprueba las colisiones con la parte inferior // if (y + h > SCREEN_HEIGHT) if ((y + h > SCREEN_HEIGHT) && (vy > 0)) { // y = SCREEN_HEIGHT - h; vy = -vy * LOSS; std::cout << vy << std::endl; if (abs(vy) < 0.5f) { 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; exit(0); } } // 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(); }