/**
 * Pipe.java
 * Handles collisions and rendering for pipes
 *
 * @author  Paul Krishnamurthy
 */

package com.example.flappybird;

public class Pipe extends HasPosition {
	// Placement (top or bottom) of pipe
	String location;

	// Pipe constants
	public static final int WIDTH = 67;
	public static final int HEIGHT = 416;

	// Horizontal distance between pipes
	public static final int PIPE_DISTANCE = 150;

	// Vertical distance between pipes
	public static final int PIPE_SPACING = HEIGHT + 200;

	// Speed of the bird.
	private static final int SPEED = -2;

	// If the bird can get a point passing this pipe
	public boolean canAwardPoint = true;

	public Pipe(String location) {
		super(FlappyBird.WIDTH + 5, 0);
		this.location = location;
		reset();
	}

	public void reset() {
		super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate

		// Set boundaries for top pipes
		// This y-coordinate + PIPE_SPACING will be for the bottom pipe
		if (location.equals("top")) {
			super.y = - Math.max((int) (Math.random() * 320) + 30, 140);
		}
	}

	/**
	 * Moves the pipe
	 */
	public void move() {
		super.x += SPEED;
	}

	/**
	 * Checks for bird colliding with pipe
	 * @param  bird The bird.
	 * @return If bird is colliding with the pipe
	 */
	public boolean collide(Bird bird) {
		// Do not allow bird to jump over pipe
		if (bird.getX() > super.x && bird.getY() < 0 && canAwardPoint) {
			return true;
		}

		return bird.getX() < super.x + WIDTH &&
				bird.getX() + bird.BIRD_WIDTH > super.x &&
				bird.getY() < super.y + HEIGHT &&
				bird.getY() + bird.BIRD_HEIGHT > super.y;
	}

	/**
	 * Set's pipe's y-coordinate (for bottom pipes)
	 * @param newY New y-coordinate
	 */
	public void setY(int newY) {
		y = newY;
	}
}

