import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;


public class Game {
	
	Bat bat;
    int numBalls;
	Ball ball;
	ArrayList<Block> blocks; 

	
	static double xdim = 300.0;
	static double ydim = 250.0;

	static int rows = 4;
	static int cols = 10;
	static double colGap = xdim/(cols+1);
	static double rowGap = 15.0;
	
	
	
	public Game(Screen screen) {
		
		
		
		bat = new Bat();

		ball = new Ball(xdim/2.0, ydim/2.0);
		blocks =  new ArrayList<Block>();
	    for (int r = 0 ; r < rows; r++) {
	    	for (int c = 0 ; c < cols; c++){
	    		Block block = new Block(colGap*(c+1), rowGap*(r+1));
	    		blocks.add(block);
	    	}
	    	
	    }
		numBalls = 3;

		screen.addMouseMotionListener(bat);
		
	}
	
	public void draw(Graphics g) {
		bat.draw(g);

		for (Block b : blocks) {
			b.draw(g);
		}
		ball.draw(g);

	}
	

	public void step() {
		ball.step();
		
		Iterator<Block> it = blocks.iterator();
		while(it.hasNext()) {
			Block block = it.next();
			
		    if (block.hit(ball)) { 
		    	it.remove();
		    	ball.vy = -ball.vy;
		    }
		}
		
		if (bat.hit(ball) && ball.vy > 0.0) {
			ball.vy = - ball.vy;
		}
		
		if (ball.offScreen()) {
			numBalls--;
			ball = new Ball(xdim/2.0, ydim/2.0);
		}
		
	}
	

}

