/*
*	SimpleCamera.cpp
*
*	Programmé par Hillaire Sébastien
*
*
*	Se référé au fichier en-tête .h pour plus de détail
*
*/

#include "SimpleCamera.h"
#include "MemoryManager/DebugNew.h"


SimpleCamera::SimpleCamera()
{

	mouse_x_sensibility=0.002f;
	mouse_y_sensibility=0.0009f;
	last_mouse_x_move=0.0f;
	last_mouse_y_move=0.0f;
	move_speed=1.0f;
	
	for(int i = 0;i<NB_MOUVEMENT;i++)
	{
		activeMovement[i]=false;
	}
}

SimpleCamera::~SimpleCamera()
{

}

void SimpleCamera::setAvance(const bool m)
{
	activeMovement[AVANCE]=m;
}

void SimpleCamera::setRecule(const bool m)
{
	activeMovement[RECULE]=m;
}

void SimpleCamera::setGauche(const bool m)
{
	activeMovement[GAUCHE]=m;
}

void SimpleCamera::setDroite(const bool m)
{
	activeMovement[DROITE]=m;
}

void SimpleCamera::setHaut(const bool m)
{
	activeMovement[HAUT]=m;
}

void SimpleCamera::setBas(const bool m)
{
	activeMovement[BAS]=m;
}

void SimpleCamera::setLastMouseMove(const float dx, const float dy)
{
	last_mouse_x_move=dx;
	last_mouse_y_move=dy;
}

void SimpleCamera::computeMovement(const float elapsedTime)
{
	///////// PARTIE DEPLACEMENT /////////
	vector3 sumDep;
	vector3 tmp;


	if(activeMovement[AVANCE])
	{
		tmp = p.getBaseZ();
		sumDep = sumDep + tmp;
	}

	if(activeMovement[RECULE])
	{
		tmp = p.getBaseZ();
		sumDep = sumDep + tmp*(-1);
	}

	if(activeMovement[GAUCHE])
	{
		tmp = p.getBaseX();
		sumDep = sumDep + tmp;
	}

	if(activeMovement[DROITE])
	{
		tmp = p.getBaseX();
		sumDep = sumDep + tmp*(-1);
	}

	if(activeMovement[HAUT])
	{
		tmp = p.getBaseY();
		sumDep = sumDep + tmp;
	}

	if(activeMovement[BAS])
	{
		tmp = p.getBaseY();
		sumDep = sumDep + tmp*(-1);
	}

	if(sumDep.length()!=0)
	{
		//mise à l'échelle du déplacement
		sumDep.normalize();
		sumDep = sumDep*elapsedTime*move_speed;
		p.deltaPosition(&sumDep);
	}

	///////// PARTIE REGARD /////////

	p.deltaAlphaXZ(mouse_x_sensibility*last_mouse_x_move);
	p.deltaAlphaZY(-mouse_y_sensibility*last_mouse_y_move);	//- car sur l'écran y est inversé

	if( last_mouse_x_move!=0 || last_mouse_y_move!=0 )
	{
		p.compute();
	}

}

void SimpleCamera::gluLookAt()
{
	vector3 pos = *p.getPosition();
	vector3 depth = *p.getBaseZ();
	vector3 up = *p.getBaseY();

	::gluLookAt(pos[0], pos[1], pos[2], pos[0]+depth[0], pos[1]+depth[1], pos[2]+depth[2], up[0], up[1], up[2]);
}

void SimpleCamera::setProperties(const float _mouse_x_sensibility,const float _mouse_y_sensibility,const float _move_speed)
{
	mouse_x_sensibility=_mouse_x_sensibility;
	mouse_y_sensibility=_mouse_y_sensibility;
	move_speed=_move_speed;
}
