/*
 * BreakBlocks.java
 *
 * Created on 2006/12/17, 17:32
 */

package block;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

/**
 *
 * @author  kim-my
 */
public class BreakBlocks extends javax.swing.JApplet implements ActionListener, KeyListener {
    
    int balldx = ball;
    /** ボールの縦移動距離*/
    int balldy = ball;
    /** パドルの位置 */
    int padx = 200;
    /** パドルの移動距離 */
    int dx = 0;
    /** ブロック */
    boolean[][] block = new boolean[8][10];
    /** ゲームオーバーかどうか */
    boolean over = true;
    
    Dimension d;
    BufferedImage bi = null;
    
    /** Initializes the applet BreakBlocks */
    public void init(){
	try{
	    java.awt.EventQueue.invokeAndWait(new Runnable(){
		public void run(){
		    initComponents();
		}
	    });
	}catch(Exception ex){
	    ex.printStackTrace();
	}
	
	d = new Dimension(400, 300);
	this.setSize(d);
	new javax.swing.Timer(50, this).start();
	addKeyListener(this);
    }
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
    public void actionPerformed(ActionEvent actionEvent){
	//パドルの移動
	padx += dx;
	if(padx < 0){
	    padx = 0;
	}
	if(padx > 380){
	    padx = 380;
	}
	
	//ボールのx座標の移動
	ballx += balldx;
	if(ballx < 0){
	    ballx = 0;
	    balldx = -balldx;
	}
	if(ballx > 380){
	    ballx = 380;
	    balldx = -balldx;
	}
	//ブロックのあたり判定
	for(int i=0; i<block.length; ++i){
	    for(int j=0; j<block[i].length; ++j){
		if(!block[i][j]){
		    continue;
		}
		if(j*37+5 < ballx+4 && ballx < j*37+5+34 && i*12+10 < bally+2 && bally+2 < i*12+10+9){
		    balldx = -balldx;
		    ballx += balldx;
		    if(!over){
			block[i][j] = false;
		    }
		}
	    }
	}
	
	//ボールのy座標の移動
	bally += balldy;
	if(bally < 0){
	    bally = 0;
	    balldy = -balldy;
	}
	if(bally > 280){
	    bally = 280;
	    balldy = -balldy;
	    //ボールが落ちたときなのでゲームオーバー
	    over = true;
	}
	//ブロックのあたり判定
	for(int i=0; i<block.length; ++i){
	    for(int j=0; j<block[i].length; ++j){
		if(!block[i][j]){
		    continue;
		}
		if(j*37+5 < ballx+2 && ballx+2< j*37+5+34 && i*12+10 < bally+4 && bally < i*12+10+9){
		    balldy = -balldy;
		    bally += balldy;
		    if(!over){
			block[i][j] = false;
		    }
		}
	    }
	}
	
	//ゲームオーバーのときは、ボールと同じ位置にパドルがある
	if(over){
	    padx = ballx - 15;
	}
	
	//パドルのあたり判定
	if(padx < ballx + 2 && ballx + 2 < padx + 40 && 250 < bally + 4 && bally < 260 && balldy > 0){
	    balldy = -balldy;
	}
	
	repaint();
    }
    
    public void keyTyped(KeyEvent keyEvent){
    }
    
    /** キーが押されたとき */
    public void keyPressed(KeyEvent keyEvent){
	if(over){
	    switch(keyEvent.getKeyCode()){
		case KeyEvent.VK_SPACE://スペース
		    //ゲーム開始
		    /*
		    ballx = 200;
		    bally = 150;
		    balldx = 5;
		    balldy = -5;
		    padx = 200;
		    dx = 0;
		    */
		    for(int i=0; i<block.length; ++i){
			for(int j=0; j<block[i].length; ++j){
			    block[i][j] = true;
			}
		    }
		    over = false;
		    break;
		case KeyEvent.VK_LEFT:
		    if(ball == 1){
			break;
		    }
		    ball--;
		    balldx = balldx/Math.abs(balldx)*ball;
		    break;
		case KeyEvent.VK_RIGHT:
		    if(ball == 9){
			break;
		    }
		    ball++;
		    balldx = balldx/Math.abs(balldx)*ball;
		    break;
	    }
	    return;
	}
	
	switch(keyEvent.getKeyCode()){
	    case KeyEvent.VK_LEFT://左
		dx = -6;
		break;
	    case KeyEvent.VK_RIGHT://右
		dx = 6;
		break;
	}
    }
    
    /** キーが離されたとき */
    public void keyReleased(KeyEvent keyEvent){
	switch(keyEvent.getKeyCode()){
	    case KeyEvent.VK_LEFT://左
	    case KeyEvent.VK_RIGHT://右
		dx = 0;
		break;
	}
    }
    
    public void paint(Graphics g){
	if(bi == null){
	    bi = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
	}
	Graphics2D g2 = (Graphics2D)bi.getGraphics();
	
	//背景描画
	GradientPaint gp = new GradientPaint(0, 170, Color.blue, 0, 360, Color.black, true);
	g2.setPaint(gp);
	g2.fillRect(0, 0, 400, 300);
	
	//ブロック描画
	g2.setColor(Color.red);
	for(int i=0; i<8; ++i){
	    int c = 512 * i / 8;
	    int cr, cg, cb;
	    if(c < 256){
		cr = 255;
		cg = c;
		cb = 0;
	    } else{
		cr = 0;
		cg = 511 - c;
		cb = 255;
	    }
	    gp = new GradientPaint(0, i*12+10, new Color(cr, cg, cb), 0, i*12+14, Color.white, true);
	    g2.setPaint(gp);
	    for(int j=0; j<10; ++j){
		if(!block[i][j]){
		    continue;
		}
		g2.fillRect(j*37+5, i*12+10, 35, 10);
	    }
	}
	
	//ボール描画
	g2.setColor(Color.yellow);
	g2.fillOval(ballx, bally, 5, 5);
	
	//パドル描画
	g2.setColor(Color.white);
	g2.fillRect(padx, 250, 40, 10);
	
	if(over){
	    g2.drawString("Game Over", 170, 120);
	    g2.drawString("Click Space Key!!", 155, 150);
	    g2.drawString("Ball Speed --> "+ball, 160, 200);
	}
	
	g.drawImage(bi, 0, 0, null);
    }
    
    public void update(Graphics g){
	paint(g);
    }
    
}
