package de.frajul.endlessroll.entities;

import de.frajul.endlessroll.main.game.Timer;

/**
 * Created by Julian on 14.12.2015.
 */
public class Animation {

    private int[] indexSequence = {0, 1, 2, 3};
    private boolean looping = false;
    private int requiredDelta = 110;

    private int indexPointer = 0;
    private boolean stillRunning = true;
    private long lastSwitchTime = -1;

    public void disable(){
        stillRunning = false;
    }

    public void update(Timer timer) {
        if (lastSwitchTime == -1) {
            lastSwitchTime = timer.getCurrentTime();
            return;
        }
        if (stillRunning) {
            long delta = timer.getCurrentTime() - lastSwitchTime;
            if (delta >= requiredDelta) {
                lastSwitchTime = timer.getCurrentTime();
                indexPointer++;
                if (!looping && indexPointer == indexSequence.length - 1)
                    stillRunning = false;
                if (looping && indexPointer == indexSequence.length)
                    indexPointer = 0;
            }
        }
    }

    public int getCurrentTexIndex() {
        return indexSequence[indexPointer];
    }

    public void setIndexSequence(int[] indexSequence) {
        this.indexSequence = indexSequence;
    }

    public void setRequiredDelta(int requiredDelta) {
        this.requiredDelta = requiredDelta;
    }

    public void setLooping(boolean looping) {
        this.looping = looping;
    }
}
