#include <DangerousRacers/AnimatedSprite.h>

AnimatedSprite::AnimatedSprite(const float x, const float y, const std::string &texturePath, const int _frames, const int _fps) : DrawableEntity(x, y, texturePath) {
    fps = _fps;
    frames = _frames;
    sprite.setTextureRect({0, 0, getHeight(), getHeight()});
}

void AnimatedSprite::render(sf::RenderWindow &window) {
    DrawableEntity::render(window);
}

void AnimatedSprite::update(const float deltaTime) {
    DrawableEntity::update(deltaTime);

    frameTimer += deltaTime;

    if (frameTimer >= 1.f / static_cast<float>(fps)) {
        frameTimer = 0.f;
        sprite.setTextureRect({getHeight() * (currentFrame % frames), 0, getHeight(), getHeight()});
        currentFrame++;
    }
}

