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

public class Bounce extends Item{
//Overview: Bounce is a permanent Item.
//	When used, it will repel a Shell within 5 blocks back.
	public Bounce(){
		super("Bounce");
		permanent = true;
	}
	public void action(SimObject so){
		synchronized(so){
			synchronized(getGrid()){
				Tank t = (Tank)so;
				int newx;
				int newy;
				for(int i=1;i<=5;i++){
					newx = t.getX()+i*t.orient.easterlyDirection();
					newy = t.getY()-i*t.orient.northerlyDirection();
					if(getGrid().validLocation(newx,newy)){
						if(!getGrid().isSquareEmpty(newx,newy)){
							SimObject tmp = getGrid().getObjectAt(newx,newy);
							synchronized(tmp){
								if(tmp instanceof Shell){
									((Shell)tmp).orient = t.orient;
									return;
								}
							}
						}
					}
				}
			}
		}
	}
	public void drawImage(Graphics g,int x, int y, int w, int h){
		//draw the frame
		g.setColor(Color.black);
		g.fillRect(x+1,y+1,w-4,h-4);
		g.setColor(new Color(100,100,255));
		g.fillRect(x+3,y+3,w-8,h-8);
		//draw the actual image
		g.setColor(Color.lightGray);
		g.fillOval(x+w/8,y+h/8,w/4,6*h/8);
		g.setColor(Color.red);
		g.fillRect(x+3*w/8,y+3*h/8-1,w/2,2);
		g.drawLine(x+3*w/8,y+3*h/8-1,x+3*w/8+5,y+3*h/8-4);
		g.fillRect(x+3*w/8,y+5*h/8-1,w/2,2);
		g.drawLine(x+7*w/8,y+5*h/8-1,x+7*w/8-5,y+5*h/8+3);
	}
}