package Game;

public class SingleGameBomb extends Bomb {

	public SingleGameBomb(Game game, Role role) {
		super(game, role);
	}

	public void update() {
		this.swellClock++;
		if (swellClock >= 1 && swellClock <= 60) {
			this.AddDangerArea();
		}
		if (swellClock >= 56) {
			this.AddExtDangerArea();
			this.addLinkedDanger2();
		}
		if (this.swellClock > Global.BOMB_EXPLODE_DELAY) {
			this.status = Global.BOMB_EXPLODE;
		}
		if (this.status == Global.BOMB_SWELL) {
			if (this.swellClock % 10 == 0) {
				bombLayer.setCell(col, row, bombLayer.getCell(col, row) % 3
						+ Global.IMAGE_BOMB_SWELL);
				return;
			}
		}

		if (explodeClock >= 5) {
			this.clearExplode();
			this.removeDangerArea();
			this.removeExtDangerArea();
			return;
		}

		if (this.status == Global.BOMB_EXPLODE) {
			explode();
		}
	}

	/**
	 * Add Dangerous Area
	 */

	public void AddDangerArea() {

		int mapCols = Global.COL;
		int mapRows = Global.ROW;
		this.game.map.addFeature(row, col, Global.DangerArea);

		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				if (this.game.map.hasFeature(row, col + i, Global.CAN_PASS)) {
					this.game.map.addFeature(row, col + i, Global.DangerArea);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				if (this.game.map.hasFeature(row, col - i, Global.CAN_PASS)) {
					this.game.map.addFeature(row, col - i, Global.DangerArea);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				if (this.game.map.hasFeature(row - i, col, Global.CAN_PASS)) {
					this.game.map.addFeature(row - i, col, Global.DangerArea);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				if (this.game.map.hasFeature(row + i, col, Global.CAN_PASS)) {
					this.game.map.addFeature(row + i, col, Global.DangerArea);
				} else {
					break;
				}
			}
		}
	}

	/**
	 * Add Dangerous Area
	 */
	public void AddExtDangerArea() {

		int mapCols = Global.COL;
		int mapRows = Global.ROW;
		this.game.map.addFeature(row, col, Global.EXCDANGERAREA);

		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				if (this.game.map.hasFeature(row, col + i, Global.CAN_PASS)) {
					this.game.map
							.addFeature(row, col + i, Global.EXCDANGERAREA);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				if (this.game.map.hasFeature(row, col - i, Global.CAN_PASS)) {
					this.game.map
							.addFeature(row, col - i, Global.EXCDANGERAREA);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				if (this.game.map.hasFeature(row - i, col, Global.CAN_PASS)) {
					this.game.map
							.addFeature(row - i, col, Global.EXCDANGERAREA);
				} else {
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				if (this.game.map.hasFeature(row + i, col, Global.CAN_PASS)) {
					this.game.map
							.addFeature(row + i, col, Global.EXCDANGERAREA);
				} else {
					break;
				}
			}
		}
	}

	/**
	 * Remove Dangerous Area
	 */

	public void removeDangerArea() {

		int mapCols = Global.COL;
		int mapRows = Global.ROW;
		this.game.map.removeFeature(row, col, Global.DangerArea);

		/**
		 * 添加右方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				this.game.map.removeFeature(row, col + i, Global.DangerArea);
			}
		}

		/**
		 * 添加左方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				this.game.map.removeFeature(row, col - i, Global.DangerArea);
			}
		}
		/**
		 * 添加上方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				this.game.map.removeFeature(row - i, col, Global.DangerArea);
			}
		}
		/**
		 * 添加下方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				this.game.map.removeFeature(row + i, col, Global.DangerArea);
			}
		}
	}

	/**
	 * Add Dangerous Area
	 */

	public void removeExtDangerArea() {

		int mapCols = Global.COL;
		int mapRows = Global.ROW;
		this.game.map.removeFeature(row, col, Global.EXCDANGERAREA);

		/**
		 * 添加右方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				this.game.map.removeFeature(row, col + i, Global.EXCDANGERAREA);
			}
		}

		/**
		 * 添加左方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				this.game.map.removeFeature(row, col - i, Global.EXCDANGERAREA);
			}
		}
		/**
		 * 添加上方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				this.game.map.removeFeature(row - i, col, Global.EXCDANGERAREA);
			}
		}
		/**
		 * 添加下方危险区域
		 */
		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				this.game.map.removeFeature(row + i, col, Global.EXCDANGERAREA);
			}
		}
	}

	/**
	 * 炸弹极度危险属性关联设置
	 */
	private void addLinkedDanger2() {

		int mapCols = Global.COL;
		int mapRows = Global.ROW;

		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				if (this.game.map.hasFeature(row, col + i, Global.BOMB)) {
					this.linkedBomb2(row, col + i);
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				if (this.game.map.hasFeature(row, col - i, Global.BOMB)) {
					this.linkedBomb2(row, col - i);
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				if (this.game.map.hasFeature(row - i, col, Global.BOMB)) {
					this.linkedBomb2(row - i, col);
					break;
				}
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				if (this.game.map.hasFeature(row + i, col, Global.BOMB)) {
					this.linkedBomb2(row + i, col);
					break;
				}
			}
		}
	}

	private void linkedBomb2(int row, int col) {
		String key = row * 10 + "" + col;
		SingleGameBomb bomb = (SingleGameBomb) this.game.bombTable.get(key);
		if (bomb != null)
			bomb.AddExtDangerArea();
	}

}
