package finale.boss;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import com.sun.opengl.util.texture.Texture;

import devUtil.TextureLoader;
import devUtil.Vector3;
import finale.Player;
import util.MP3;
import world.GameObject;

public class Meteor extends GameObject {
	
	Vector3 position;
	Vector3 direction;
	
	double radius;
	
	double velocity;
	
	Texture texture;
	
	MP3 woosh = new MP3("/audio/FX/Missle.mp3");
	boolean wooshed;
	
	public Meteor() {
		
		this.position = new Vector3(
				Math.random() * 10000 - 5000,
				17000,
				Math.random() * 10000 - 5000);
		
		direction = new Vector3();
		
		this.radius = 100;
		
		velocity = 1;
	}
	
	int time;
	
	public void update() {
		
		time++;
		
		if(camera.getXPosition() < position.getX())
			direction.setX(direction.getX() + .025);
		
		if(camera.getXPosition() > position.getX())
			direction.setX(direction.getX() - .025);
		
		if(camera.getYPosition() < position.getY())
			direction.setY(direction.getY() + .025);
		
		if(camera.getYPosition() > position.getY())
			direction.setY(direction.getY() - .025);
		
		if(camera.getZPosition() < position.getZ())
			direction.setZ(direction.getZ() + .025);
		
		if(camera.getZPosition() > position.getZ())
			direction.setZ(direction.getZ() - .025);
		
		if(direction.getX() > 10)
			direction.setX(10);
		
		if(direction.getX() < -10)
			direction.setX(-10);
		
		if(direction.getY() > 10)
			direction.setY(10);
		
		if(direction.getY() < -10)
			direction.setY(-10);
		
		if(direction.getZ() > 10)
			direction.setZ(10);
		
		if(direction.getZ() < -10)
			direction.setZ(-10);
		
		velocity += .25;
		
		position = new Vector3(
				position.getX(),
				position.getY() - velocity,
				position.getZ());
		
		//radius -= .25;
		
		if(position.getY() < 2000 && !wooshed) {
			
			if((int) (Math.random() * 10) == 0)
				woosh.play(1);
			
			wooshed = true;
		}
		
		if(position.getY() < 0 || radius <= 0) {
			
			new MP3("/audio/FX/Grenade.mp3").play(1);
			
			destroy();
		}
	}
	
	public void render(GL gl) {
		
		if(texture == null)
			texture = TextureLoader.getTexture("/visual/textures/Sun.jpg");
		
		gl.glColor3f(0, 1, 1);
		
		texture.enable();
		texture.bind();
		
		gl.glTranslated(position.getX(), position.getY(), position.getZ());
		
		GLUquadric projectile = glu.gluNewQuadric();
		
		glu.gluQuadricTexture(projectile, true);
		glu.gluQuadricDrawStyle(projectile, GLU.GLU_FILL);
		glu.gluQuadricNormals(projectile, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(projectile, GLU.GLU_OUTSIDE);
		
		final int slices = 16;
		final int stacks = 16;
		
		glu.gluSphere(projectile, radius, slices, stacks);
		glu.gluDeleteQuadric(projectile);
		
		gl.glTranslated(-position.getX(), -position.getY(), -position.getZ());
		
		gl.glColor3f(1, 1, 1);
		
		texture.disable();
	}
	
	public void interact(GameObject object) {
		
		if(object instanceof Player) {
			
			Vector3 space = new Vector3(position.getX(), position.getY(), position.getZ());
			space.subtract(camera.getPosition());
			
			if(space.getMagnitude() <= 100) {
				
				((Player) object).damage(10);
				new MP3("/audio/FX/Grenade.mp3").play(1);
				
				destroy();
			}
		}
	}
}