package Game;

import javax.microedition.lcdui.game.TiledLayer;

public abstract class Bomb {

	protected Game game;
	/**
	 * 泡泡的基本属性
	 */
	protected int power;
	protected int row;
	protected int col;
	protected int status = Global.BOMB_SWELL;
	protected boolean isvisable = true;

	/**
	 * 泡泡的额外属性
	 */
	protected int swellClock = 0;
	protected int explodeClock = 0;

	protected boolean firstExplode = true;

	protected TiledLayer bombLayer;
	protected TiledLayer toolLayer;
	protected Role role;
	
	public Bomb(Game game, Role role) {
		this.game = game;
		this.role = role;
		this.row = role.getRow();
		this.col = role.getCol();
		this.power = role.power;
		this.bombLayer = this.game.map.getBombLayer();
		this.toolLayer = this.game.map.getToolLayer();
		this.bombLayer.setCell(col, row, Global.IMAGE_BOMB_SWELL);
	}
	
	public abstract void update();
	
	
	public void explode() {
		if (this.firstExplode) {
			this.role.increaseBomb();
			this.firstExplode = false;
		}
		this.explodeClock++;
		this.bombLayer.setCell(col, row, Global.IMAGE_BOMB_EXPLODE
				+ this.bombLayer.getCell(col, row) % 4);
		this.game.map.removeFeature(row, col, Global.BOMB);
		this.game.map.addFeature(row, col, Global.DEADLY);
		this.game.map.addFeature(row, col, Global.CAN_PASS);

		int mapCols = this.game.map.mapCols;
		int mapRows = this.game.map.mapRows;

		/**
		 * 绘制右方冲击波
		 */
		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {				
				if (this.game.map.hasFeature(row, col + i, Global.BOMB)) {
					this.detonate(row, col + i);
					break;
				}
				if (this.game.map.hasFeature(row, col + i, Global.CAN_PASS)) {
					this.game.map.addFeature(row, col + i, Global.DEADLY);
					this.toolLayer.setCell(col + i, row, 0);
					if (i == power) {
						this.bombLayer.setCell(col + i, row, 25 + explodeClock);
					} else {
						this.bombLayer.setCell(col + i, row, 40);
					}
				} else {
					this.game.map.destroy(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.detonate(row, col - i);
					break;
				}
				if (this.game.map.hasFeature(row, col - i, Global.CAN_PASS)) {
					this.game.map.addFeature(row, col - i, Global.DEADLY);
					this.toolLayer.setCell(col - i, row, 0);
					if (i == power) {
						this.bombLayer.setCell(col - i, row, 17 + explodeClock);
					} else {
						this.bombLayer.setCell(col - i, row, 39);
					}
				} else {
					this.game.map.destroy(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.detonate(row - i, col);
					break;
				}
				if (this.game.map.hasFeature(row - i, col, Global.CAN_PASS)) {
					this.game.map.addFeature(row - i, col, Global.DEADLY);
					this.toolLayer.setCell(col, row - i, 0);
					if (i == power) {
						this.bombLayer.setCell(col, row - i, 1 + explodeClock);
					} else {
						this.bombLayer.setCell(col, row - i, 37);
					}
				} else {
					this.game.map.destroy(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.detonate(row + i, col);
					break;
				}
				if (this.game.map.hasFeature(row + i, col, Global.CAN_PASS)) {
					this.game.map.addFeature(row + i, col, Global.DEADLY);
					this.toolLayer.setCell(col, row + i, 0);
					if (i == power) {
						this.bombLayer.setCell(col, row + i, 9 + explodeClock);
					} else {
						this.bombLayer.setCell(col, row + i, 37);
					}
				} else {
					this.game.map.destroy(row + i, col);
					break;
				}
			}
		}
	}

	public void clearExplode() {
		this.isvisable = false;
		this.bombLayer.setCell(col, row, 0);
		this.game.map.removeFeature(row, col, Global.DEADLY);

		int mapCols = Global.MAP_WIDTH / Global.MAP_CELL;
		int mapRows = Global.MAP_HEIGHT / Global.MAP_CELL;

		for (int i = 1; i <= this.power; i++) {
			if (col + i < mapCols) {
				this.clearExplodeWave(row, col + i);
				this.game.map.removeFeature(row, col + i, Global.DEADLY);
			} else {
				break;
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (col - i >= 0) {
				this.clearExplodeWave(row, col - i);
				this.game.map.removeFeature(row, col - i, Global.DEADLY);
			} else {
				break;
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row + i < mapRows) {
				this.clearExplodeWave(row + i, col);
				this.game.map.removeFeature(row + i, col, Global.DEADLY);
			} else {
				break;
			}
		}

		for (int i = 1; i <= this.power; i++) {
			if (row - i >= 0) {
				this.clearExplodeWave(row - i, col);
				this.game.map.removeFeature(row - i, col, Global.DEADLY);
			} else {
				break;
			}
		}
	}

	private void clearExplodeWave(int row, int col) {
		if ((this.bombLayer.getCell(col, row) > 0 && this.bombLayer.getCell(
				col, row) <= 32)
				|| (this.bombLayer.getCell(col, row) > 36 && this.bombLayer
						.getCell(col, row) <= 40)) {
			this.bombLayer.setCell(col, row, 0);
		}
	}

	private void detonate(int row, int col) {
		String key = row * 10 + "" + col;
		Bomb bomb = (Bomb) this.game.bombTable.get(key);
		if (bomb != null)
			bomb.status = Global.BOMB_EXPLODE;
	}
}
