import java.awt.Rectangle;
import java.util.Random;


public class Chunk extends Entity {
	
	Random r = new Random();
	private int createNewParticleTime = 0;
	private int timeBetweenParticles = 100;
	Game game;
	
	public Chunk(String ref, Game game, int x, int y) {
		super(ref, x, y, -1, 20, 10, 10, true);
		
		sprite = game.pm.chunks[r.nextInt(game.pm.chunks.length)];
		
		this.game = game;
		
		dx += r.nextInt(800) - 400;
		dy += r.nextInt(800) - 600;
	}

	public void collidedWith(Entity other) {
		if (other instanceof Block || other instanceof Piston) {
//			super.dy = 0;
			
			if (!customSize) {
				me.setBounds((int) x,(int) y, sprite.getWidth(), sprite.getHeight());
			} else {
				me.setBounds(rect.x, rect.y, (int) rect.getWidth(), (int) rect.getHeight());
			}
			if (!other.customSize) {
				him.setBounds((int) other.x, (int) other.y, other.sprite.getWidth(), other.sprite.getHeight());
			} else {
				him.setBounds(other.rect.x, other.rect.y, (int) other.rect.getWidth(), (int) other.rect.getHeight());
			}
			Rectangle intersection = me.intersection(him);
			
			if (intersection.getHeight() > intersection.getWidth()) { // horizontal collision
				if (intersection.x == him.x) { // collision from right
					super.x -= intersection.getWidth();
				} else { // collision from left
					super.x += intersection.getWidth();
				}
			} else { // vertical collision
				if (intersection.y == him.y) { // collision from below
					super.y -= intersection.getHeight();
				} else { // collision from above
					super.y += intersection.getHeight();
				}
			}
			
			super.dx = super.dx * Game.FRICTION;
			super.dy = super.dy * Game.FRICTION;
		}
	}
	
	public void move(long delta) {
		
//		if (mode == 0) {
			
		dy += Game.GRAVITY;
		
		createNewParticleTime += delta;
		if (!this.isStill() && createNewParticleTime  > timeBetweenParticles) {
			createNewParticleTime = 0;
			game.pm.createBloodParticle((int) x, (int) y);
		}
		
		super.move(delta);
	}
	
}
