
public class Field {
	private Ball ball;
	private Bar bar;
	private int score;
	private int life;
	private Block_Field[][] bf = new Block_Field[10][12];
	private int [][] stage =  {
			                    {21,21,21,21,21,21,21,21,21,21,21,21},
								{11,11,12,12,12,12,12,12,12,12,11,11},
								{21,01,02,02,22,02,02,02,22,32,31,31},
								{01,21,02,02,03,23,03,03,02,22,01,01},
								{01,01,22,02,03,43,23,03,02,02,21,01},
								{01,01,02,22,03,43,43,23,02,02,01,21},
								{11,01,02,02,13,43,43,03,12,02,01,01},
								{01,11,02,02,02,12,02,02,32,12,31,31},
								{01,01,12,02,02,02,12,02,02,02,11,01},
								{01,01,01,11,01,01,01,11,31,31,31,21},
								};
	private boolean blockhit;
	private Block_Break bb;
	public Field(String s,Block_Break bb){
		this.bb = bb;
		ball = new Ball();
		bar = new Bar();
		if(s.equals("‚q")){
			for(int i = 0;i < stage.length;i++){
				for(int j = 0;j < stage[i].length;j++){
					stage[i][j] = (int)(Math.random() * 4);
					if(stage[i][j] > 0){
						int itemno = (int)(Math.random() * 9);
						if(itemno <= 4){
							itemno = 0;
						}else{
							itemno = itemno - 4;
						}
						stage[i][j] = itemno * 10 + stage[i][j];
					}
				}
			}
		}
		for(int i = 0;i < stage.length;i++){
			int y = 15 + i * 17;
			for(int j = 0;j < stage[i].length;j++){
				int x = 24 + j * 22;
				if(stage[i][j] % 10 > 0){
					Item item;
					switch(stage[i][j] / 10){
						case 1:
							item = new BigBall_Item(0,x,y);
							break;
						case 2:
							item = new SmallBall_Item(1,x,y);
							break;
						case 3:
							item = new BigBar_Item(2,x,y);
							break;
						case 4:
							item = new SmallBar_Item(3,x,y);
							break;
						default:
							item = null;
					}
					bf[i][j] = new Block_Field(x,y,item,stage[i][j] % 10);
				}else{
					bf[i][j] = null;
				}
			}
		}
		blockhit = false;
		score = 0;
		life = 3;
	}
	public boolean itemHit(int itemIndex_x, int itemIndex_y) {
		int bar_x = bar.getBarIndex_x();
		int bar_y = bar.getBarIndex_y();
		int bar_length = bar.getBarLength();
		int bar_height = bar.getBarHeight();
		if(bar_y + bar_height >= itemIndex_y && bar_y <= itemIndex_y + 12){
			if(bar_x + bar_length >= itemIndex_x && bar_x <= itemIndex_x + 18){
				return true;
			}
		}
		return false;
	}
	public void ballMove(){
		ball.setBallIndex();
	}
	public Block_Field getBlockField(int x,int y){
		return bf[x][y];
	}
	public Ball getBall(){
		return ball;
	}
	public int getScore(){
		return score;
	}
	public Bar getBar(){
		return bar;
	}
	public void setBarIndex(int x,int y){
		if(barOutCheck(x,y)){
			if(x < 5){
				x = 5;
			}else{
				x = 305 - bar.getBarLength();
			}
		}
		bar.setBarIndex(x,bar.getBarIndex_y());
	}
	public boolean barOutCheck(int x,int y){
		if(x >= 5  && x + bar.getBarLength() <= 305){
			return false;
		}
		return true;
	}
	public void barHitSyori(){
		int ball_x = ball.getBallIndex_x();
		int ball_y = ball.getBallIndex_y();
		int ball_move_x = ball.getBallMove_x();
		int ball_length = ball.getBallLength();
		int bar_x = bar.getBarIndex_x();
		int bar_y = bar.getBarIndex_y();
		int bar_length = bar.getBarLength();
		int bar_height = bar.getBarHeight();
		int hazi_length = bar.getBarLength() / 6;
		if(ball_y + ball_length >= bar_y && ball_y + ball_length <= bar_y + bar_height){
			if(ball_x + ball_length >= bar_x && ball_x <= bar_x + bar_length){
				if(ball_move_x == 0){
					if(ball_x + ball_length >= bar_x && ball_x <= bar_x + hazi_length){
						ball.setBallMove_x(-2);
					}
					if(ball_x + ball_length >= bar_x + bar_length - hazi_length && ball_x <= bar_x + bar_length){
						ball.setBallMove_x(2);
					}
				}else{
					if(ball_x + ball_length >= bar_x && ball_x <= bar_x + hazi_length){
						ball.setBallMove_x(0);
					}else{
						if(ball_x + ball_length >= bar_x + bar_length - hazi_length && ball_x <= bar_x + bar_length){
							ball.setBallMove_x(0);
						}
					}
				}
				ball.setBallMove_y(-2);
				bb.musicPlay(1);
			}
		}
	}
	public void wallHitSyori(){
		int ball_x = ball.getBallIndex_x();
		int ball_y = ball.getBallIndex_y();
		int ball_length = ball.getBallLength();
		if(ball_x <= 5 || ball_x + ball_length >= 305){
			ball.setBallMove_x(ball.getBallMove_x() * -1);
		}
		if(ball_y <= 5){
			ball.setBallMove_y(ball.getBallMove_y() * -1);
		}
	}
	public void blockHitSyori(){
		int ball_x = ball.getBallIndex_x();
		int ball_y = ball.getBallIndex_y();
		int ball_length = ball.getBallLength();
		for(int i = 0;i < stage.length;i++){
			for(int j = 0;j < stage[i].length;j++){
				if(bf[i][j] != null){
					if(bf[i][j].getBlockLife() > 0){
						int block_x = bf[i][j].getBlockIndex_x();
						int block_y = bf[i][j].getBlockIndex_y();
						if(ball_x + ball_length >= block_x && ball_x <= block_x + 20){
							if(ball_y + ball_length >= block_y && ball_y <= block_y + 15){
								blockhit = true;
								bf[i][j].setBlockLife(bf[i][j].getBlockLife() - 1);
								if(bf[i][j].getBlockLife() == 0){
									if(bf[i][j].getItem() != null){
										bb.itemFalledStart(bf[i][j].getItem());
									}
								}
								score = score + 1;
							}
						}
					}
				}
			}
		}
		if(blockhit){
			ball.setBallMove_y(ball.getBallMove_y() * -1);
			bb.musicPlay(0);
			blockhit = false;
		}
	}
	public boolean gameClearCheck(){
		for(int i = 0;i < stage.length;i++){
			for(int j = 0;j < stage[i].length;j++){
				if(bf[i][j] != null){
					if(bf[i][j].getBlockLife() > 0){
						return false;
					}
				}
			}
		}
		return true;
	}
	public int getLife(){
		return life;
	}
	public void setLife(int life){
		this.life = life;
	}
}
