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

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

vector3 basex(1.0f,0.0f,0.0f);
vector3 basey(0.0f,1.0f,0.0f);
vector3 basez(0.0f,0.0f,1.0f);

RTSCamera::RTSCamera()
{

	mouse_x_sensibility=0.002f;
	mouse_y_sensibility=0.002f;
	last_mouse_x_move=0.0f;
	last_mouse_y_move=0.0f;
	move_speed=1.0f;

	p.setPosition(0,5*CASE_SIDE_SIZE,0);
	p.setAlphaXZ(-3.141f/2.0f);
	//p.setAlphaZY(1.4f);
	p.setAlphaZY(1.1f);
	p.compute();
	
	for(int i = 0;i<RTSC_NB_MOUVEMENT;i++)
	{
		activeMovement[i]=false;
	}
}

RTSCamera::~RTSCamera()
{

}

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

void RTSCamera::setCurMousePos(const float mx,const float my)
{
	cur_mouse_x_pos=mx;
	cur_mouse_y_pos=my;
}

void RTSCamera::computeMovement(const float elapsedTime)
{
	///////// PARTIE DEPLACEMENT /////////
	const float offset = 20.0f;
	vector3 sumDep;

	if(cur_mouse_y_pos<=offset)
	{
		sumDep = sumDep + basex*((offset-cur_mouse_y_pos)/offset);
	}
	if(cur_mouse_y_pos>=screenHeight-offset)
	{
		sumDep = sumDep + basex*(-1)*((offset-(screenHeight-cur_mouse_y_pos))/offset);;
	}
	if(cur_mouse_x_pos<=offset)
	{
		sumDep = sumDep + basez*(-1)*((offset-cur_mouse_x_pos)/offset);
	}
	if(cur_mouse_x_pos>=screenWidth-offset)
	{
		sumDep = sumDep + basez*((offset-(screenWidth-cur_mouse_x_pos))/offset);
	}
	if(sumDep.length()!=0)
	{
		//mise à l'échelle du déplacement
		//sumDep.normalize();				//les diagonal sont plus rapide :-(
		sumDep = sumDep*elapsedTime*move_speed;
		p.deltaPosition(&sumDep);
	}

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

	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 )
	{
		last_mouse_x_move = 0.0f;
		last_mouse_y_move = 0.0f;
		p.compute();
	}

}

void RTSCamera::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 RTSCamera::setProperties(const float _headUpSensibility,const float _move_speed,int screenW, int screenH)
{
	mouse_x_sensibility=_headUpSensibility;
	mouse_y_sensibility=_headUpSensibility;
	move_speed=_move_speed;
	screenWidth = screenW;
	screenHeight = screenH;
}

float RTSCamera::getMoveSpeed()
{
	return this->move_speed;
}

void RTSCamera::setMoveSpeed(float s)
{
	this->move_speed = s;
}
