import java.awt.Graphics;
import java.awt.Color;

public class MagneticPulse extends Shell{
	private int count;
	public MagneticPulse(Direction or, int d, int speed){
		super(or,d,speed);
		count = 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);
					if(so instanceof Tank){
						((Tank)so).orient = Direction.randomDirection4();
						((Tank)so).shoot();
					}
					die();
				}
			}else{
				die();
			}
		}
	}
	public void drawImage(Graphics g, int x, int y, int w, int h){
		if(hp>0){
			g.setColor(Color.white);
			g.fillOval(x+w/4,y+h/4,w/2,h/2);
			switch(count){
				case 0:
					g.setColor(Color.blue);
					g.drawOval(x+w/4,y,w/2,h);
					count=1;
					break;
				case 1:
					g.setColor(Color.blue);
					g.drawOval(x,y+h/4,w,h/2);
					count=0;
					break;
			}
		}else{
			drawExplosion(g,x,y,w,h);
			die();
		}
	}
}