time-based: Bullet amb dual-API move(float dt_s), velocitats en px/s (era px/frame)

This commit is contained in:
2026-05-19 16:59:44 +02:00
parent c920f99c82
commit eac2d42a1b
2 changed files with 58 additions and 12 deletions
+36
View File
@@ -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<float>(x);
pos_y_f_ = static_cast<float>(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<int>(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<int>(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<float>(x);
}
// Establece el valor de la variable
void Bullet::setPosY(int y) {
pos_y_ = y;
pos_y_f_ = static_cast<float>(y);
}
// Obtiene el valor de la variable