migrat a SDL3

This commit is contained in:
2025-03-24 12:48:32 +01:00
parent 111cbb2217
commit 4a2e5c27e2
13 changed files with 366 additions and 309 deletions
+52 -54
View File
@@ -2,123 +2,121 @@
#include "defines.h"
// Constructor
Ball::Ball(float x, float vx, float vy, color_t color, Texture *texture)
Ball::Ball(float x, float vx, float vy, Color 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;
x_ = x;
y_ = 0.0f;
w_ = BALL_SIZE;
h_ = BALL_SIZE;
vx_ = vx;
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});
color_ = color;
gravity_force_ = GRAVITY_FORCE;
on_floor_ = false;
stopped_ = false;
loss_ = ((rand() % 30) * 0.01f) + 0.6f;
}
// Destructor
Ball::~Ball()
{
if (sprite)
if (sprite_)
{
delete sprite;
delete sprite_;
}
}
// Actualiza la lógica de la clase
void Ball::update()
{
if (stopped)
if (stopped_)
{
return;
}
// Aplica la gravedad a la velocidad
if (!onFloor && (y - SCREEN_HEIGHT) < BALL_SIZE * 2)
if (!on_floor_ && (y_ - SCREEN_HEIGHT) < BALL_SIZE * 2)
{
vy += g;
vy_ += gravity_force_;
}
// Actualiza la posición en función de la velocidad
x += vx;
y += vy;
x_ += vx_;
y_ += vy_;
// Comprueba las colisiones con el lateral izquierdo
if (x < 0)
if (x_ < 0)
{
x = 0;
vx = -vx;
x_ = 0;
vx_ = -vx_;
}
// Comprueba las colisiones con el lateral derecho
if (x + w > SCREEN_WIDTH)
if (x_ + w_ > SCREEN_WIDTH)
{
x = SCREEN_WIDTH - w;
vx = -vx;
x_ = SCREEN_WIDTH - w_;
vx_ = -vx_;
}
// Comprueba las colisiones con la parte superior
if (y < 0)
if (y_ < 0)
{
y = 0;
vy = -vy;
//vy = -vy * loss;
y_ = 0;
vy_ = -vy_;
}
// Comprueba las colisiones con la parte inferior
if (y + h > SCREEN_HEIGHT)
if (y_ + h_ > SCREEN_HEIGHT)
{
y = SCREEN_HEIGHT - h;
vy = -vy * loss;
if (abs(vy) < 0.1f)
y_ = SCREEN_HEIGHT - h_;
vy_ = -vy_ * loss_;
if (abs(vy_) < 0.1f)
{
vy = 0.0f;
onFloor = true;
vy_ = 0.0f;
on_floor_ = true;
}
}
// Aplica rozamiento al rodar por el suelo
if (onFloor)
if (on_floor_)
{
vx = vx * 0.97f;
if (abs(vx) < 0.1f)
vx_ = vx_ * 0.97f;
if (abs(vx_) < 0.1f)
{
vx = 0.0f;
stopped = true;
vx_ = 0.0f;
stopped_ = true;
}
}
// Actualiza la posición del sprite
sprite->setPos({(int)x, (int)y});
sprite->update();
sprite_->setPos({(int)x_, (int)y_});
}
// Pinta la clase
void Ball::render()
{
sprite->setColor(color.r, color.g, color.b);
sprite->render();
sprite_->setColor(color_.r, color_.g, color_.b);
sprite_->render();
}
// Modifica la velocidad
void Ball::modVel(float vx, float vy)
{
if (stopped)
if (stopped_)
{
this->vx = this->vx + vx;
vx_ = vx_ + vx;
}
this->vy = this->vy + vy;
onFloor = false;
stopped = false;
vy_ = vy_ + vy;
on_floor_ = false;
stopped_ = false;
}
// Cambia la gravedad
void Ball::switchGravity()
{
g = g == 0.0f ? GRAVITY : 0.0f;
gravity_force_ = gravity_force_ == 0.0f ? GRAVITY_FORCE : 0.0f;
}