time-based: Bullet amb dual-API move(float dt_s), velocitats en px/s (era px/frame)
This commit is contained in:
@@ -11,6 +11,8 @@ Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Text
|
|||||||
// Posición inicial del objeto
|
// Posición inicial del objeto
|
||||||
pos_x_ = x;
|
pos_x_ = x;
|
||||||
pos_y_ = y;
|
pos_y_ = y;
|
||||||
|
pos_x_f_ = static_cast<float>(x);
|
||||||
|
pos_y_f_ = static_cast<float>(y);
|
||||||
|
|
||||||
// Alto y ancho del objeto
|
// Alto y ancho del objeto
|
||||||
width_ = 10;
|
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
|
// Velocidad inicial en el eje Y
|
||||||
vel_y_ = -3;
|
vel_y_ = -3;
|
||||||
|
vel_y_s_ = VEL_Y_PX_PER_S;
|
||||||
|
|
||||||
// Tipo de bala
|
// Tipo de bala
|
||||||
this->kind_ = kind;
|
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:
|
case Bullet::Kind::UP:
|
||||||
// Establece la velocidad inicial
|
// Establece la velocidad inicial
|
||||||
vel_x_ = 0;
|
vel_x_ = 0;
|
||||||
|
vel_x_s_ = 0.0F;
|
||||||
|
|
||||||
// Rectangulo con los gráficos del objeto
|
// Rectangulo con los gráficos del objeto
|
||||||
if (!powered_up) {
|
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:
|
case Bullet::Kind::LEFT:
|
||||||
// Establece la velocidad inicial
|
// Establece la velocidad inicial
|
||||||
vel_x_ = -2;
|
vel_x_ = -2;
|
||||||
|
vel_x_s_ = VEL_X_LEFT_PX_PER_S;
|
||||||
|
|
||||||
// Rectangulo con los gráficos del objeto
|
// Rectangulo con los gráficos del objeto
|
||||||
if (!powered_up) {
|
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:
|
case Bullet::Kind::RIGHT:
|
||||||
// Establece la velocidad inicial
|
// Establece la velocidad inicial
|
||||||
vel_x_ = 2;
|
vel_x_ = 2;
|
||||||
|
vel_x_s_ = VEL_X_RIGHT_PX_PER_S;
|
||||||
|
|
||||||
// Rectangulo con los gráficos del objeto
|
// Rectangulo con los gráficos del objeto
|
||||||
if (!powered_up) {
|
if (!powered_up) {
|
||||||
@@ -123,6 +129,34 @@ auto Bullet::move() -> MoveResult {
|
|||||||
return msg;
|
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
|
// Comprueba si el objeto está habilitado
|
||||||
auto Bullet::isEnabled() const -> bool {
|
auto Bullet::isEnabled() const -> bool {
|
||||||
return kind_ != Bullet::Kind::NONE;
|
return kind_ != Bullet::Kind::NONE;
|
||||||
@@ -146,11 +180,13 @@ auto Bullet::getPosY() const -> int {
|
|||||||
// Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void Bullet::setPosX(int x) {
|
void Bullet::setPosX(int x) {
|
||||||
pos_x_ = x;
|
pos_x_ = x;
|
||||||
|
pos_x_f_ = static_cast<float>(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void Bullet::setPosY(int y) {
|
void Bullet::setPosY(int y) {
|
||||||
pos_y_ = y;
|
pos_y_ = y;
|
||||||
|
pos_y_f_ = static_cast<float>(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ class Bullet {
|
|||||||
auto operator=(const Bullet &) -> Bullet & = delete;
|
auto operator=(const Bullet &) -> Bullet & = delete;
|
||||||
|
|
||||||
void render(); // Pinta el objeto en pantalla
|
void render(); // Pinta el objeto en pantalla
|
||||||
auto move() -> MoveResult; // Actualiza la posición y estado del objeto
|
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
|
void disable(); // Deshabilita el objeto
|
||||||
|
|
||||||
[[nodiscard]] auto isEnabled() const -> bool; // Comprueba si el objeto está habilitado
|
[[nodiscard]] auto isEnabled() const -> bool; // Comprueba si el objeto está habilitado
|
||||||
@@ -49,16 +50,25 @@ class Bullet {
|
|||||||
auto getCollider() -> Circle &; // Obtiene el circulo de colisión
|
auto getCollider() -> Circle &; // Obtiene el circulo de colisión
|
||||||
|
|
||||||
private:
|
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
|
// Objetos y punteros
|
||||||
Sprite *sprite_; // Sprite con los graficos y métodos de pintado
|
Sprite *sprite_; // Sprite con los graficos y métodos de pintado
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
int pos_x_; // Posición en el eje X
|
int pos_x_; // Posición en el eje X (px enters per al sprite/collider)
|
||||||
int pos_y_; // Posición en el eje Y
|
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 width_; // Ancho del objeto
|
||||||
Uint8 height_; // Alto del objeto
|
Uint8 height_; // Alto del objeto
|
||||||
int vel_x_; // Velocidad en el eje X
|
int vel_x_; // Velocidad en el eje X (frame-based: px/frame)
|
||||||
int vel_y_; // Velocidad en el eje Y
|
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
|
Kind kind_; // Tipo de objeto
|
||||||
int owner_; // Identificador del dueño del objeto
|
int owner_; // Identificador del dueño del objeto
|
||||||
Circle collider_; // Circulo de colisión del objeto
|
Circle collider_; // Circulo de colisión del objeto
|
||||||
|
|||||||
Reference in New Issue
Block a user