import Raster;
import Sprite;
import java.awt.*;
import java.awt.image.*;


//*******************************************************************
/* The State class is basically a "record" sort of speak that holds
   the relevant information to an individual state in a "track".
   Each state holds the dx -- position in x that the state moves from the 
   prior state; dy -- position in y that the state moves from the prior
   state; the number of ticks -- number of cycles before the state moves
   and the frame -- the actual frame that the state represents */

//*******************************************************************

class State
{
  protected int dx, dy, ticks, frame;
  public State(int dxVal, int dyVal, int ticksVal, int frameVal)
   
       /*This constructor initializes the state with the proper ticks,
	 dx, dy, and corresponding frame */
  {
    ticks = ticksVal;
    dx = dxVal;
    dy = dyVal;
    frame = frameVal;
  }
  
//*******************************************************************
  public int getDx()
  {
    return dx;
  }
  //*******************************************************************
  public int getDy()
  {
    return dy;
  }
  //*******************************************************************
  public int getTicks()
  {
    return ticks;
  }
  //*******************************************************************
  public int getFrame()
  {
    return frame;
  }
}

//*******************************************************************
/*
  An animated sprite is a sprite. However, it has many different frames
  required for the animation. The number of frames holds the number of 
  frames in the sprite. The currentState is the state at which the sprite
  is at. And the numberStates, is the total number of states the
  animatedSprite has. And Track, is an array of states...   
 */

class AnimatedSprite extends Sprite
{
  protected int frames;  // number of frames in sprite
  protected int dx, dy, ticks;
  protected int currentState;
  protected int currentTicks;
  protected int numberStates;
  protected State Track[];
  //*******************************************************************
  public AnimatedSprite(Image images, int numberFrames, int xLoc, int yLoc)
  {
    super(images);
    frames = numberFrames;
    currentState = 0;
    x = xLoc;
    y = yLoc;
  }
  //*******************************************************************
  public void Draw(Raster bgnd)
       /* This Draw methods is resetting the location of x and y
	  in case it scrolls of the background */
  {
    if (x<(-(width/frames)))
      x=bgnd.getWidth();
    if (x>(bgnd.getWidth()))
      x=(-(width/frames));
    if (y>(bgnd.getHeight()))
      y=(-height);
    if (y<-height)
      y=(bgnd.getHeight());
    super.Draw(bgnd);
  }
  //*******************************************************************
  public void addState(int stateNo, int frame, int ticks, int dx, int dy)
  {
    Track[stateNo] = new State(dx, dy, ticks, frame);
  }
  //*******************************************************************
  public void updateState()
       /* The frameB is the beginning of the respective frame 
	  The frameE is the end of the respective frame
	  */
  {
    
    frameB = (Track[currentState].getFrame())*(width/frames);
    frameE = frameB + (width/frames);
  }

  //*******************************************************************
  public int getFrameWidth()
  {
    return (width/frames);
  }
  //*******************************************************************
  public int getFramePixel(int xLoc, int yLoc)
  {
    return getPixel((xLoc + frameB), yLoc);
  }
  //*******************************************************************
  public void nextState()
       // Update to the next state checking boundary conditions
  {
    currentTicks++;
    if (currentTicks == Track[currentState].getTicks())
      {
	currentState++;
	currentTicks = 0;
	if (currentState > numberStates)
	  currentState=0;
	x = x+Track[currentState].getDx();
	y = y+Track[currentState].getDy();
	updateState();
      }
  }
  //*******************************************************************
  public void setTrack(int numF)
       /* Set the track... */
  {
    Track = new State[numF];
    currentState = 0;
    numberStates = numF-1;
  }
}



















