time-based: Item amb dual-API update/move/updateTimeToLive(dt_s), vels/accels en px/s i px/s2, TTL en s

This commit is contained in:
2026-05-19 17:02:42 +02:00
parent eac2d42a1b
commit 13875e7b8c
2 changed files with 111 additions and 22 deletions
+72 -2
View File
@@ -1,6 +1,7 @@
#include "game/entities/item.h"
#include <cstdlib> // for rand
#include <algorithm> // for max
#include <cstdlib> // for rand
#include "core/rendering/animatedsprite.h" // for AnimatedSprite
#include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_AR...
@@ -13,7 +14,9 @@ Item::Item(Id id, float x, float y, Texture *texture, const std::vector<std::str
this->id_ = id;
enabled_ = true;
time_to_live_ = 600;
time_to_live_s_ = TIME_TO_LIVE_S;
accel_x_ = 0.0F;
accel_x_s_ = 0.0F;
floor_collision_ = false;
if (id == Item::Id::COFFEE_MACHINE) {
@@ -24,15 +27,23 @@ Item::Item(Id id, float x, float y, Texture *texture, const std::vector<std::str
vel_x_ = 0.0F;
vel_y_ = -0.1F;
accel_y_ = 0.1F;
vel_x_s_ = 0.0F;
vel_y_s_ = COFFEE_VEL_Y_PX_PER_S;
accel_y_s_ = COFFEE_ACCEL_Y_PX_PER_S2;
collider_.r = 10;
} else {
width_ = 16;
height_ = 16;
pos_x_ = x;
pos_y_ = y;
vel_x_ = -1.0F + ((rand() % 5) * 0.5F);
// Distribució original: -1.0, -0.5, 0.0, 0.5, 1.0 px/frame ⇒ -60..60 px/s en passos de 30.
const int RAND_STEP = rand() % 5;
vel_x_ = -1.0F + (static_cast<float>(RAND_STEP) * 0.5F);
vel_y_ = -4.0F;
accel_y_ = 0.2F;
vel_x_s_ = (-2.0F + static_cast<float>(RAND_STEP)) * ITEM_VEL_X_STEP_PX_PER_S;
vel_y_s_ = ITEM_VEL_Y_PX_PER_S;
accel_y_s_ = ITEM_ACCEL_Y_PX_PER_S2;
collider_.r = width_ / 2;
}
@@ -122,6 +133,47 @@ void Item::move() {
shiftColliders();
}
// Actualiza la posición y estados del objeto (time-based)
void Item::move(float dt_s) {
floor_collision_ = false;
// Posició
pos_x_ += vel_x_s_ * dt_s;
pos_y_ += vel_y_s_ * dt_s;
// Acceleració
vel_x_s_ += accel_x_s_ * dt_s;
vel_y_s_ += accel_y_s_ * dt_s;
// Si surt per laterals, corregeix i inverteix
if ((pos_x_ < PLAY_AREA_LEFT) || (pos_x_ + width_ > PLAY_AREA_RIGHT)) {
pos_x_ -= vel_x_s_ * dt_s;
vel_x_s_ = -vel_x_s_;
}
// Rebot per dalt (excepte la màquina de cafè)
if ((pos_y_ < PLAY_AREA_TOP) && !(id_ == Item::Id::COFFEE_MACHINE)) {
pos_y_ -= vel_y_s_ * dt_s;
vel_y_s_ = -vel_y_s_;
}
// Topa amb el terra
if (pos_y_ + height_ > PLAY_AREA_BOTTOM) {
vel_y_s_ = 0;
vel_x_s_ = 0;
accel_x_s_ = 0;
accel_y_s_ = 0;
pos_y_ = PLAY_AREA_BOTTOM - height_;
if (id_ == Item::Id::COFFEE_MACHINE) {
floor_collision_ = true;
}
}
sprite_->setPosX(int(pos_x_));
sprite_->setPosY(int(pos_y_));
shiftColliders();
}
// Pone a cero todos los valores del objeto
void Item::disable() {
enabled_ = false;
@@ -135,6 +187,14 @@ void Item::update() {
checkTimeToLive();
}
// Actualiza el objeto (time-based)
void Item::update(float dt_s) {
move(dt_s);
sprite_->animate(dt_s);
updateTimeToLive(dt_s);
checkTimeToLive();
}
// Actualiza el contador
void Item::updateTimeToLive() {
if (time_to_live_ > 0) {
@@ -142,6 +202,16 @@ void Item::updateTimeToLive() {
}
}
// Actualiza el contador (time-based). Manté time_to_live_ sincronitzat per a
// que render() segueixi funcionant amb la mateixa condició de parpelleig.
void Item::updateTimeToLive(float dt_s) {
if (time_to_live_s_ > 0.0F) {
time_to_live_s_ = std::max(0.0F, time_to_live_s_ - dt_s);
}
constexpr float FRAMES_PER_S = 60.0F;
time_to_live_ = static_cast<Uint16>(time_to_live_s_ * FRAMES_PER_S);
}
// Comprueba si el objeto sigue vivo
void Item::checkTimeToLive() {
if (time_to_live_ == 0) {
+39 -20
View File
@@ -29,12 +29,14 @@ class Item {
Item(const Item &) = delete;
auto operator=(const Item &) -> Item & = delete;
void allignTo(int x); // Centra el objeto en la posición X
void render(); // Pinta el objeto en la pantalla
void disable(); // Pone a cero todos los valores del objeto
void update(); // Actualiza al objeto a su posicion, animación y controla los contadores
void updateTimeToLive(); // Actualiza el contador
void checkTimeToLive(); // Comprueba si el objeto sigue vivo
void allignTo(int x); // Centra el objeto en la posición X
void render(); // Pinta el objeto en la pantalla
void disable(); // Pone a cero todos los valores del objeto
void update(); // Actualiza al objeto (frame-based)
void update(float dt_s); // Actualiza al objeto (time-based)
void updateTimeToLive(); // Actualiza el contador (frame-based)
void updateTimeToLive(float dt_s); // Actualiza el contador (time-based)
void checkTimeToLive(); // Comprueba si el objeto sigue vivo
[[nodiscard]] auto getPosX() const -> float; // Obtiene del valor de la variable
[[nodiscard]] auto getPosY() const -> float; // Obtiene del valor de la variable
@@ -47,24 +49,41 @@ class Item {
auto getCollider() -> Circle &; // Obtiene el circulo de colisión
private:
// Time-based: equivalents en unitats físiques precalculades a 60Hz.
static constexpr float ITEM_VEL_Y_PX_PER_S = -240.0F; // Era -4.0 px/frame
static constexpr float ITEM_ACCEL_Y_PX_PER_S2 = 720.0F; // Era +0.2 px/frame²
static constexpr float ITEM_VEL_X_STEP_PX_PER_S = 30.0F; // Era 0.5 px/frame, 5 valors en [-1.0, 1.0]
static constexpr float COFFEE_VEL_Y_PX_PER_S = -6.0F; // Era -0.1 px/frame
static constexpr float COFFEE_ACCEL_Y_PX_PER_S2 = 360.0F; // Era +0.1 px/frame²
static constexpr float TIME_TO_LIVE_S = 10.0F; // Era 600 frames
static constexpr float BLINK_START_S = TIME_TO_LIVE_S - (200.0F / 60.0F); // Era time_to_live_ > 200
static constexpr float BLINK_PERIOD_S = 20.0F / 60.0F; // Era % 20
static constexpr float BLINK_OFF_S = 10.0F / 60.0F; // Era % 20 <= 10
// Objetos y punteros
AnimatedSprite *sprite_; // Sprite con los graficos del objeto
// Variables
float pos_x_; // Posición X del objeto
float pos_y_; // Posición Y del objeto
Uint8 width_; // Ancho del objeto
Uint8 height_; // Alto del objeto
float vel_x_; // Velocidad en el eje X
float vel_y_; // Velocidad en el eje Y
float accel_x_; // Aceleración en el eje X
float accel_y_; // Aceleración en el eje Y
bool floor_collision_; // Indica si el objeto colisiona con el suelo
Id id_; // Especifica el tipo de objeto que es
bool enabled_; // Especifica si el objeto está habilitado
Uint16 time_to_live_; // Temporizador con el tiempo que el objeto está presente
Circle collider_; // Circulo de colisión del objeto
float pos_x_; // Posición X del objeto
float pos_y_; // Posición Y del objeto
Uint8 width_; // Ancho del objeto
Uint8 height_; // Alto del objeto
float vel_x_; // Velocidad en el eje X (frame-based: px/frame)
float vel_y_; // Velocidad en el eje Y (frame-based: px/frame)
float accel_x_; // Aceleración en el eje X (frame-based: px/frame²)
float accel_y_; // Aceleración en el eje Y (frame-based: px/frame²)
float vel_x_s_{0.0F}; // Velocidad en el eje X (time-based: px/s)
float vel_y_s_{0.0F}; // Velocidad en el eje Y (time-based: px/s)
float accel_x_s_{0.0F}; // Aceleración en el eje X (time-based: px/s²)
float accel_y_s_{0.0F}; // Aceleración en el eje Y (time-based: px/s²)
bool floor_collision_; // Indica si el objeto colisiona con el suelo
Id id_; // Especifica el tipo de objeto que es
bool enabled_; // Especifica si el objeto está habilitado
Uint16 time_to_live_; // Temporizador (frame-based)
float time_to_live_s_{0.0F}; // Temporizador (time-based)
Circle collider_; // Circulo de colisión del objeto
void shiftColliders(); // Alinea el circulo de colisión con la posición del objeto
void move(); // Actualiza la posición y estados del objeto
void move(); // Actualiza la posición y estados del objeto (frame-based)
void move(float dt_s); // Actualiza la posición y estados del objeto (time-based)
};