package world;

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


import eng.Data;
import eng.GameState;

@SuppressWarnings("serial")
public class GameScene extends GameState {
	
	protected GameWorld world;
	protected Camera camera;
	
	public GameScene() {
		super();
	}
	
	public void initiate(Data data) {
		
		super.initiate(data);
		
		camera = new Camera();
	}
	
	public void GLDisplay(GLAutoDrawable glad) {
		
		GL gl = glad.getGL();
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glShadeModel(GL.GL_SMOOTH);
		
		GLU glu = new GLU();
		
		glu.gluLookAt(camera.getXPosition(), camera.getYPosition(), camera.getZPosition(),
				camera.getXPosition() + camera.getXView(),
				camera.getYPosition() + camera.getYView(),
				camera.getZPosition() + camera.getZView(),
				camera.getXAngle(), camera.getYAngle(), camera.getZAngle());
		
		render(gl);
	}
	
	public void GLInit(GLAutoDrawable glad) {
		
		GL gl = glad.getGL();
		
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_LINE_SMOOTH);
		gl.glEnable(GL.GL_LIGHTING);
		
		gl.glDepthFunc(GL.GL_LEQUAL);
		
		initializeWorld(gl);
	}
	
	public void initializeWorld(GL gl) {
		world = new GameWorld(gl);
	}
	
	public void GLReshape(GLAutoDrawable glad,
			int x, int y, int width, int height) {
		
		GL gl = glad.getGL();
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		GLU glu = new GLU();
		
		glu.gluPerspective(camera.getFOV(),
				((float)getWidth() / (float) getHeight()), .1, camera.getDrawDistance());
	}
	
	public void render(GL gl) {
		
		world.render(gl);
	}
	
	public void update() {
		
		if(world != null) {
			
			world.update(camera, input, data);
			
			data = world.getData();
			camera = world.getCamera();
			
			nextGameState = world.getTriggeredGameState();
			
			paused = world.isScenePaused();
			ended = world.isSceneEnded();
		}
	}
}