import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Image;

public class teki1 extends JFrame {

    public teki1() {
	setSize(750, 500);
	setTitle("Samurai");
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	MyJPanel myJPanel = new MyJPanel();
	Container c = getContentPane();
	c.add(myJPanel);	
	setVisible(true);
    }

    public static void main(String[] args) {
	new teki1();
    }

    public class MyJPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
	double line = -3.14/2;
	//自機
	int jiki_x = 100;
	int jiki_y;
	Image jiki_ima1,jiki_ima2;
	int jiki_w, jiki_h;
	int jiki_id=0;//瞬間移動打消、0＝マウスに反応 1＝マウスが出た 2＝up 3＝down
	int cut;
	
	int score;
	int[] teki1_x,teki1_y;
	int[] teki2_x,teki2_y;
	Image teki1_ima,teki2_ima;
	int teki1_w,teki1_h;
	int teki2_w,teki2_h;
	int[] teki1_alive,teki2_alive;

	int[] teki3_x,teki3_y;
	int bomb_x,bomb_y;
	Image teki3_ima,bomb_ima;
	int teki3_w,teki3_h;
	int bomb_w,bomb_h;
	int bomb_alive;
	int[] teki3_alive;

	Timer timer;
	public MyJPanel() {
	    ImageIcon icon1 = new ImageIcon("normal.png");
	    ImageIcon icon2 = new ImageIcon("cut.png");
	    jiki_ima1 = icon1.getImage();
	    jiki_ima2 = icon2.getImage();
	    jiki_w = jiki_ima1.getWidth(this);
	    jiki_h = jiki_ima1.getHeight(this);
	    cut = 0;
	    jiki_id=1;
	    

	    // 画像の設定
	    ImageIcon icon_teki1=new ImageIcon("teki1.png");
	    ImageIcon icon_teki2=new ImageIcon("teki2.png");
	    ImageIcon icon_teki3 = new ImageIcon("teki3.png");
	    ImageIcon icon_bomb = new ImageIcon("bomb.png");
	    
	    teki1_ima = icon_teki1.getImage();
	    teki2_ima = icon_teki2.getImage();
	    teki3_ima = icon_teki3.getImage();
	    bomb_ima = icon_bomb.getImage();
	    teki1_w = teki1_ima.getWidth(this);
	    teki2_w = teki2_ima.getWidth(this);
	    teki3_w = teki3_ima.getWidth(this);
	    bomb_w = bomb_ima.getWidth(this);
	    teki1_h = teki1_ima.getHeight(this);
	    teki2_h = teki2_ima.getHeight(this);
	    teki3_h = teki3_ima.getHeight(this);
	    bomb_w = bomb_ima.getHeight(this);
	    
	    // 座標と生存
	    teki1_x=new int[2];
	    teki2_x=new int[2];
	    teki3_x=new int[2];
	    teki1_y=new int[2];
	    teki2_y=new int[2];
	    teki3_y=new int[2];
	    teki1_alive=new int[2];
	    teki2_alive=new int[2];
	    teki3_alive=new int[2];
	    

	    for(int i=0;i<2;i++){
		teki1_x[i]=1000;
		teki2_x[i]=1000;
		teki3_x[i]=1000;
		teki1_y[i]=1000;
		teki2_y[i]=1000;
		teki3_y[i]=1000;
		teki1_alive[i]=0;
		teki2_alive[i]=0;
		teki3_alive[i]=0;// 1で敵生存 
	    }
	    bomb_x=1000;
	    bomb_y=1000;
	    bomb_alive=0;
	    

	    score=0;
	    String score_str = Integer.toString (score);
	    setResizable(false);
	    addMouseListener(this);
	    addMouseMotionListener(this);

	    timer = new Timer(100, this);
	    timer.start();
	}

	/* MouseListener に関連するメソッド */
	public void mouseClicked(MouseEvent e) { 
	    int x=e.getX();
	    int y=e.getY();

	    Dimension d = getSize();
	    //int heightR = teki_ima.getHeight(this);
	    
	    //cut
	    if(cut == 0){
	    cut = 3;
	    //当たり判定
	    for(int i = 0; i<2; i++){
		if(jiki_x+35<=teki1_x[i] && jiki_x+70>=teki1_x[i] && jiki_y<=teki1_y[i] && jiki_y+50>=teki1_y[i]){
		    teki1_alive[i]=0;
		    teki1_x[i] = 1000;
		    teki1_y[i] = 1000;
		    score += 100;
		}
		if(jiki_x+35<=teki2_x[i] && jiki_x+70>=teki2_x[i] && jiki_y<=teki2_y[i] && jiki_y+50>=teki2_y[i]){
		    teki2_alive[i]=0;
		    teki2_x[i] = 1000;
		    teki2_y[i] = 1000;
		    score += 300;
		}
		if(jiki_x+35<=teki3_x[i] && jiki_x+70>=teki3_x[i] && jiki_y<=teki3_y[i] && jiki_y+50>=teki3_y[i]){
		    teki3_alive[i]=0;
		    teki3_x[i] = 1000;
		    teki3_y[i] = 1000;
		    score += 500;
		}
	    }
	    }
	    repaint();
	    
	}
	public void mousePressed(MouseEvent e) { }
	public void mouseReleased(MouseEvent e) { }
	public void mouseExited(MouseEvent e) {
	    jiki_id=1;
	}
	public void mouseEntered(MouseEvent e) { 
	    if(jiki_y==e.getY()){
		jiki_id=0;
	    }else if(jiki_y<e.getY()){
		jiki_id=2;
	    }else if(jiki_y>e.getY()){
		jiki_id=3;
	    }
	}

	/* MouseMotionListener に関連するメソッド */
	public void mouseMoved(MouseEvent e) { 
	    Dimension d;
	    d = getSize();

	    int heightL = 50;
	 

	     if(jiki_id==0){//手動操縦
		jiki_y = e.getY();
		if(jiki_y>d.height-heightL-70){
		    jiki_y = d.height-heightL-70;
	      }
		repaint();
	     }else{
		if(jiki_y==e.getY()){
		    jiki_id=0;
		}else if(jiki_y<e.getY()){
		    jiki_id=2;
		}else if(jiki_y>e.getY()){
		    jiki_id=3;
		}
	     }
	     repaint();
	}
	public void mouseDragged(MouseEvent e) { }

	/* ActionListener に関連するメソッド */
	public void actionPerformed(ActionEvent e) { 
	    Dimension d = getSize();
	    int heightL = 50;
	    if (e.getSource()==timer) {
		line += 3.14/30;
		//自機とマウスの照準
		if(jiki_id==2){
		    jiki_y+=5;
		    if(jiki_y>d.height-heightL-70){
			jiki_y = d.height-heightL-70;
			jiki_id=3;
		    }
		}else if(jiki_id==3){
		    jiki_y-=5;
		    if(jiki_y<0){
			jiki_y = 0;
			jiki_id=2;
		    }
		}
		//cut 復元
		if(cut == 0){}else{
		    cut --;
		    if(cut < 0){
			cut = 0;
		    }
		}

		

		// 敵の登場条件
		int random=(int)(Math.random()*30+1);
		if(random%15==0){
		    if(teki1_alive[1]!=1){
			teki1_alive[1]=1;
			teki1_x[1]=d.width;
			teki1_y[1]=(int)(Math.random()*(d.height-teki1_h)+1);
		    }else if(teki1_alive[0]!=1){
			teki1_alive[0]=1;
			teki1_x[0]=d.width;
			teki1_y[0]=(int)(Math.random()*(d.height-teki1_h)+1);
		    }
		}

	

		if(random%20==0){
		    if(teki2_alive[1]!=1 && teki2_alive[0]==1){
			teki2_alive[1]=1;
			teki2_x[1]=d.width;
			teki2_y[1]=(int)(Math.random()*(d.height-teki2_h)+1);
		    }else if(teki2_alive[0]!=1){
			teki2_alive[0]=1;
			teki2_x[0]=d.width;
			teki2_y[0]=(int)(Math.random()*(d.height-teki2_h)+1);
		    }
		}

		if(random%25==0){
		    if(teki3_alive[1]!=1 && teki3_alive[0]==1){
			teki3_alive[1]=1;
			teki3_x[1]=d.width;
			teki3_y[1]=(int)(Math.random()*(d.height-teki3_h)+1);
		    }else if(teki3_alive[0]!=1){
			teki3_alive[0]=1;
			teki3_x[0]=d.width;
			teki3_y[0]=(int)(Math.random()*(d.height-teki3_h)+1);
		    }
		}

		if(random%30==0){
		    if(bomb_alive!=1){
			bomb_alive=1;
			bomb_x=d.width;
			bomb_y=(int)(Math.random()*(d.height-bomb_h)+1);
		    }
		}
		

		for(int i=0;i<2;i++){
		    if(teki1_x[i]<=-teki1_w){
			teki1_alive[i]=0;
			teki1_x[i]=1000;
		    }
		    if(teki2_x[i]<=-teki2_w){
			teki2_alive[i]=0;
			teki2_x[i]=1000;
		    }
		    if(teki3_x[i]<=-teki3_w){
			teki3_alive[i]=0;
			teki3_x[i]=1000;
		    }
		    
		}
		if(bomb_x<=-bomb_w){
		    bomb_alive=0;
		    bomb_x=1000;
		}
		
		// 敵の移動
		for(int i=0;i<2;i++){
		    if(teki1_alive[i]==1){teki1_x[i]-=5;}
		    if(teki2_alive[i]==1){teki2_x[i]-=10;}
		    if(teki3_alive[i]==1){teki3_x[i]-=8;}
		}
		if(bomb_alive==1){bomb_alive-=10;}
	
		repaint();
	    }
	}

	/*　画面描画に関連するメソッド */
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);

	    int heightL = jiki_ima1.getHeight(this);
	    int widthL = jiki_ima1.getWidth(this);
	    int maru_y = jiki_y + heightL +35;
	    int maru_x = jiki_x + 35;
	    
	    //自機を描く
	    if(cut == 0){
		g.drawImage(jiki_ima1, jiki_x,  jiki_y, this);
	    }else{
		g.drawImage(jiki_ima2, jiki_x+35,  jiki_y, this);
	    }
	    //ball
	    g.fillOval(jiki_x,  jiki_y + heightL,70,70);
	    g.setColor(Color.red);
	    g.drawLine(maru_x, maru_y, (int)(maru_x + 35*(Math.cos(line))), (int)(maru_y + 35*(Math.sin(line))));
	    
	    for(int i=0;i<2;i++){
		g.drawImage(teki1_ima, teki1_x[i], teki1_y[i], this);
		g.drawImage(teki2_ima, teki2_x[i], teki2_y[i], this);
		g.drawImage(teki3_ima, teki3_x[i], teki3_y[i], this);
	    }
	    g.drawImage(bomb_ima, bomb_x, bomb_y, this);
	    String score_str = Integer.toString (score);
	    g.drawString(score_str, 700, 20);
	}
    }
}

