/*
 * Graphics and Computation - 433-380
 * Project 2
 * 06 May 2006
 *
 * Brad Plant   - bkplant
 * Adam Peacock - adamdp
 */

import javax.media.opengl.*;
import com.sun.opengl.util.*;
import java.lang.Math;


/**
 * This object represents a score pill.
 */
public class ScorePill extends StationaryGameObject
{
	/**
	 * Radius of score pill.
	 */
	private final float radius = 0.25f;

	/**
	 * Color properties of the score pill.
	 */
	private final float color[] = {0.8f, 0.8f, 0.8f, 0.5f};

	/**
	 * The list id of the openGL glList.
	 * Override the variable in the GameObject class as all the
	 * score pills look the same so there isn't any point creating
	 * a separate list for each one.
	 */
	private static int glListId = -1;


	/**
	 * Constructor
	 *
	 * @param xCoordinate Specifies the xCoorindate of the score pill
	 * @param yCoordinate Specifies the xCoorindate of the score pill
	 * @param zCoordinate Specifies the xCoorindate of the score pill
	 */
	public ScorePill(float xCoordinate, float yCoordinate, float zCoordinate)
	{
		super(xCoordinate, yCoordinate + 1 + Settings.getHoverHeight(), zCoordinate);
	}

	
	/**
	 * Draw the score pill
	 *
	 * @param gl Specifies the GL pipeline this object uses.
	 */
	public void draw(GL gl)
	{
		if(!visible)
		{
			return;
		}

		Pacman p = Game.getPacman();

		if(Math.sqrt((xCoordinate - p.getXCoordinate())
					* (xCoordinate - p.getXCoordinate())
					+ (zCoordinate - p.getZCoordinate())
					* (zCoordinate - p.getZCoordinate()))
				< Settings.getContactDistance())
		{
			visible = false;

			Game.getOSD().updateScore(1);

			return;
		}

		gl.glPushMatrix();

			gl.glTranslatef(xCoordinate, yCoordinate, zCoordinate);

			gl.glCallList(glListId);

		gl.glPopMatrix();
	}


	/**
	 * Initialise something to do with drawing.
	 * I.e. compling a list of openGL commands to draw the object?
	 *
	 * @param gl Specifies the GL pipeline this object uses.
	 */
	public void draw_init(GL gl)
	{
		if(glListId != -1)
		{
			return;
		}

		glListId = gl.glGenLists(1);
		gl.glNewList(glListId, gl.GL_COMPILE);

			GLUT glut = new GLUT();
	
			gl.glPushMatrix();
	
			gl.glColor4fv(color, 0);

			glut.glutSolidSphere(radius,
					(int)(5 + Settings.getLevelOfDetail() * 10),
					(int)(5 + Settings.getLevelOfDetail() * 10));

			gl.glPopMatrix();

		gl.glEndList();
	}
}
