package Game;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public abstract class Role extends Sprite {

	/**
	 * 人物的基本属性
	 */
	protected int type;
	protected int xCoodinate;
	protected int yCoodinate;
	protected int row;
	protected int col;
	protected int width;
	protected int height;
	protected int speed;
	protected int status;
	protected boolean isCanOperate = false;
	protected boolean isAlive = true;

	/**
	 * 人物放置炸弹的基本属性
	 */
	protected int power;
	protected int bombNums;

	protected int characterClock = 0;
	protected int deadTime = 0;

	protected Game game;

	protected Role(Image image, int width, int Height, Game game) {
		super(image, width, Height);
		this.game = game;
	}

	/**
	 * 人物拾起道具
	 * @param tool
	 */
	public abstract void pickupTool(int tool);
	/**
	 * 碰撞检测以及坐标的改变,如果对行走条件有特殊需求，既可以在这里写自己的条件
	 * @param direction
	 */
	public abstract void collisionCheck(int direction);
	
	public void updateRole() {
		if (this.characterClock < 10000) {
			this.characterClock++;
		} else {
			this.characterClock = 100;
		}

		int row = this.getRow();
		int col = this.getCol();

		if (this.isAlive) {
			
			int tool = this.game.map.getToolLayer().getCell(col, row);
			
			if (tool > 0) {
				this.pickupTool(tool);
				this.game.map.getToolLayer().setCell(col, row, 0);
			}

			if (this.game.map.hasFeature(row, col, Global.DEADLY)) {
				this.isAlive = false;
				return;
			}

			if (this.status == Global.BORN
					&& this.characterClock > Global.BORN_TIME) {
				this.status = Global.SOUTH;
				this.setFrame(Global.SOUTH * 6);
				this.isCanOperate = true;
			}

			if (this.status == Global.BORN) {
				if (this.characterClock % 2 == 0)
					this.setFrame(Global.BORN * 6 + (this.getFrame() - 1) % 4);
				return;
			}
			
		} else {
			this.isCanOperate = false;
			if (this.deadTime <= 20) {
				this.deadTime++;
			} else {
				this.deadTime = 100;
				this.setVisible(false);
				return;
			}

			if (this.characterClock % 2 == 0) {
				if (this.getFrame() < Global.DEAD * 6) {
					this.setFrame(Global.DEAD * 6);
				} else {
					if (this.getFrame() < 29) {
						this.setFrame(this.getFrame() + 1);
					} else {
						if (this.characterClock % 4 == 0) {
							this.setFrame(29);
							this.setVisible(true);
						} else {
							this.setVisible(false);
						}
					}
				}
			}
		}
	}
	
	public void walk(int direction) {
		if (!isAlive)
			return;
		if (!isCanOperate)
			return;
		if(direction==9) return;
		this.collisionCheck(direction);
		
		if (this.characterClock % 2 == 0) {
			if (this.status == direction) {
				this.setFrame(this.status * 6 + (this.getFrame() + 1) % 6);
			} else {
				this.status = direction;
				this.setFrame(this.status * 6);
			}
		}
		this.setPosition(xCoodinate, yCoodinate);
	}

	public void stop() {
		this.isCanOperate = false;
	}

	public void play() {
		this.isCanOperate = true;
	}

	public abstract void setBomb(int row, int col);

	public void increaseBomb() {
		if (this.bombNums < Global.MAX_BOMB_NUMBER)
			this.bombNums++;
	}

	public int getRow() {
		return getRow(getBottomY(yCoodinate) - Global.MAP_CELL / 2);
	}

	public int getCol() {
		return getCol(xCoodinate + Global.MAP_CELL / 2);
	}

	protected int getBottomY(int y) {
		return y + this.height - 1;
	}

	protected int getRightX(int x) {
		return x + Global.MAP_CELL - 1;
	}

	protected int getPreY(int y) {
		return getBottomY(y) + 1 - Global.MAP_CELL;
	}

	protected int getRow(int x) {
		return x / Global.MAP_CELL;
	}

	protected int getCol(int y) {
		return y / Global.MAP_CELL;
	}
}
