import java.awt.*;
import java.awt.image.*;
	
class Playfield extends Raster {
    Raster background;          // background image
    Sprite sprite[];    // list of sprites on this playfield...
    AnimatedSprite animatedSprite[];  // ...and the animated sprites
    int counter;	

    public Playfield(Image bgnd, int maxSprites, int maxAnimatedSprites) {
    
		// Initialize the pixel array
		super(bgnd);
		
		counter = 0;
		
    	background = new Raster(bgnd);
    	sprite = new Sprite[maxSprites];
    	animatedSprite = new AnimatedSprite[maxAnimatedSprites];
    }
    
    public int onSprite(int xclick, int yclick) {
    	// Check to see if the user clicked on a sprite.
    	//   If yes, return the index of the sprite
    	//   Otherwise, return -1.
    	
    	for (int i=0; i<sprite.length; i++) {
    	    if (sprite[i] != null) {
    	        if (sprite[i].onSprite(xclick, yclick)) {
    	        	return i;
    	        }
    	    }
    	}
    	return -1;
    }
    
    public int onAnimatedSprite(int xclick, int yclick) {
    	// Check to see if the user clicked on an animated sprite.
    	//   If yes, return the index of the anmiated sprite
    	//   Otherwise, return -1.
    	
    	for (int i=0; i<animatedSprite.length; i++) {
    	    if (animatedSprite[i] != null) {
    	        if (animatedSprite[i].onSprite(xclick, yclick)) {
    	        	return i;
    	        }
    	    }
    	}
    	return -1;
    }
    
    public void addSprite(int i, Sprite s) {
    	sprite[i] = s;
    }

    public void addAnimatedSprite(int i, AnimatedSprite s) {
    	animatedSprite[i] = s;
    }
    
    public void Draw() {
    	//First, redraw the background to the playfield...
    	for (int i=0; i<pixel.length; i++) {
    		this.pixel[i] = background.pixel[i];
    	}
    	
    	// Now draw the animated sprites sequentially...
    	for (int i=0; i<animatedSprite.length; i++) {
    	    if (animatedSprite[i] != null) {
    	        // Draw each sprite directly to the playfield, 
    	        //   (NOT to the background)
    	        animatedSprite[i].nextState();
    	    	animatedSprite[i].Draw(this);
    	    }
    	}
    	
    	// Finally, draw the sprites sequentially...
    	for (int i=0; i<sprite.length; i++) {
    	    if (sprite[i] != null) {
    	        // Draw each sprite directly to the playfield, 
    	        //   (NOT to the background)
    	    	sprite[i].Draw(this);
    	    }
    	}
    }
}
