/**
 *This is the AnimatedSprite class derived from Sprite.
 *(C)Hongsong Chou for 6.837(F98) project 1.
 *It can be compiled using javac on athena
 */
import java.awt.*;
import java.awt.image.*;
import Sprite;

class AnimatedSprite extends Sprite {
  int frames;//number of frames in Sprite
  int tracks;//number of displays needed in the Animated Sprite
  long duration[];//ticks for each display
  int right;//if =1, animation towards right, if =-1, towards left
  int tr;//the index of current display
  int f[], dx[], dy[];//frame index, displacement of each movement  
  int xx, yy;//used when clipping each frame from Sprite image
  
  public AnimatedSprite(Image images, int frames) {
    super(images);
    this.frames = frames;
  }
  
  public void addState(int tracks, int [] frame, 
		       long [] ticks, int [] d_x, int [] d_y, int direction) {
    this.tracks = tracks;
    right = direction;
    duration = new long[this.tracks];
    duration = ticks;
    f = new int[this.tracks];
    f = frame;
    dx = new int[this.tracks];
    dx = d_x;
    dy = new int[this.tracks];
    dy = d_y;
  }
  
  public void nextState() {
    if(tr < tracks) {
      x += dx[tr]*right;//in the current version, displays moves only along x
      y += dy[tr];
      xx = x - f[tr] * width/frames;
      yy = y;
      tr++;
    }
  }
    
  public void setTrack() {
    tr = 0;
  }

  public void Draw(Raster bgnd) {
    //This method overwrites Sprite.Draw in that it clips each frame from 
    //Sprite image
    int bgnd_w = bgnd.getWidth();
    int bgnd_h = bgnd.getHeight();
    for(int j = 0; j < bgnd_h; j++)
      for(int i = 0; i < bgnd_w; i++)
	{
	  if( i < x + width/frames && i >= x
	      && j < y + height && j >= y )
	    //then, overlaps
	    {
	      if( !isSpriteTransp(i-xx , j-yy) )
		bgnd.setPixel(pixel[(j-yy)*width+(i-xx)],i,j);
	    }
	}
  }
}




















