import java.awt.Color;
public class Blitzer extends Shell {
	private int progress;

	public Blitzer() {
		orient = Direction.NORTH;
		speed = 100;
		damage = 2;
		progress = 0;
	}

	public Blitzer(Direction orient, int d, int speed) {
		this.orient = orient;
		this.speed = speed;
		damage = d;
		progress = 0;
	}

	public void shoot() {
		synchronized (getGrid()) {
			int newx = getX() + orient.easterlyDirection();
			int newy = getY() - orient.northerlyDirection();
			if (getGrid().validLocation(newx, newy)) {
				if (getGrid().isSquareEmpty(newx, newy)) { //@nowarn Pre
					setLocation(newx, newy);
				} else {
					SimObject so = grid.getObjectAt(newx, newy);
					if (so instanceof Shell
						&& ((Shell) so).orient == this.orient) {
						return; //Don't hit the previous bullet, man.
					}
					so.decreaseHP(damage);
					die();
				}
			} else {
				die();
			}
			progress++;
			if (!dead && progress == 5) {
				int north = orient.northerlyDirection();
				int coord1;
				int coord2;
				Direction side1;
				Direction side2;
				if (north != 0) {
					side1 = new Direction(0, 1);
					side2 = new Direction(0, -1);
					coord1 = 1;
					coord2 = 0;
				} else {
					side1 = new Direction(-1, 0);
					side2 = new Direction(1, 0);
					coord1 = 0;
					coord2 = 1;
				}
				Shell bullet2 = new Shell(side1, 1, 150);
				Shell bullet3 = new Shell(side2, 1, 150);
				try {
					if (getGrid()
						.validLocation(newx + coord1, newy - coord2)) {
						if (getGrid()
							.isSquareEmpty(newx + coord1, newy - coord2)) {
							getGrid().setObjectAt(
								newx + coord1,
								newy - coord2,
								bullet2);
							bullet2.init(
								newx + coord1,
								newy - coord2,
								getGrid());
							bullet2.resumeObject();
						}
					}
					if (getGrid()
						.validLocation(newx + coord1, newy +coord2)) {
						if (getGrid()
							.isSquareEmpty(newx - coord1, newy + coord2)) {
							getGrid().setObjectAt(
								newx - coord1,
								newy + coord2,
								bullet3);
							bullet3.init(
								newx - coord1,
								newy + coord2,
								getGrid());
							bullet3.resumeObject();
						}
					}
				} catch (BadLocationException e) {
					System.err.println("BadLocation for shell");
				}
			}
		}
	}

	public Color getColor() {
		return Color.orange;
	}
}