// Improvements by Greg Borenstein (November 2012):
// - Set speed based on desired FPS
// - playOnce(), loop(), stop functions
// - load images from a directory
// - rename to AnimatedSprite to get around weird windows problem.

// Originally by:
// Daniel Shiffman
// Hanukkah 2011
// 8 nights of Processing examples
// http://www.shiffman.net

// The animation object

class AnimatedSprite {
  float x;  // location for Animation
  float y;  // location for Animation
  float w;  // location for Animation
  float h;  // location for Animation
  int dirX;  // location for Animation

  // The index into the array is a float!
  // This allows us to vary the speed of the animation
  // It will have to be converted to an int before the actual image is displayed
  float index = 0; 

  // Speed, this will control both the animations movement
  // as well as how fast it cycles through the images
  float speed;
  int fps;
  // The array of images
  PImage[] images;

  boolean playing = false;
  boolean playOnce = true;
  
  AnimatedSprite(String pathToImages, int fps, float x_, float y_) {
    loadImagesFromPath(pathToImages);
    this.x = this.x + x_;
    this.y = this.y + y_;
    this.fps = fps;
    index = 0;
    setFPS(fps);
  }
  
  AnimatedSprite(PImage[] images_,int fps, float x_, float y_, float w_, float h_ ) {
    images = images_;
    this.x = this.x + x_;
    this.y = this.y + y_;
    this.w = this.w + w_;
    this.h = this.h + h_;
    this.fps = fps;
    this.dirX = 1;
    index = 0;
    setFPS(fps);
  }
  
  
  void loadImagesFromPath(String path) {
    java.io.File folder = new java.io.File(dataPath(path));
    String[] filenames = folder.list();
    images = new PImage[filenames.length];

    for (int i = 0; i < filenames.length; i++) {
      images[i] = loadImage(dataPath(path + "/" + filenames[i]));
    }
  }
  
  void mirrorX(int dir) {
        this.dirX = dir;
    }
  
  float getWidth() {
    return this.w;
  }
  
  float getHeight() {
    return this.h;
  }
  
  void setFPS(int fps) {
    this.fps = fps;
  }

  void setSpeed(int fps) {
    speed = fps/(float)frameRate;
  }

  void playOnce() {
    index = 0;
    playOnce = true;
    playing = true;
  }
  
  void loop(){
    playing = true;
    playOnce = false;
  }
  
  void stop(){
    playing = false;
    playOnce = false;
  }
  
  boolean isPlaying(){
    return playing;
  }
  

  void draw() {
    // We must convert the float index to an int first!
    int imageIndex = int(index);
    pushMatrix();
    scale(this.dirX,1);
    image(images[imageIndex], this.x*this.dirX, this.y,this.w,this.h);
    popMatrix();
  }

  void update() {
    setSpeed(fps);
    if (playing) {
      // Move the index forward in the animation sequence
      index += speed;
      // If we are at the end, go back to the beginning
      if (index >= images.length) {
        if (playOnce) {
          playing = false;
          index = images.length-1;
        } 
        else {
          // We could just say index = 0
          // but this is slightly more accurate
          index -= images.length;
        }
      }
    }
  }
}

void loadAnimation(String[] stringArray, PImage[] seq) {
  for (int i = 0; i < stringArray.length; i++) {
    seq[i] = loadImage(stringArray[i]);
  }

}
