import objectdraw.*;
import java.awt.*;

/* $Id: Breakout.java 1593 2011-04-07 01:01:17Z terescoj $ */

/**
 * Example Breakout: play a simple breakout game
 *
 * @author Jim Teresco, Siena College, CSIS 120, Fall 2011
 *
 */

public class Breakout extends WindowController {

    // position and dimensions of the court
    private static final double BORDER_WIDTH = 25;

    // dimensions of the paddle
    private static final double PADDLE_WIDTH = 50;
    private static final double PADDLE_HEIGHT = 20;

    // dimensions of the ball
    private static final double BALL_DIAMETER = 20;
    
    // numbers and sizes of bricks
    private static final int NUM_BRICKS_PER_ROW = 10;
    private static final int NUM_BRICK_ROWS = 6;
    private static final double BRICK_HEIGHT = 10;
    private static final double BRICK_TOP_OFFSET = 50;

    private FilledRect paddle;
    private FramedRect boundary; // the boundary of the playing area.

    // last ball in play
    private BreakoutBall lastBall;

    // the collection of bricks to hit
    private BrickCollection bricks;
    
    /**
     * set up playing area and bricks.
     */
    public void begin() {
        
        new FilledRect(BORDER_WIDTH, BORDER_WIDTH,
                        canvas.getWidth() - BORDER_WIDTH*2, 
                        canvas.getHeight() - BORDER_WIDTH*2, canvas).setColor(Color.gray);
                        
        // make the playing area
        boundary = new FramedRect(BORDER_WIDTH, BORDER_WIDTH,
                        canvas.getWidth() - BORDER_WIDTH*2, 
                        canvas.getHeight() - BORDER_WIDTH*2, canvas);

        // make the paddle
        paddle = new FilledRect(canvas.getWidth()/2,
            canvas.getHeight() - BORDER_WIDTH - PADDLE_HEIGHT -1,
            PADDLE_WIDTH, PADDLE_HEIGHT,
            canvas);
            
        // set up the bricks
        bricks = new BrickCollection(NUM_BRICKS_PER_ROW, NUM_BRICK_ROWS, 
                     BRICK_HEIGHT, BRICK_TOP_OFFSET+BORDER_WIDTH,
                     BORDER_WIDTH, boundary.getWidth(), canvas);
            
        // no ball in play
        lastBall = null;

    }
    
    /**
     * create a ball if no ball is currently in play
     * 
     * @param point not used
     */
    public void onMousePress(Location point) {
        
        if (lastBall == null || lastBall.outOfPlay()) {
            lastBall = new BreakoutBall(BALL_DIAMETER, paddle, bricks, boundary, canvas);
        }
    
    }

    /**
     * move the paddle to follow the mouse's x position
     * 
     * @param point the current Location of the mouse pointer
     */
    public void onMouseMove(Location point) {

        if (point.getX() < boundary.getX()) {
            // place paddle at left edge of the court
            paddle.moveTo(boundary.getX(), paddle.getY());
        } else if (point.getX() + PADDLE_WIDTH > boundary.getX() + boundary.getWidth()) {
            // place paddle at right edge of the court
            paddle.moveTo(boundary.getX() + boundary.getWidth() - PADDLE_WIDTH, paddle.getY());
        } else {
            // keep the edge of the paddle lined up with the mouse
            paddle.moveTo(point.getX(), paddle.getY());
        }
    }
}
