import java.awt.*;
import java.awt.image.*;
import java.math.*;

class AnimatedSprite extends Sprite {
              int frames;         // frames in sprite
              // the individual frames that compose the animated sprite
              // will be kept in an array of arrrays of pixels.
              // (each frame is an array of pixels)
              int[][] frameArray;  
              private int curFrame=0;
              int frameWidth, frameHeight;
              //these will be used for moving the sprite
              int startx=x, endx=x, starty=y, endy=y;
              int counter=0; 
              // flag 
              boolean move = false;                   

          

	public AnimatedSprite(Image images, int numframes)
	{
	  super(images);
	  // create the array of frames	 
	  frameArray=new int[numframes][width*height];
	  frames = numframes;
	  // now go thru the big image and split it into individual frames
	  for (int i=0; i < frames; i++)
	  {
	    try {			
	      PixelGrabber grabber = new PixelGrabber(images,
              width/frames*i, 0, width/frames, -1, true);
	      if (grabber.grabPixels()) {
		frameWidth = grabber.getWidth();
                frameHeight = grabber.getHeight();
                frameArray[i] = (int []) grabber.getPixels();              
	      }
	    }
	    catch (InterruptedException e) {
	    }
	  }
	}

	public void nextState()
	{
	  int dx; 
	  int dy;
	  // first check if we need to move at all - otherwise do nothing
	  if (move)
	  {
	    // now calculate the direction into which we need to go
	    if ((endx-startx)!=0)
	       int slope = Math.abs((endy-starty)/(endx-startx));
	    // handle the special case when moving along y-axis
	    else
	    {
		dx=0;
		slope = 1;
	     }
	    // the sprites will move by 20 pixels at a time 
	    // in each direction	
	    if (endx<startx)
	       dx=-20;
	    else
	       dx=20;
	    if (endy<starty)
	       dy=-20;
	    else
	       dy=20;
				
	    dy=dy*slope;
	    // ideally, the idea was to move the sprite to the mouse,
	    // but I ran out of time, so the sprite will take
	    // 7 steps towward the mouse
	   if (counter < 7)
	   {x=x+dx;
	    y=y+dy;
	    counter++;
	   }
	   else
	   {move=false;
	    counter = 0;
	    //System.out.println("set move to false");
	   }
	  }
	}
	
	public void Draw(Raster bgnd)
	{	
	  // take the current frame and draw it
		pixels=frameArray[curFrame];
		width=frameWidth;
		height=frameHeight;
		super.Draw(bgnd);
	 // set the next frame to be drawn 
		curFrame = (curFrame + 1) % frames;
	}
}
