package com.kangacult.octogone;

import com.badlogic.gdx.graphics.Texture;

public class Animation {

    public Texture[] textures;
    public float[] times;
    public float totalTime;
    public float currentTime;
    /**Access this to display currentFrame */
    public Texture currentFrame;
    
    /**Textures, frames, and timings must all be loaded by the time this constructor is
     * invoked
     * @param textures Will be disposed during dispose()
     * @param frames
     * @param times
     */
    public Animation(Texture[] textures, float[] times) {
        this.textures = textures;
        this.times = times;
        currentTime = 0f;
        currentFrame = textures[0];
        totalTime = 0f;
        for (float t: times) {
            totalTime += t;
        }
    }
    
    public void update(float deltaTime) {
        currentTime += deltaTime;
        currentTime %= totalTime;
        
        float time = currentTime;
        for (int i = 0; i < textures.length; i++) {
            time -= times[i];
            if (time < 0) {
                currentFrame = textures[i];
                break;
            }
        }
    }
    
    public Texture getFrame(float time) {
        time %= totalTime;
        
        for (int i = 0; i < textures.length; i++) {
            time -= times[i];
            if (time < 0) {
                return textures[i];
            }
        }
        
        System.out.println(String.format("WARNING - Texture not found for time: %f", time));
        return textures[0];
    }
    
    public void dispose() {
        for (Texture t: textures) {
            t.dispose();
        }
    }

    public void resetAnimation()
    {
        currentTime = 0f;
    }
}
