import java.awt.*;
import java.util.*;

/** class BrickoutPlayfield extends Playfield
  *
  * class BrickoutPlayfield implements a Playfield that handles some game 
  * rules of Brickout.
  */
class BrickoutPlayfield extends Playfield {
  Sprite gameover, youwin;
  int lives = 3;

  public BrickoutPlayfield(Image background, int w, int h,
			   Sprite go, Sprite win) {
    super(background, w, h);
    gameover = go;
    youwin = win;
  }

  // BrickoutPlayfield.deleteSprite -- removes this sprite from the 
  //    playfield, check if that's all of them.
  public void deleteSprite(Sprite s) {
    super.deleteSprite(s);
    // is that all the bricks?
    for (Enumeration e = sprites.elements(); e.hasMoreElements();) {
      Sprite t = (Sprite)e.nextElement();
      if (t instanceof Brick) return;
    }

    // You win! Delete the ball and put up the sign.
    for (Enumeration e = sprites.elements(); e.hasMoreElements();) {
      Sprite t = (Sprite)e.nextElement();
      if (t instanceof Ball) deleteSprite(t);
    }
    addSprite(youwin, true);
    youwin.setPlayfield(this);
    youwin.setPos(0,(height-youwin.height)/2);
  }

  // We've lost the ball for some reason (fallen off the bottom of
  // the screen blown up by a bomb, whatever.) What now?
  public void lostBall(Sprite s) {
    lives--;
    if (lives == 0) {
      deleteSprite(s);
      addSprite(gameover);
      gameover.setPlayfield(this);
      gameover.setPos(0,(height-gameover.height)/2);
    } else {
      // reset ball
      s.setPos(0,height-s.height);
      s.setVelocity(5,-5);
    }
  }

}
