/* Function prototypes originally from 6.837 website */

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

class AnimatedSprite extends Sprite {
  int frames;         // frames in sprite
  int current_frame;
  int tick_count;
  /* State arrays */
  int dx_array [];          // array of dx's for each state  
  int dy_array [];          // array of dy's for each state
  Sprite sprite_array [];   // array of sprites for each state
  int tick_array [];        // length of time to stay at state
   
  /***** Constructors *****/

  /* This created an animated sprite that is essentially the same
   * as a normal Sprite, with the specified image, and the total
   * number of states specified.
   */
  public AnimatedSprite (Image first_image, int number_frames)
  {
    /* Create a sprite for the first image. */
    super (first_image);

    frames = number_frames;
    current_frame = 0;
    tick_count = 0;
    dx_array = new int [frames];
    dy_array = new int [frames];
    sprite_array = new Sprite [frames];
    tick_array = new int [frames];

    /* Initialize all of the states. */
    for (int i = 0; i < frames; i++)
      {
	dx_array [i] = 0;
	dy_array [i] = 0;
	sprite_array [i] = new Sprite (first_image);
	tick_array [i] = 0;
      }
  }

  /***** Methods *****/
  
  /* Defining States */
  
  public void setStateImage (int frame, Image img)
  {
    if ((frame >= 0) || (frame < frames))
      sprite_array [frame] = new Sprite (img);
  }

  public void setStateMovement (int frame, int dx, int dy)
  {
    if ((frame >= 0) || (frame < frames))
      {
	dx_array [frame] = dx;
	dy_array [frame] = dy;
      }
  }

  public void setStateTicks (int frame, int ticks)
  {
    if ((frame >= 0) || (frame < frames))
      tick_array [frame] = ticks;
  }

  /* Display methods */
  public void Draw(Raster bgnd)
  {
    sprite_array [current_frame].setCoordinates (this.x, this.y);
    sprite_array [current_frame].Draw (bgnd);
  }

  public void nextTick()
  {
    /* Check and see if we're supposed to move to the next state yet. */
    if (tick_count < tick_array [current_frame])
      tick_count++;
    else
      {
	/* Ok, now we have to move to the next state... */
	tick_count = 0;
	
	current_frame++;
	if (frames == current_frame)
	  current_frame = 0;

	this.height = sprite_array [current_frame].height;
	this.width = sprite_array [current_frame].width;
	this.pixel = sprite_array [current_frame].pixel;
	this.x = this.x + dx_array [current_frame];
	this.y = this.y + dy_array [current_frame];
      }
  }

  
}
