From eac2d42a1b4b9fc0201795654ae6a171ae09f74d Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 19 May 2026 16:59:44 +0200 Subject: [PATCH] time-based: Bullet amb dual-API move(float dt_s), velocitats en px/s (era px/frame) --- source/game/entities/bullet.cpp | 36 +++++++++++++++++++++++++++++++++ source/game/entities/bullet.h | 34 ++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/source/game/entities/bullet.cpp b/source/game/entities/bullet.cpp index cf031fa..5eebb82 100644 --- a/source/game/entities/bullet.cpp +++ b/source/game/entities/bullet.cpp @@ -11,6 +11,8 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text // Posición inicial del objeto pos_x_ = x; pos_y_ = y; + pos_x_f_ = static_cast(x); + pos_y_f_ = static_cast(y); // Alto y ancho del objeto width_ = 10; @@ -18,6 +20,7 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text // Velocidad inicial en el eje Y vel_y_ = -3; + vel_y_s_ = VEL_Y_PX_PER_S; // Tipo de bala this->kind_ = kind; @@ -30,6 +33,7 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text case Bullet::Kind::UP: // Establece la velocidad inicial vel_x_ = 0; + vel_x_s_ = 0.0F; // Rectangulo con los gráficos del objeto if (!powered_up) { @@ -42,6 +46,7 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text case Bullet::Kind::LEFT: // Establece la velocidad inicial vel_x_ = -2; + vel_x_s_ = VEL_X_LEFT_PX_PER_S; // Rectangulo con los gráficos del objeto if (!powered_up) { @@ -54,6 +59,7 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text case Bullet::Kind::RIGHT: // Establece la velocidad inicial vel_x_ = 2; + vel_x_s_ = VEL_X_RIGHT_PX_PER_S; // Rectangulo con los gráficos del objeto if (!powered_up) { @@ -123,6 +129,34 @@ auto Bullet::move() -> MoveResult { return msg; } +// Actualiza la posición y estado del objeto (time-based) +auto Bullet::move(float dt_s) -> MoveResult { + MoveResult msg = MoveResult::OK; + + pos_x_f_ += vel_x_s_ * dt_s; + pos_x_ = static_cast(pos_x_f_); + + if ((pos_x_ < PLAY_AREA_LEFT - width_) || (pos_x_ > PLAY_AREA_RIGHT)) { + kind_ = Bullet::Kind::NONE; + msg = MoveResult::OUT; + } + + pos_y_f_ += vel_y_s_ * dt_s; + pos_y_ = static_cast(pos_y_f_); + + if (pos_y_ < PLAY_AREA_TOP - height_) { + kind_ = Bullet::Kind::NONE; + msg = MoveResult::OUT; + } + + sprite_->setPosX(pos_x_); + sprite_->setPosY(pos_y_); + + shiftColliders(); + + return msg; +} + // Comprueba si el objeto está habilitado auto Bullet::isEnabled() const -> bool { return kind_ != Bullet::Kind::NONE; @@ -146,11 +180,13 @@ auto Bullet::getPosY() const -> int { // Establece el valor de la variable void Bullet::setPosX(int x) { pos_x_ = x; + pos_x_f_ = static_cast(x); } // Establece el valor de la variable void Bullet::setPosY(int y) { pos_y_ = y; + pos_y_f_ = static_cast(y); } // Obtiene el valor de la variable diff --git a/source/game/entities/bullet.h b/source/game/entities/bullet.h index d3cfa84..b24ce0f 100644 --- a/source/game/entities/bullet.h +++ b/source/game/entities/bullet.h @@ -32,9 +32,10 @@ class Bullet { Bullet(const Bullet &) = delete; auto operator=(const Bullet &) -> Bullet & = delete; - void render(); // Pinta el objeto en pantalla - auto move() -> MoveResult; // Actualiza la posición y estado del objeto - void disable(); // Deshabilita el objeto + void render(); // Pinta el objeto en pantalla + auto move() -> MoveResult; // Actualiza la posición y estado del objeto (frame-based) + auto move(float dt_s) -> MoveResult; // Actualiza la posición y estado del objeto (time-based) + void disable(); // Deshabilita el objeto [[nodiscard]] auto isEnabled() const -> bool; // Comprueba si el objeto está habilitado [[nodiscard]] auto getPosX() const -> int; // Obtiene el valor de la variable @@ -49,19 +50,28 @@ class Bullet { auto getCollider() -> Circle &; // Obtiene el circulo de colisión private: + // Velocidades time-based (px/s). Derivades de les antigues px/frame * 60. + static constexpr float VEL_Y_PX_PER_S = -180.0F; // Era -3 px/frame + static constexpr float VEL_X_LEFT_PX_PER_S = -120.0F; // Era -2 px/frame + static constexpr float VEL_X_RIGHT_PX_PER_S = 120.0F; // Era +2 px/frame + // Objetos y punteros Sprite *sprite_; // Sprite con los graficos y métodos de pintado // Variables - int pos_x_; // Posición en el eje X - int pos_y_; // Posición en el eje Y - Uint8 width_; // Ancho del objeto - Uint8 height_; // Alto del objeto - int vel_x_; // Velocidad en el eje X - int vel_y_; // Velocidad en el eje Y - Kind kind_; // Tipo de objeto - int owner_; // Identificador del dueño del objeto - Circle collider_; // Circulo de colisión del objeto + int pos_x_; // Posición en el eje X (px enters per al sprite/collider) + int pos_y_; // Posición en el eje Y + float pos_x_f_{0}; // Acumulador subpíxel per al moviment time-based + float pos_y_f_{0}; // Acumulador subpíxel per al moviment time-based + Uint8 width_; // Ancho del objeto + Uint8 height_; // Alto del objeto + int vel_x_; // Velocidad en el eje X (frame-based: px/frame) + int vel_y_; // Velocidad en el eje Y (frame-based: px/frame) + float vel_x_s_{0}; // Velocidad en el eje X (time-based: px/s) + float vel_y_s_{0}; // Velocidad en el eje Y (time-based: px/s) + Kind kind_; // Tipo de objeto + int owner_; // Identificador del dueño del objeto + Circle collider_; // Circulo de colisión del objeto void shiftColliders(); // Alinea el circulo de colisión con el objeto };