delta-time: moving_sprite.cpp

This commit is contained in:
2025-09-16 16:37:39 +02:00
parent e2fd470ad3
commit 26e0fd7247
2 changed files with 39 additions and 9 deletions

View File

@@ -53,7 +53,7 @@ void MovingSprite::stop() {
flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
}
// Mueve el sprite
// Mueve el sprite (frame-based)
void MovingSprite::move() {
x_ += vx_;
y_ += vy_;
@@ -65,16 +65,34 @@ void MovingSprite::move() {
pos_.y = static_cast<int>(y_);
}
// Actualiza las variables internas del objeto
// Mueve el sprite (time-based)
void MovingSprite::move(float deltaTime) {
x_ += vx_ * deltaTime;
y_ += vy_ * deltaTime;
vx_ += ax_ * deltaTime;
vy_ += ay_ * deltaTime;
pos_.x = static_cast<int>(x_);
pos_.y = static_cast<int>(y_);
}
// Actualiza las variables internas del objeto (frame-based)
void MovingSprite::update() {
move();
rotate();
}
// Actualiza las variables internas del objeto (time-based)
void MovingSprite::update(float deltaTime) {
move(deltaTime);
rotate(deltaTime);
}
// Muestra el sprite por pantalla
void MovingSprite::render() { getTexture()->render(pos_.x, pos_.y, &sprite_clip_, horizontal_zoom_, vertical_zoom_, rotate_.angle, &rotate_.center, flip_); }
// Establece la rotacion
// Establece la rotacion (frame-based)
void MovingSprite::rotate() {
if (rotate_.enabled) {
++rotate_.counter;
@@ -85,6 +103,15 @@ void MovingSprite::rotate() {
}
}
// Establece la rotacion (time-based)
void MovingSprite::rotate(float deltaTime) {
if (rotate_.enabled) {
// Convertir speed (frames) a tiempo: speed frames = speed/60 segundos a 60fps
float rotationSpeed = static_cast<float>(rotate_.speed) / 60.0f;
rotate_.angle += rotate_.amount * (deltaTime / rotationSpeed);
}
}
// Activa o desactiva el efecto de rotación
void MovingSprite::setRotate(bool enable) {
rotate_.enabled = enable;