package spaceinvaders.objects;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

public class Block {
	private ArrayList<Rectangle> blocks;
	private Rectangle border;

	public Block(int x, int y){
		blocks = new ArrayList<Rectangle>();		
		for(int i = 0; i < 13; i++){
			for(int j = 0; j < 17; j++){
				if(!(j > 3 + (((i-13)*-1)/2) && j < 13 - (((i-13)*-1)/2) && i > 6)){
					if(!(i >=0 && i < 4 && (j >= 0 && j < 3-(i/2) || j > 13+(i/2) && j <= 16))){
						blocks.add(new Rectangle(x+j*5,y+i*5,5,5));
					}
				}
			}
		}
		border = new Rectangle(x,y,85,65);
	}
	public boolean missileCollision(Missile m){
		if(border.intersects(m.getRect())){
			Iterator<Rectangle> i = blocks.iterator();
			while(i.hasNext()){
				Rectangle r = i.next();
				if(r.intersectsLine(m.getRect().getX(), m.getRect().getY(), 5, 5)){
					i.remove();
					return true;
				}
			}
		}
		return false;
	}
	public void draw(Graphics2D g2){
		for(Rectangle r:blocks){
			g2.fill(r);
		}
	}
}
