cmasmasizat el codi

This commit is contained in:
2025-03-24 13:28:54 +01:00
parent 4a2e5c27e2
commit e2e3b7c779
6 changed files with 75 additions and 111 deletions
+16 -28
View File
@@ -2,16 +2,13 @@
#include "defines.h"
// Constructor
Ball::Ball(float x, float vx, float vy, Color color, Texture *texture)
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture)
{
x_ = x;
y_ = 0.0f;
w_ = BALL_SIZE;
h_ = BALL_SIZE;
pos_ = {x, 0.0f, BALL_SIZE, BALL_SIZE};
vx_ = vx;
vy_ = vy;
sprite_ = new Sprite(texture);
sprite_->setPos({(int)x, (int)y_});
sprite_ = std::make_unique<Sprite>(texture);
sprite_->setPos({pos_.x, pos_.y});
sprite_->setSize(BALL_SIZE, BALL_SIZE);
sprite_->setClip({0, 0, BALL_SIZE, BALL_SIZE});
color_ = color;
@@ -21,15 +18,6 @@ Ball::Ball(float x, float vx, float vy, Color color, Texture *texture)
loss_ = ((rand() % 30) * 0.01f) + 0.6f;
}
// Destructor
Ball::~Ball()
{
if (sprite_)
{
delete sprite_;
}
}
// Actualiza la lógica de la clase
void Ball::update()
{
@@ -39,40 +27,40 @@ void Ball::update()
}
// Aplica la gravedad a la velocidad
if (!on_floor_ && (y_ - SCREEN_HEIGHT) < BALL_SIZE * 2)
if (!on_floor_ && (pos_.y - SCREEN_HEIGHT) < BALL_SIZE * 2)
{
vy_ += gravity_force_;
}
// Actualiza la posición en función de la velocidad
x_ += vx_;
y_ += vy_;
pos_.x += vx_;
pos_.y += vy_;
// Comprueba las colisiones con el lateral izquierdo
if (x_ < 0)
if (pos_.x < 0)
{
x_ = 0;
pos_.x = 0;
vx_ = -vx_;
}
// Comprueba las colisiones con el lateral derecho
if (x_ + w_ > SCREEN_WIDTH)
if (pos_.x + pos_.w > SCREEN_WIDTH)
{
x_ = SCREEN_WIDTH - w_;
pos_.x = SCREEN_WIDTH - pos_.w;
vx_ = -vx_;
}
// Comprueba las colisiones con la parte superior
if (y_ < 0)
if (pos_.y < 0)
{
y_ = 0;
pos_.y = 0;
vy_ = -vy_;
}
// Comprueba las colisiones con la parte inferior
if (y_ + h_ > SCREEN_HEIGHT)
if (pos_.y + pos_.h > SCREEN_HEIGHT)
{
y_ = SCREEN_HEIGHT - h_;
pos_.y = SCREEN_HEIGHT - pos_.h;
vy_ = -vy_ * loss_;
if (abs(vy_) < 0.1f)
{
@@ -93,7 +81,7 @@ void Ball::update()
}
// Actualiza la posición del sprite
sprite_->setPos({(int)x_, (int)y_});
sprite_->setPos({pos_.x, pos_.y});
}
// Pinta la clase