import java.awt.*;
import java.awt.image.*;


class Playfield extends Raster {
  Raster background;          // background image
  Sprite sprite[];            // list of sprites on this playfield     
  int n;                      // number of sprites in playfield

  public Playfield(Image bgnd, int numSprites){         // initialize sprite with an image
    super(bgnd);
    n = numSprites;                                    
    sprite =  new Sprite[n];
    background = new Raster(bgnd);                      // maintains background raster
  }

  public void addSprite(int i, Sprite s){
    sprite[i] = s;
  }
  
  // draw playfield raster        
  public void Draw(){                          
    int width = getWidth();
    int height = getHeight();
    
    // read the pixels from the background raster into the playfield raster
    for (int i = 0; i < width; i++)
      for (int j = 0; j < height; j++)
        pixel[j*width+i] = background.pixel[j*background.getWidth()+i];

    // draw sprites onto playfield raster
    for (int k = 0; k < n; k++)
      sprite[k].Draw(this);
  }

  // reDraw playfield raster after movement of sprites
  public void reDraw(int index, int x, int y) {
    int width = sprite[index].getWidth();
    int height = sprite[index].getHeight();

    // read the pixels from the background raster into the playfield raster
    for (int i = 0; i < width; i++)
      for (int j = 0; j < height; j++)
        if ((x+i >= 0) && (x+i < background.getWidth()) &&
            (y+j >= 0) && (y+j < background.getHeight()))
          pixel[(y+j)*this.width+x+i] = background.pixel[(y+j)*background.getWidth()+x+i];
    
    // draw sprites onto playfield
    for (int k = 0; k < n; k++)
      sprite[k].Draw(this);
 }
  
  // tells in which Sprite, if any, a set of coords. x, y is
  public int isSprite(int x, int y) {
    int status1 = -1;
    int status2 = -1;
    for (int k = 0; k < n; k++){
       if(sprite[k].inSprite(this, x, y) > 0)
         status1 = k;
       else
         status2 = -2;
    }
    if(status1 != -1)
      return status1;
    else return status2;
  }
 }




