package finale;

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 object.Model;

import util.MP3;
import world.GameObject;

public class Rocket extends GameObject {
	
	Model rocket;
	
	MP3 earthquake = new MP3("/audio/FX/Earthquake.mp3");
	
	public Rocket() {
		
		rocket = new Model();
		
		rocket.setMode(true, false);
		rocket.parseObjectFile(ClassLoader.getSystemResourceAsStream(
						"visual/models/Rocket.obj"));

		rocket.setPosition(2500, 1000, 2500);
		rocket.setRotation(-90, 0, 0);
		rocket.setSize(1000, 1000, 2000);
		
		rocket.setColor(0, 0, 1);
	}
	
	int time;
	
	public void update() {
		
		time += 2;
		
		if(time == 100)
			earthquake.play();
		
		if(time >= 200) {
			
			rocket.setYPosition(1000 - (time - 200));
			
			if(time % 10 < 5) {
				rocket.setXPosition(2490);
				rocket.setXPosition(2510);
			}
			
			else {
				rocket.setXPosition(2510);
				rocket.setXPosition(2490);
			}
		}
		
		if(time == 500)
			new MP3("/audio/FX/Grenade.mp3").play(1);
		
		if(time == 2600) {
			
			earthquake.stop();
			
			destroy();
		}
	}
	
	Texture fire = TextureLoader.getTexture("/visual/Textures/Sun.jpg");
	
	public void render(GL gl) {
		
		if(time < 2200)
			rocket.render(gl);
		
		if((time > 500 && time < 2200) ||
				(time >= 2200 && time < 2500) && time % 10 < 5) {
			
			fire.enable();
			fire.bind();
			
			gl.glTranslated(2500, 0, 2500);
			
			GLUquadric skybox = glu.gluNewQuadric();
			
			glu.gluQuadricTexture(skybox, true);
			glu.gluQuadricDrawStyle(skybox, GLU.GLU_FILL);
			glu.gluQuadricNormals(skybox, GLU.GLU_FLAT);
			glu.gluQuadricOrientation(skybox, GLU.GLU_OUTSIDE);
			
			final float radius = time - 500;
			final int slices = 16;
			final int stacks = 16;
			
			glu.gluSphere(skybox, radius, slices, stacks);
			glu.gluDeleteQuadric(skybox);
			
			gl.glTranslated(-2500, 0, -2500);
			
			fire.disable();
		}
	}
}