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

/* $Id: Breakout.java 1602 2011-04-14 02:51:26Z 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;
    
    // number of turns per game
    private static final int NUM_TURNS = 3;

    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;
    
    // turns remaining
    private int turnsLeft;
    
    // array of FilledOvals representing turns remaining
    private FilledOval[] lives;
    
    /**
     * 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;
        
        // initialize number of turns
        turnsLeft = NUM_TURNS;
        lives = new FilledOval[NUM_TURNS];
        
        // create visual display of turns remaining
        double nextXBall = BORDER_WIDTH;
        double yBallPos = (BORDER_WIDTH - BALL_DIAMETER)/2;
        for (int ballNum = 0; ballNum < NUM_TURNS; ballNum++) {
            lives[ballNum] = new FilledOval(nextXBall, yBallPos, 
                                 BALL_DIAMETER, BALL_DIAMETER,
                                 canvas);
            nextXBall = nextXBall + 2*BALL_DIAMETER;
        }
    }
    
    /**
     * create a ball if no ball is currently in play
     * 
     * @param point not used
     */
    public void onMousePress(Location point) {
        
        if ((turnsLeft > 0) && (lastBall == null || lastBall.outOfPlay())) {
            lastBall = new BreakoutBall(BALL_DIAMETER, paddle, bricks, boundary, canvas);
            turnsLeft--;
            lives[turnsLeft].removeFromCanvas();
        }
    
    }

    /**
     * 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());
        }
    }
}
