import objectdraw.*;

/*
 * An object to represent a ball that falls from the
 * top of the screen to the bottom.
 */
public class FallingBall extends ActiveObject {

  // size and speed of balls
  private static final int BALLSIZE = 30;
  private static final double Y_SPEED = 4;
  private static final int DELAY_TIME = 20;
  private static final int TOP = 50;

  // relevant dimensions of playing area
  private static final int BOTTOM = 500;
  private static final int SCREENWIDTH = 400;

  // the falling object
  private FilledOval ball;
  private FilledRect paddle;
  private double topBoundary;
  private double bottomBoundary;

  public FallingBall(FramedRect boundary, FilledRect aPaddle,
  DrawingCanvas aCanvas) {

    ball = new FilledOval(SCREENWIDTH/2 ,TOP,
      BALLSIZE, BALLSIZE, aCanvas);
    paddle = aPaddle;
    topBoundary = boundary.getY();
    bottomBoundary = aCanvas.getHeight();

    this.start();
  }

  public void run() {
    // move the ball repeatedly until it fall off screen
    double speed = Y_SPEED;

    while (ball.getY() < bottomBoundary) {
      
      ball.move(0, speed);

      if (ball.overlaps(paddle)) {
        speed = -Y_SPEED;
      } else if (ball.getY() < topBoundary) {
        speed = Y_SPEED;
      }  

      pause(DELAY_TIME);
    }

    ball.removeFromCanvas();
  }

}
