migrat Game, Player i Item a time based

This commit is contained in:
2025-10-28 10:52:13 +01:00
parent 9e8c5e13df
commit 31c84f9676
8 changed files with 190 additions and 153 deletions

View File

@@ -6,11 +6,11 @@
// Constructor
Item::Item(const ItemData &item)
: sprite_(std::make_shared<SurfaceSprite>(Resource::get()->getSurface(item.tile_set_file), item.x, item.y, ITEM_SIZE, ITEM_SIZE)),
change_color_speed_(4) {
time_accumulator_(static_cast<float>(item.counter) * COLOR_CHANGE_INTERVAL),
is_paused_(false) {
// Inicia variables
sprite_->setClip((item.tile % 10) * ITEM_SIZE, (item.tile / 10) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
collider_ = sprite_->getRect();
counter_ = item.counter * change_color_speed_;
// Inicializa los colores
color_.push_back(item.color1);
@@ -20,9 +20,19 @@ Item::Item(const ItemData &item)
color_.push_back(item.color2);
}
// Actualiza las variables del objeto
void Item::update(float delta_time) {
if (is_paused_) {
return;
}
time_accumulator_ += delta_time;
}
// Pinta el objeto en pantalla
void Item::render() const {
const int INDEX = (counter_ / change_color_speed_) % color_.size();
// Calcula el índice de color basado en el tiempo acumulado
const int INDEX = static_cast<int>(time_accumulator_ / COLOR_CHANGE_INTERVAL) % static_cast<int>(color_.size());
sprite_->render(1, color_.at(INDEX));
}