import java.awt.*;

class Playfield extends Raster {
  Raster background;
  Sprite sprite[];
  int num_sprites;

  public Playfield(Image bgnd, int numSprites){
    super(bgnd);
    background = new Raster(bgnd);
    sprite = new Sprite[numSprites];
    num_sprites = numSprites;
  }
  public void addSprite(int i, Sprite s){
    sprite[i] = s;
  }
  public void Step(){
    // send a Step to each sprite
   int k;
   for (k=0; k<num_sprites; k++)
     sprite[k].Step();
  }
  public void Draw(){
    // overwrite with the background
    int j;
    System.arraycopy(background.pixel, 0, this.pixel, 0, this.pixel.length);
    //this.pixel = background.pixel;
    // now draw each sprite. Altitude is determined by index in array
    for (j=0; j<num_sprites; ++j){
      //      if (j< num_sprites)
      //      if (sprite[j].x>=0 && sprite[j].x< background.width-sprite[j].width && sprite[j].y >= 0 && sprite[j].y <= (background.height)-sprite[j].height)
	sprite[j].Draw(this);
    
      // now if sprites are against a wall, bounce them
      if (sprite[j].x <= 0)
	sprite[j].bounceRight();
      if (sprite[j].x >= background.width-sprite[j].width)
	sprite[j].bounceLeft();
      if (sprite[j].y <= 0)
	sprite[j].bounceTop();
      if (sprite[j].y >= (background.height)-sprite[j].height)
	sprite[j].bounceBottom();
    }
    // and that's all.
  }

  public void MouseThis(int x, int y){
    // the point of this is that you can click on
    // sprites to get them to speed up.
    int k;
    for (k=0; k<num_sprites; k++){
      if (sprite[k].TestCollision(x,y) != 0){
	// let's set a max speed of 10 in any direction
	if ((sprite[k].dx >= 10) || (sprite[k].dy >= 10)) {
	  sprite[k].bounceHoriz();
	  //System.out.println("Whoa dogies!");
	} else {
	  sprite[k].bounceVert();
	  sprite[k].dx *= 2;
	  sprite[k].dy *= 2;
	  //System.out.println("Yee haw!");
	}
      }
    }
  }
}
