package net.canking.plane;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;


public class Player {
	//Ö÷½ÇµÄÑªÁ¿ÓëÑªÁ¿Î»Í¼
	//Ä¬ÈÏ3Ñª
	private int playerHp = 3;
	private Bitmap bmpPlayerHp;
	//Ö÷½ÇµÄ×ø±êÒÔ¼°Î»Í¼
	public int x, y;
	private Bitmap bmpPlayer;
	//Ö÷½ÇÒÆ¶¯ËÙ¶È
	private int speed = 5;
	//Ö÷½ÇÒÆ¶¯±êÊ¶£¨»ù´¡ÕÂ½ÚÒÑ½²½â£¬Äã¶®µÃ£©
	private boolean isUp, isDown, isLeft, isRight;
	//Åö×²ºó´¦ÓÚÎÞµÐÊ±¼ä
	//¼ÆÊ±Æ÷
	private int noCollisionCount = 0;
	//ÒòÎªÎÞµÐÊ±¼ä
	private int noCollisionTime = 60;
	//ÊÇ·ñÅö×²µÄ±êÊ¶Î»
	private boolean isCollision;

	//Ö÷½ÇµÄ¹¹Ôìº¯Êý
	public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) {
		this.bmpPlayer = bmpPlayer;
		this.bmpPlayerHp = bmpPlayerHp;
		x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
		y = MySurfaceView.screenH - bmpPlayer.getHeight();
	}

	//Ö÷½ÇµÄ»æÍ¼º¯Êý
	public void draw(Canvas canvas, Paint paint) {
		//»æÖÆÖ÷½Ç
		//µ±´¦ÓÚÎÞµÐÊ±¼äÊ±£¬ÈÃÖ÷½ÇÉÁË¸
		if (isCollision) {
			//Ã¿2´ÎÓÎÏ·Ñ­»·£¬»æÖÆÒ»´ÎÖ÷½Ç
			if (noCollisionCount % 2 == 0) {
				canvas.drawBitmap(bmpPlayer, x, y, paint);
			}
		} else {
			canvas.drawBitmap(bmpPlayer, x, y, paint);
		}
		//»æÖÆÖ÷½ÇÑªÁ¿
		for (int i = 0; i < playerHp; i++) {
			canvas.drawBitmap(bmpPlayerHp, i * bmpPlayerHp.getWidth(), MySurfaceView.screenH - bmpPlayerHp.getHeight(), paint);
		}
	}

	//ÊµÌå°´¼ü
	public void onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			isUp = true;
			Log.e("", "keydown up");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			isDown = true;
			Log.e("", "keydown down");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			isLeft = true;
			Log.e("", "keydown left");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			isRight = true;
			Log.e("", "keydown right");
		}
	}

	//ÊµÌå°´¼üÌ§Æð
	public void onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			isUp = false;
			Log.e("", "keyup up");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			isDown = false;
			Log.e("", "keyup up");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			isLeft = false;
			Log.e("", "keyup up");
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			isRight = false;
			Log.e("", "keyup up");
		}
	}

	//Ö÷½ÇµÄÂß¼­
	public void logic() {
		//´¦ÀíÖ÷½ÇÒÆ¶¯
		if (isLeft) {
			x -= speed;
		}
		if (isRight) {
			x += speed;
		}
		if (isUp) {
			y -= speed;
		}
		if (isDown) {
			y += speed;
		}
		//ÅÐ¶ÏÆÁÄ»X±ß½ç
		if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
			x = MySurfaceView.screenW - bmpPlayer.getWidth();
		} else if (x <= 0) {
			x = 0;
		}
		//ÅÐ¶ÏÆÁÄ»Y±ß½ç
		if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
			y = MySurfaceView.screenH - bmpPlayer.getHeight();
		} else if (y <= 0) {
			y = 0;
		}
		//´¦ÀíÎÞµÐ×´Ì¬
		if (isCollision) {
			//¼ÆÊ±Æ÷¿ªÊ¼¼ÆÊ±
			noCollisionCount++;
			if (noCollisionCount >= noCollisionTime) {
				//ÎÞµÐÊ±¼ä¹ýºó£¬½Ó´¥ÎÞµÐ×´Ì¬¼°³õÊ¼»¯¼ÆÊýÆ÷
				isCollision = false;
				noCollisionCount = 0;
			}
		}
	}

	//ÉèÖÃÖ÷½ÇÑªÁ¿
	public void setPlayerHp(int hp) {
		this.playerHp = hp;
	}

	//»ñÈ¡Ö÷½ÇÑªÁ¿
	public int getPlayerHp() {
		return playerHp;
	}

	//ÅÐ¶ÏÅö×²(Ö÷½ÇÓëµÐ»ú)
	public boolean isCollsionWith(Enemy en) {
		//ÊÇ·ñ´¦ÓÚÎÞµÐÊ±¼ä
		if (isCollision == false) {
			int x2 = en.x;
			int y2 = en.y;
			int w2 = en.frameW;
			int h2 = en.frameH;
			if (x >= x2 && x >= x2 + w2) {
				return false;
			} else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
				return false;
			} else if (y >= y2 && y >= y2 + h2) {
				return false;
			} else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
				return false;
			}
			//Åö×²¼´½øÈëÎÞµÐ×´Ì¬
			isCollision = true;
			return true;
			//´¦ÓÚÎÞµÐ×´Ì¬£¬ÎÞÊÓÅö×²
		} else {
			return false;
		}
	}
	//ÅÐ¶ÏÅö×²(Ö÷½ÇÓëµÐ»ú×Óµ¯)
	public boolean isCollsionWith(Bullet bullet) {
		//ÊÇ·ñ´¦ÓÚÎÞµÐÊ±¼ä
		if (isCollision == false) {
			int x2 = bullet.bulletX;
			int y2 = bullet.bulletY;
			int w2 = bullet.bmpBullet.getWidth();
			int h2 = bullet.bmpBullet.getHeight();
			if (x >= x2 && x >= x2 + w2) {
				return false;
			} else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
				return false;
			} else if (y >= y2 && y >= y2 + h2) {
				return false;
			} else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
				return false;
			}
			//Åö×²¼´½øÈëÎÞµÐ×´Ì¬
			isCollision = true;
			return true;
			//´¦ÓÚÎÞµÐ×´Ì¬£¬ÎÞÊÓÅö×²
		} else {
			return false;
		}
	}
	
	/**
	 * ´¥ÆÁ¿ØÖÆ
	 * @param event
	 */
	public void onTouchEven(MotionEvent event){
		x=(int) event.getX()-bmpPlayer.getWidth()/2;
		y=(int) event.getY()-bmpPlayer.getHeight();
	}
}
