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 Blaster extends GameObject {
	
	Vector3 position;
	Vector3 direction;
	
	double radius;
	
	Texture texture;
	
	public Blaster(Vector3 position, Vector3 direction, double radius) {
		
		this.position = position;
		this.direction = direction;
		
		this.radius = radius;
	}
	
	int time;
	
	public void update() {
		
		time++;
		
		position = new Vector3(
				position.getX() + direction.getX(),
				position.getY() + direction.getY(),
				position.getZ() + direction.getZ());
		
		if(position.getY() < 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(1, 0, 0);
		
		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() <= radius) {
				
				((Player) object).damage(10);
				new MP3("/audio/FX/Grenade.mp3").play(1);
				
				destroy();
			}
		}
	}
}