import java.awt.*;
import java.awt.image.*;

class AnimatedSprite extends Sprite {
    int frames;         // frames in sprite
                        // there are other private variables
    Track[] track;      // An array of tracks that can be run
    int currentTrack;   // The current track being run
    int currentState;   // The current state, within that track
                        	
    public AnimatedSprite(Image images, int numFrames, int numTracks) {
        super(images);
    	frames = numFrames;
    	track = new Track[numTracks];
    	currentTrack = 0;
    	currentState = 0;
    }
    
    public AnimatedSprite() {
    
    }
    
    public AnimatedSprite copySprite() {
    	AnimatedSprite newSprite = new AnimatedSprite();
    	newSprite.pixel = this.pixel;
    	newSprite.width = this.width;
    	newSprite.height = this.height;
    	newSprite.x = this.x;
    	newSprite.y = this.y;
    	newSprite.transparent_color = this.transparent_color;
    	newSprite.frames = this.frames;
    	newSprite.track = this.track;
    	newSprite.currentTrack = this.currentTrack;
    	newSprite.currentState = this.currentState;
		return newSprite;
    }
    
    //public void addState(int track, int frame, int ticks, int dx, int dy) {
    //}
    
    public boolean onSprite(int xclick, int yclick) {
    	int frameWidth = width/frames;
		int xframe = frameWidth*(track[currentTrack].frameSequence[currentState]);

		// If the click missed the entire raster, return false...
		if (xclick < x || xclick > x+frameWidth || yclick < y || yclick > y+height) {
			return false;
		}
		
		// Otherwise, see if the click hit a non-transparent pixel...
    	if (this.getPixel(xframe+xclick-x,yclick-y) != transparent_color) {
    		return true;
    	}
    	else return false;
    }
    
	public boolean inState(int t, int s) {
		// Returns true if the animated sprite is in track t, state s.
		if (currentTrack == t && currentState == s) {
			return true;
		}
		return false;
	}

    public void Draw(Raster bgnd) {
		int frameWidth = width/frames;
		int xframe = frameWidth*(track[currentTrack].frameSequence[currentState]);
		
		// Move the animated sprite just before drawing it so that
		// x and y always indicate its ACTUAL position
		x += track[currentTrack].dx[currentState];
		y += track[currentTrack].dy[currentState];

    	for (int ystep = 0; ystep<height; ystep++) {
    		for (int xstep=0; xstep<width/frames; xstep++) {
    		    if (this.getPixel(xstep+xframe,ystep) != transparent_color) {
    				bgnd.setPixel(this.getPixel(xstep+xframe,ystep),x+xstep,y+ystep);
    			}
    		}
    	}

    }
        
    public void nextState() {
    	if (currentState+1 >= track[currentTrack].frameSequence.length) {
    		currentState = 0;
    	}
    	else currentState++;
    }
    
    public void startTrack(int tracknum) {
    	currentTrack = tracknum;
    	currentState = 0;
    }
    
    public void addTrack(int i, Track t) {
    	track[i] = t;
    }
}
