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

public class DemolationTank extends ComputerTank{
//Overview: DemolationTank is a ComputerTank that runs into
//	UserTank and explodes itself.  When it explodes, it will
//	destroy all Block objects nearby.
//	The range of the explosion is 5 by 5 squares centered at this.
	protected boolean explosion = false; 
	
	public DemolationTank(){
		super(100,1,Direction.SOUTH);
		setPoint(30);
		setXp(10);
	}

	public void drawGun(Graphics g, int x, int y, int w, int h){
	//Effects: draw the gun according to the orientation
		g.setColor(Color.black);
		int midx = x + w/2;
		int midy = y + h/2;
		if(orient.equals(Direction.NORTH)){
			g.fillRect(midx-w/4-1,y,w/2,h/2);
		} else if(orient.equals(Direction.SOUTH)){
			g.fillRect(midx-w/4-1,midy-1,w/2,h/2);
		} else if(orient.equals(Direction.WEST)){
			g.fillRect(x,midy-h/4,w/2,h/2);
		} else if(orient.equals(Direction.EAST)){
			g.fillRect(midx-1,midy-h/4,w/2,h/2);
		}
	}
	synchronized public void moveDir(Direction dir){
	//Modifies: this
	//Effects: move this one block in dir Direction
	//	but if there is a UserTank on that block, explodes itself
		if(hp<=0) return;
		synchronized(getGrid()){
			if(dir.equals(orient)){
				int newx = getX()+dir.easterlyDirection();
				int newy = getY()-dir.northerlyDirection();
				if (getGrid().validLocation(newx, newy)) {
						if (getGrid().isSquareEmpty(newx, newy)) { //@nowarn Pre
							setLocation(newx,newy);
						}else {
							SimObject so = getGrid().getObjectAt(newx,newy);
							if(so instanceof UserTank){
								explode();
							}
						}
				}
			}else{
				orient = dir;
			}
		}
	}
	synchronized public void explode(){
	//Modifies: this
	//Effects: decreaseHP of SimObject nearby.  If a SimObject nearby is a Blcok,
	//	destroy it.
		explosion=true;
		int newx=0, newy =0;
		SimObject so;
		for(int i=-2;i<=2;i++){
			for(int j=-2;j<=2;j++){
				newx=getX()+i;
				newy=getY()+j;
				if(getGrid().validLocation(newx,newy)){
					if(!getGrid().isSquareEmpty(newx,newy)){
						so = getGrid().getObjectAt(newx,newy);
						if(i!=0||j!=0){
							if(so instanceof Block){
								//destroy the Block
								so.die();
							}else{
								so.decreaseHP(5-(Math.abs(i)+Math.abs(j)));
								//don't kill yourself yet
							}
						}
					}
				}
			}
		}
	}
	synchronized public void die(){
		if(explosion==false){
			explode();
		}else{
			super.die();
		}
	}
	public void drawImage(Graphics g, int x, int y, int w, int h){
		if(explosion==false){
			drawGun(g,x,y,w,h);
			g.setColor(Color.magenta);
			g.fillRect(x+w/5,y+h/5,3*w/5,3*h/5);
			g.setColor(Color.white);
			g.drawLine(x+w/5+2,y+h/5+2,x+4*w/5-4,y+4*h/5-3);
			g.drawLine(x+4*w/5-4,y+h/5+2,x+w/5+2,y+4*h/5-3);
		}else{
			drawExplosion(g,x,y,w,h);
			die();
		}
	}

	public void drawExplosion(Graphics g, int x, int y, int w, int h){
		int newx=0, newy =0;
		for(int i=-2;i<=2;i++){
			for(int j=-2;j<=2;j++){
				newx=getX()+i;
				newy=getY()+j;
				if(getGrid().validLocation(newx,newy)){
					g.setColor(Color.red);
					g.drawOval(x+i*w-1,y+j*h-1,w-3,h-3);
				}
			}
		}
	}
	public void executeTurn() throws RuntimeException
	// Note: requires isInitialized is inherited from SimObject
	// EFFECTS: If there is an UserTank in this's line of sight,
	//		move toward that direction.
	//		Otherwise, move randomly.

	{
		if(dead){
			delay(3600000);	//sleep for an hour
		}
		delay(TURN_DELAY+speed);
		if(explosion==false){
			synchronized(getGrid()){
				Direction dir =isInLineOfSight();
				if(dir!=null){
					moveDir(dir);
				}else{
					dir = Direction.randomDirection4();
					moveDir(dir);
				}
			}
		}
	}
}