// Author : Chris Tolley
// Date   : 7/21/2004
// SurvivalCamera.cpp contains the class definition for a camera to use
// in the game Survival

#pragma warning ( disable : 4305 )

#include <windows.h>
#include <gl/glu.h>

#include "SurvivalCamera.h"
#include "InputSystem.h"
#include "quadTreeTerrain.h"
#include "ms3d.h"

// Default Constructor
SurvivalCamera::SurvivalCamera() : m_fYOff( 40 )
{
	// Set up the camera into it's initial position
	m_fCenter[0] = 0;  m_fCenter[1] = 0;  m_fCenter[2] = 0;
}// End SurvivalCamera::Constructor


// Destructor
SurvivalCamera::~SurvivalCamera()
{}


// Sets the input system and the terrain
void SurvivalCamera::Initialize( CInputSystem* input, 
								 quadTreeTerrain* terrain )
{
	// Set the input and the terrain
	m_Input = input;
	m_qtTerrain = terrain;
}// End SurvivalCamera::Initialize


// Updates the camera
void SurvivalCamera::Update()
{
	// Based on the keyboard input, move the camera around
	if( m_Input->KeyDown( DIK_A ) )
		m_fCenter[0] += .5;

	if( m_Input->KeyDown( DIK_D ) )
		m_fCenter[0] -= .5;

	if( m_Input->KeyDown( DIK_W ) )
		m_fCenter[2] += .5;

	if( m_Input->KeyDown( DIK_X ) )
		m_fCenter[2] -= .5;

	// Make sure that we are still on the map
	int size = m_qtTerrain->GetSize();
	if( m_fCenter[0] < 0 )
		m_fCenter[0] = 0;

	if( m_fCenter[0] > size )
		m_fCenter[0] = size;

	if( m_fCenter[2] < 0 )
		m_fCenter[2] = 0;

	if( m_fCenter[2] > size )
		m_fCenter[2] = size;

	// Based on the mouse, move the camera along the y axis
	if( m_Input->ButtonDown( 0 ) )
		m_fYOff += .2;

	if( m_Input->ButtonDown( 1 ) )
		m_fYOff -= .2;

	// Make sure m_fYOff is in range
	if( m_fYOff > 80 )
		m_fYOff = 80;

	if( m_fYOff < 15 )
		m_fYOff = 15;

	gluLookAt( m_fCenter[0], 
			   m_fCenter[1] + 
				 m_qtTerrain->GetHeightAt( m_fCenter[0], m_fCenter[2] )
																	+ m_fYOff,
			   m_fCenter[2],
			   
			   m_fCenter[0], m_fCenter[1], m_fCenter[2] + 30,
			   
			   0, 0, 1 );
}// End SurvivalCamer::Update