/****************************************************
 *    6.837 Project 1:  Sprite-based Applets        *
 *    Created by:  Rielyn Sarabia                   *
 *    September 28, 1998                            *
 ****************************************************/

package project1;

import java.awt.*;
import java.awt.image.*;
import java.util.Vector;

class AnimatedSprite extends Sprite {
  //AnimatedSprite is a Sprite with two or more frames
  //The image used for this must be one row of equally-sized frames stored in 
  //  one file (gif or jpg)

  int frames;           // frames in sprite
  Vector tracks;        // tracks used for animation
  int current_track=0;  //current track
  int current_index=0;  //current index of frame in current track
  int current_ticks=5;  //current number of ticks left

  public AnimatedSprite () {}  //empty constructor
    
  public AnimatedSprite(Image images, int frames) {
    //initializes this to be a sprite with several frames
    //default location of sprite is (0,0)
    super(images);
    super.width /= frames;   //width of each individual frame
    this.frames = frames;
    tracks = new Vector();
  }

  public AnimatedSprite(Image images, int frames, int x, int y) {
    //initializes this to be a sprite with several frames
    //starting location of sprite is (x,y)
    super(images, x, y);
    super.width /= frames;
    this.frames = frames;
    tracks = new Vector();
  }

  public void addState(int track, int frame, int ticks, int dx, int dy) {
    //adds a frame to track

    //create new Frame
    Frame f = new Frame (frame, ticks, dx, dy);
    //if tracks does not contain track, add it
    if (tracks.size() <= track) {  //this allows non-sequential track numbers
      tracks.setSize(track+1);
    }
    
    //if track does not exist, create a new Vector for it
    if (tracks.elementAt(track) == null) {
      tracks.setElementAt(new Vector(), track);
    }
    //add frame to track
    Vector v = (Vector)tracks.elementAt(track);
    v.addElement(f);
  }  
  
  public void Draw(Raster bgnd) {
    //if ticks has run out, go to next frame
    if (current_ticks <= 0) {
      nextState();
    }

    //get frame to draw
    Vector v = (Vector)tracks.elementAt(current_track);
    Frame f = (Frame)v.elementAt(current_index);

    //draw frame
    int framestart_x = (f.getFrameNum())*super.width;
    int bgndend_x = (bgnd.getWidth() < super.x+super.width) ? bgnd.getWidth() : super.x+super.width;
    int bgndend_y = (bgnd.getHeight() < super.y+super.height) ? bgnd.getHeight() : super.y+super.height;
    
    for (int i=super.x; i<bgndend_x; i++) {
      for (int j=super.y; j<bgndend_y; j++) {
	if (i>=0 && j>=0) {
	  if (this.getPixel(i-super.x+framestart_x, j-super.y) >> 24 != 0) {
	    bgnd.setPixel(this.getPixel(i-super.x+framestart_x, j-super.y),
			  i, j);
	  }
	}
      }
    }
    //decrement number of ticks left to display current frame
    current_ticks--;
  }
  
  public void nextState() {
    //go to next frame is ticks has run out
    if (current_ticks <= 0) {
      Vector v = (Vector)tracks.elementAt(current_track);
      //if end of track, keep drawing current frame
      //else go to the next frame in the track
      if (current_index < ((Vector)tracks.elementAt(current_track)).size()-1) {
	current_index++;
	Frame f = (Frame)v.elementAt(current_index);
	super.x += f.getDx();
	super.y += f.getDy();
	current_ticks = f.getTicks();
      }
    }  
  }
  
  public void setTrack () {
    //set current track to default track 0
    current_track=0;
    current_index=0;
    current_ticks=5;
  }
  
  public void setTrack(int track) {
    //set current track to track
    current_track = track;
    current_index=0;
    current_ticks=5;
  }
  
  public void setXY (int x, int y) {
    //set the location of this on the playfield
    super.x = x;
    super.y = y;
  }
  
} //end AnimatedSprite class


