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) {