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

public class Shell extends MobileSimObject{
	protected Direction orient;
	protected int speed;
	protected int damage;
	protected Color color;
	
	public Shell(){
		orient = Direction.NORTH;
		speed = 100;
		damage = 1;
		color = Color.yellow;
	}
	public Shell(Direction or, int d){
		this.orient = or;
		this.damage = d;
		this.speed = 100;
		this.color = Color.yellow;
	}
	public Shell(Direction or,int d,int speed){
		this.orient = or;
		this.speed = speed;
		this.damage = d;
		this.color = Color.yellow;
	}
	public void setColor(Color c){
		color = c;
	}
	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();
			}
		}
	}
	synchronized public void die(){
	//Modifies: this
	//Effects: remove this from the grid if its hp == 0.
	//	Otherwise, set hp = 0 and damage = 0 (for drawing explosion)
		if(hp!=0){
			hp=0;
			damage=0;
		}else{
			setDead(true);
			grid.removeObjectAt(getX(),getY());
		}
	}
	public void drawImage(Graphics g, int x, int y, int w, int h){
		if(hp>0){
			g.setColor(color);
			int midx = x + w/2;
			int midy = y + h/2;
			g.fillRect(midx-2,midy-2,4,4);
		}else{
			drawExplosion(g,x,y,w,h);
			die();
		}
	}
	public void drawExplosion(Graphics g, int x, int y, int w, int h){
		g.setColor(Color.red);
		g.drawOval(x+1,y+1,w-3,h-3);
	}
	public void executeTurn() throws RuntimeException{
		super.executeTurn();
		delay(speed);
		shoot();
	}
}