package bullithell.objects.missiles;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

import bullithell.images.Images;

public abstract class Missile {
	private Rectangle border;
	private double xspeed;
	double yspeed;
	private boolean explodable;
	private int explodeState;
	private BufferedImage explode;
	public Missiletype type;
	public enum Missiletype{BALL, BOMB, LASER};
	
	public double getXspeed() {
		return xspeed;
	}
	public void setXspeed(double d) {
		this.xspeed = d;
	}
	public double getYspeed() {
		return yspeed;
	}
	public void setYspeed(double d) {
		this.yspeed = d;
	}
	public Rectangle getBorder() {
		return border;
	}
	public void setBorder(Rectangle border) {
		this.border = border;
	}	
	public boolean isExplodable() {
		return explodable;
	}
	public void setExplodable(boolean explodable) {
		this.explodable = explodable;
	}
	public int getExplodeState() {
		return explodeState;
	}
	public void draw(Graphics2D g2){
		if(explodeState > 1){
			g2.drawImage(explode, (int)((border.getX()+border.getWidth()/2)-64), (int)((border.getY()+border.getHeight()/2)-64), (int)128, (int)128, null);
			g2.translate((Math.random()*10)-5, Math.random()*10-5);
		}
	};
	public void recalculate(){
		getBorder().setLocation((int)(getBorder().getX()+getXspeed()), (int)(getBorder().getY() + getYspeed()));
	};
	public void explode(boolean shipintersect){
		if(explodable || shipintersect){
			explodeState = 1;
			explode = Images.explode.getSubimage(0, 0, 128, 128);
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					explode = Images.explode.getSubimage(explodeState*128, 0, 128, 128);
					if(explodeState != 11){
						explodeState++;
					}
				}
			}, 0, 40);
		}
	}
	public boolean outOfScreen(int x, int y, int w, int h){
		return !getBorder().intersects(x, y, w, h);
	}	
	public boolean intersect(Missile m){
		return intersect(m.getBorder());
	}
	public boolean intersect(Rectangle r){
		if(explodeState == 0){
			return getBorder().intersects(r);
		}
		return false;
	}
}
