#include "sprites.h"
#include "include/raylib.h"

void PlayAnimation(SpriteAnimationPlayback *playbacks, SpriteAnimationPlayback playback) {
    playback.time_elapsed = 0.0f;
    playback.current_frame = 0;
    playback.is_finished = false;
    playbacks[0] = playback;
}

void TickSpriteAnimations(const SpriteAnimation *animations,
                          SpriteAnimationPlayback *playbacks,
                          int len) {
    const f32 anim_speed = 1.0f / 10.0f;
    for (int i = 0; i < len; i++) {
        SpriteAnimationPlayback *playback = &playbacks[i];
        const SpriteAnimation *animation = &animations[playback->anim_id];
        playback->time_elapsed += GetFrameTime();
        if (playback->time_elapsed >= anim_speed) {
            playback->time_elapsed = 0.0f;
            playback->current_frame++;
            if (playback->current_frame >= animation->total_frames) {
                if (animation->loop) {
                    playback->current_frame = 0;
                } else {
                    playback->is_finished = true;
                    playback->current_frame--;
                }
            }
        }
    }
}
