import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageProducer;
import java.net.URL;

@SuppressWarnings("serial")
public class game extends Frame implements Runnable{
	Image bg = null;						//背景画像の型宣言
	Image stp[] = new Image[10];			//止まっている時の画像の型宣言
	URL stp_url[] = new URL[10];			//止まっている時の画像のアドレス
	int maisu = 0;							//アニメーションの枚数
	URL s_url[] = new URL[10];				//正面のアニメーションのURL宣言
	Image s_run[] = new Image[10];			//正面のアニメーションの型宣言
	Image Itembuki = null;					//武器アイテムの画像の型宣言
	int bx = -400,by = -300;				//背景の座標
	Point p = getLocation();				//現在位置
	int PlayerHP = 140,PlayerMP = 140;		//PlayerのHP,MP
	int damage,idamage;						//HP,MPダメージ
	int hirl,mp_hirl;						//HP,MP回復
	int PlayerMaxHP = 140,PlayerMaxMP = 140;//Playerの最大HP,MP
	int speed = 5;							//移動スピード
	int posx,posy;							//クリック時の座標
	int movex = 0,movey = 0;				//動く距離
	int HPGage,MPGage;						//HP,MPゲージ
	int stp_set = 0;						//キャラの方向
	boolean ido = true;						//やりたかったけど無意味と化したもの
	int HPtes = 0,MPtes = 0;				//HP,MPテスト
	private void HPGageCalculation(){
		double HPCompression = (double)PlayerMaxHP / 60;
		double MPCompression = (double)PlayerMaxMP / 60;
		HPGage = (int)(PlayerMaxHP / HPCompression);
		MPGage = (int)(PlayerMaxMP / MPCompression);
		if (HPGage == 59)
			HPGage ++;
		if(MPGage ==59)
			MPGage ++;
		System.out.println(HPGage + ":" + HPCompression + "\n" + MPGage + ":" + MPCompression);
	}
	private void DamageCalcilation(){
		
		  if (damage > 0 && PlayerHP > 0){
			double aaa = (double)damage / PlayerMaxHP * 60;
			PlayerHP -= damage;
			HPGage -= aaa;
			System.out.println(PlayerHP + ":" + HPGage + ":" + aaa);
			}

	}
	private void MakeChara(){
		//画像の表示準備
		URL bg1 = this.getClass().getResource("/bg_x9.png");
		URL Itembuki1 = this.getClass().getResource("/item_001.png");
		
		for(int j=0;j<10;j++){
			if(j<9){
				stp_url[j] = this.getClass().getResource("/stp_0" + (j+1) + ".png");
				s_url[j] = this.getClass().getResource("/s_run0" + (j+1) + ".png");
			}else{
				stp_url[j] = this.getClass().getResource("/stp_" + (j+1) + ".png");
				s_url[j] = this.getClass().getResource("/s_run" + (j+1) + ".png");
			}
		}
		try {
			bg = this.createImage((ImageProducer)bg1.getContent());
			Itembuki = this.createImage((ImageProducer)Itembuki1.getContent());
			for(int j=0;j<10;j++){
				s_run[j] = this.createImage((ImageProducer)s_url[j].getContent());
				stp[j] = this.createImage((ImageProducer)stp_url[j].getContent());
			}
		}
		catch(Exception ex){}
	}
	public game(){
		super("たくあん☆ぷろぐらむ");	//タイトル
		this.setSize(800,600);		//Windowの大きさ
		this.setBackground(Color.black);
		p.x += 50;					//開始の座標ずらし
		p.y += 50;
		setLocation(p);				//座標習得
		validate();					//Reset
		MakeChara();				//MakeChara()呼び出し
		HPGageCalculation();		//HPをゲージ用に圧縮するメゾット
		new Thread(this).start();	//スレッドスタート
		addMouseListener(new MyMouseAdapter());//マウスリスナー
		this.setVisible(true);
//×ボタン用
		this.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent ev){
				System.exit(0);
			}
		});
//ここまで×ボタン用
	}
	public static void main(String[] args) {
		new game();
	}
	/*public void update(Graphics g)
	{
		paint(g);
	}*/
	public void paint(Graphics g){
		g.drawImage(bg,bx,by,this);
		
		//キャラクター描写
		if(stp_set < 9)
			g.drawImage(stp[stp_set],375,250,this);
		else if(stp_set == 10)
			g.drawImage(s_run[maisu],375,250,this);
		//else
		//	g.drawImage(stp[0],375,250,this);
		
		//装備品
		if((movex <= 0 || movey <= 0) && stp_set == 0)
			g.drawImage(Itembuki,386,314,this);
		
		//HP,MPゲージの外枠
		g.setColor(Color.black);
		g.fillRect(369, 351, 62, 7);
		g.fillRect(369, 356,62,6);

		g.setColor(Color.gray);
		g.fillRect(370, 352, 60, 5);
		g.setColor(Color.darkGray);
		g.fillRect(370, 358, 60, 3);
		
		//HPゲージのHP
		if(HPGage > 34)
			g.setColor(Color.green);
		else if(HPGage > 17)
			g.setColor(Color.yellow);
		else
			g.setColor(Color.red);

		g.fillRect(370, 352, HPGage, 5);
		g.setColor(Color.blue);
		g.fillRect(370, 358, MPGage, 3);
		
		//Status
		g.setColor(Color.black);
		g.drawString("HP：" + PlayerHP + " / " + PlayerMaxHP,20,50);
		g.drawString("MP：" + PlayerMP + " / " + PlayerMaxMP,20,65);
		
		//座標の表示
		g.setColor(Color.black);
		g.drawString("[" + Math.abs(bx) + " , " + Math.abs(by) + "]",360,375);
	
	}
	public void run() {
		while(ido == true){
			DamageCalcilation();
			Charadirection();
			MoveEstablish();
			maisu++;
			if(maisu > 9){maisu = 0;}
			repaint();
			try{
            	Thread.sleep(50);  //スリープ１秒
        	}catch(InterruptedException e){}
		}
	}
	private void MoveEstablish() {
		//　移動設定
		if (posx > 425){			//posx＞375の時必然に右半分をクリックされたことになる。
			if(movex > 0){			//movexは移動量なので０になるまで移動させる
				bx -= speed;		//speed分だけ背景のxをマイナス方向へ移動
				movex -= speed;		//speed分だけmovexを引いていく
				System.out.println("movex = " + movex);//デバッグ用
				System.out.println("movey = " + movey);
			}else{posx=375;}			//止める用（無限ループ回避
		}else if(posx < 375){
			if(movex > 0){//MAP制御
				if(bx < 0){
					bx += speed;
					movex -= speed;
				}
				System.out.println("movex = " + movex);
				System.out.println("bx = " + bx);
			}else{posx = 375;}
		}
		if(posy > 350){
			if(movey > 0){
				by -= speed;
				movey -= speed;
				System.out.println("movey = " + movey);
			}else{posy = 280;}
		}
		else if(posy < 400){
			if(movey > 0){
				if(by < 0){//MAP制御
					by += speed;
					movey -= speed;
				}
			}else{posy = 280;}
		}
		
	}
	private void Charadirection() {
		
		if(posx > 420 && posy < 230)							//右後ろ。画像はまだない
			stp_set = 7;
		else if(posx > 420 && (posy < 350 && posy > 250))		//真右。反転させただけだから目の色がアレなのは勘弁
			stp_set = 6;
		
		else if(posx > 420 && posy > 350)						//右前
			stp_set = 2;
		
		else if((posx > 350 && posx < 425) && posy < 250)		//真後ろ。制作途中なのでやっつけ
			stp_set = 3;
		else if((posx > 350 && posx < 425) && posy > 350)		//真ん前
			if(movey > 0)
				stp_set = 10;				//走り。Wバッファリングがまだなのでちらちらする。
			else
				stp_set = 0;				//静止
		else if(posx < 360 && posy < 230)						//左後ろ。画像はまだない
			stp_set = 8;
		else if((posx < 360) && (posy < 350 && posy > 250))		//真左
			stp_set = 4;
		else if((posx < 360) && posy > 350)						//左前
			stp_set = 1;
	}
	class MyMouseAdapter extends MouseAdapter {

	    public void mouseClicked(MouseEvent e) {
			posx = e.getX();//クリック時の座標習得
			posy = e.getY();
			
			if(posx > 375){//移動量の算出
				movex = posx - 375;
				System.out.println("posx = " + posx);
			}else{
				movex = 375 - posx;
				System.out.println("posx = " + posx);
			}
			if(posy > 350){
				movey = posy - 350;
				System.out.println("posy = " + posy);
			}else{
				movey = 350 - posy;
				System.out.println("posy = " + posy);
			}
	    }
	    /*public void mousePressed(MouseEvent e) {
		System.out.println("Pressed at ("+e.getX()+","+e.getY()+")");
	    }*/
	    /*public void mouseReleased(MouseEvent e) {
		System.out.println("Released at ("+e.getX()+","+e.getY()+")");
	    }*/
	}
}