import java.awt.*;
import java.util.*;
import Raster;
import Sprite;
import AnimateStruct;

class AnimatedSprite extends Sprite {
  
  //int pixel[];
  int frames; // vital data
  Raster u_stor_it[]; // all the pixel's
  Vector tracks = new Vector(); // uberstruktur
  int cur_track; // animation sequence variable
  int cur_tick; // time keeper
  int cur_loc; // current location in track
  AnimateStruct cur_anim;
  //int dx, dy; // current velocity
  // see Sprite for bouncing and stuff

  public AnimatedSprite(Image images, int frameinfo, int x, int y, int dx, int dy){
    // so we know how many frames there are; make a sprite out of
    // each frame.    
    Raster scrap;
    int i, j, bob, wper, k;
    
    // initialize the storage area
    u_stor_it = new Raster[frameinfo];
    scrap = new Raster(images);
    wper = scrap.width / frameinfo; // wper = 'width per (frame)'
    pixel = new int[wper*scrap.height];
    for(bob=0; bob<frameinfo; bob++){ // iterate over the frames and build em
      u_stor_it[bob] = new Raster(wper, scrap.height);
      k = wper*bob; // offset multiplication brought outside of innerloop
      for(i=wper*bob; i<wper*(bob+1); i++){
	for(j=0; j<scrap.height; j++){
	  u_stor_it[bob].setPixel(scrap.getPixel(i, j), i-k, j);
	}
      }
    }
    for(i=0; i<wper; i++){
      for(j=0; j<scrap.height; j++){
	setPixel(scrap.getPixel(i, j), i, j);
      }
    }
    
    // ok. now the u_stor_it array is constructed. Set the internal
    // sprite to the first image, set location to (0,0) and then
    // bail.
    this.width = wper;
    this.height = scrap.height;
    this.x = x;
    this.y = y;
    //   System.arraycopy(u_stor_it[0].pixel, 0, this.pixel, 0, u_stor_it[0].pixel.length);
   //pixel = u_stor_it[0].pixel;    
   cur_track = 0;
   cur_tick = 0;
   cur_loc = 0;
   this.dx = dx;
   this.dy = dy;
   this.frames = frameinfo;
  }
  
  
  public void addTrack(){
    tracks.addElement(new Vector());
  }

  public void addState(int tracknum, int framenum, int ticks){
    Vector framelist = (Vector)tracks.elementAt(tracknum);
    // make the new AnimateStruct
    AnimateStruct phil = new AnimateStruct(framenum, ticks);
    // put the new struct in the frame list
    framelist.addElement(phil);
    // put it back modified
    tracks.setElementAt(framelist, tracknum);
    //if (cur_anim = null)
    // cur_anim = phil;
  }
  
  // We won't need this - Sprite will deal appropriately
  //  public void Draw(Raster bgnd){
  // }
  //  public void Draw(Raster bgnd){
  // int i,j;
  // int pix;
  // System.out.println("Earth thinks it's drawing");
  // for(i=x; i<(x+width); i++){
  //   for(j=y; j<(y+height); j++){
  //	if (i<0 | j<0 | i>bgnd.width | j>bgnd.height)
  //	  continue;
  //	pix = getPixel(i-x, j-y);
  //	if ((pix >>> 24) != 0)
  //	  bgnd.setPixel(pix, i, j);
  //   }
  // }        
  //}
  public void nextState(){
    cur_tick++;
    if (cur_tick == cur_anim.ticks) {
      cur_tick = 0;
      x += dx;
      y += dy;
      if (cur_loc == (((Vector)tracks.elementAt(cur_track)).size()-1)){
	//System.out.println("Wraparound");
	cur_loc = 0;
      } else {
	cur_loc++;
      }
      cur_anim = (AnimateStruct)((Vector)tracks.elementAt(cur_track)).elementAt(cur_loc);
      spriteUpdate(cur_track, cur_loc);      
    }
  }

  public void setTrack(int tracknum){
    cur_track = tracknum;
    cur_tick = 0;
    cur_anim = (AnimateStruct)((Vector)tracks.elementAt(tracknum)).elementAt(0);
    spriteUpdate(tracknum, 0);
  }
  
  public void spriteUpdate(int tracknum, int loc){
    //System.arraycopy(u_stor_it[((AnimateStruct)((Vector)tracks.elementAt(tracknum)).elementAt(loc)).framenum].pixel, 0, pixel, 0, u_stor_it[((AnimateStruct)((Vector)tracks.elementAt(tracknum)).elementAt(loc)).framenum].pixel.length);
    pixel = u_stor_it[((AnimateStruct)((Vector)tracks.elementAt(tracknum)).elementAt(loc)).framenum].pixel;
    //System.out.println("Earth is turning");
  }

  public void Step(){
    super.Step();
    this.nextState();
    
  }
}
