package Game;

import java.io.IOException;

import javax.microedition.lcdui.Image;

public class NetGameRole extends Role {

	public static NetGameRole createNetGameRole(Game game, int type, int row,
			int col) {
		NetGameRole netGameRole = null;
		switch (type) {
		case Global.JACK:
			try {
				netGameRole = new NetGameRole(Image
						.createImage("/Character/Jack.png"), Global.MAP_CELL,
						Global.JACK_HEIGHT, game);
				netGameRole.height = Global.JACK_HEIGHT;
				netGameRole.power = Global.JACK_POWER;
				netGameRole.bombNums = Global.JACK_BOMBNUM;
				netGameRole.speed = Global.JACK_SPEED;
				netGameRole.setFrame(6);
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case Global.JOHN:
			try {
				netGameRole = new NetGameRole(Image
						.createImage("/Character/John.png"), Global.MAP_CELL,
						Global.JOHN_HEIGHT, game);
				netGameRole.height = Global.JOHN_HEIGHT;
				netGameRole.power = Global.JOHN_POWER;
				netGameRole.bombNums = Global.JOHN_BOMBNUM;
				netGameRole.speed = Global.JOHN_SPEED;
				netGameRole.setFrame(6);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		netGameRole.type = type;
		netGameRole.row = row;
		netGameRole.col = col;
		netGameRole.status = Global.BORN;
		netGameRole.width = Global.MAP_CELL;
		netGameRole.xCoodinate = Global.MAP_CELL * col;
		netGameRole.yCoodinate = Global.MAP_CELL * (row + 1)
				- netGameRole.height;
		netGameRole.setPosition(netGameRole.xCoodinate, netGameRole.yCoodinate);
		return netGameRole;
	}

	protected NetGameRole(Image image, int width, int Height, Game game) {
		super(image, width, Height, game);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 网络人物无碰撞检测
	 */
	public void collisionCheck(int direction) {
		// TODO Auto-generated method stub

	}

	/**
	 * 获取异地机获取道具信号后进行处理
	 */
	public void pickupTool(int tool) {
		// TODO Auto-generated method stub
		switch ((tool - 1) / 3) {
		case Global.SHOES_INC:
			if (this.speed < Global.MAX_SPEED) {
				this.speed++;
			}
			break;
		case Global.SUPER_SHOES:
			this.speed = Global.MAX_SPEED;
			break;
		case Global.POWER_INC:
			if (this.power < Global.MAX_POWER)
				this.power++;
			break;
		case Global.BOMB_INC:
			increaseBomb();
			break;
		case Global.SUPER_POWER:
			this.power = Global.MAX_POWER;
			break;
		case Global.SUPER_BOMB:
			this.bombNums = Global.MAX_BOMB_NUMBER;
			break;
		}
		this.game.map.getToolLayer().setCell(this.getCol(), this.getRow(), 0);
	}

	/**
	 * 获取异地机安放炸弹信号后进行处理
	 */
	public void setBomb(int row, int col) {
		// TODO Auto-generated method stub
		if (!this.isAlive || !this.isCanOperate || this.bombNums == 0
				|| this.game.map.hasFeature(row, col, Global.BOMB))
			return;
		String key = row * 10 + "" + col;
		this.game.bombTable.put(key, new NetGameBomb(this.game, this));
		this.game.map.removeFeature(row, col, Global.CAN_PASS);
		this.game.map.addFeature(row, col, Global.BOMB);
		this.bombNums--;
	}

	public void updateRole() {
		if (this.characterClock < 10000) {
			this.characterClock++;
		} else {
			this.characterClock = 100;
		}

		if (this.isAlive) {
			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);
						}
					}
				}
			}
		}
	}

}
