/**
*
* \file AVP_Platform_Player.cpp
*
* Created by Anthony Gregson - 31/08/06
*
* Copyright (C) 2006 Rebellion, All Rights Reserved.
*
* \brief
*
**/

#include "Core/Asura.h"

#include "Network/AVP_Player.h"

#include "Core/Asura_UserInput.h"
#include "Rendering/Asura_Font_System.h"

#include "Entity/PhysicalObject/Actor/AVP_ClientEntity_Actor.h"
#include "Entity/PhysicalObject/Actor/AVP_Movement_System.h"

DEBUGVAR float dbg_fMouseSensitivity = 1.0f;

void AVP_Player::Platform_UpdateControlData()
{
	//Clear values
	m_xControlData.Reset();

	//Look
	if (Asura_UserInput::KeyPressed(ASURA_KEYID_LEFT) || Asura_UserInput::KeyPressed(ASURA_KEYID_RIGHT))
	{
		m_xControlData.m_fLookHorizontal = (Asura_UserInput::KeyPressed(ASURA_KEYID_RIGHT) ? 1.0f : 0.0f) - (Asura_UserInput::KeyPressed(ASURA_KEYID_LEFT) ? 1.0f : 0.0f);
	}
	else
	{
		m_xControlData.m_fLookHorizontal = Asura_UserInput::GetAxisValue(ASURA_AXIS_MOUSE_H) * dbg_fMouseSensitivity;
	}
	if (Asura_UserInput::KeyPressed(ASURA_KEYID_UP) || Asura_UserInput::KeyPressed(ASURA_KEYID_DOWN))
	{
		m_xControlData.m_fLookVertical = (Asura_UserInput::KeyPressed(ASURA_KEYID_UP) ? 1.0f : 0.0f) - (Asura_UserInput::KeyPressed(ASURA_KEYID_DOWN) ? 1.0f : 0.0f);
	}
	else
	{
		m_xControlData.m_fLookVertical = -Asura_UserInput::GetAxisValue(ASURA_AXIS_MOUSE_V) * dbg_fMouseSensitivity;
	}

	//Move
	if( UseMoveVelocity() )
	{
		AVP_ClientEntity_Actor* pxClientActor = AVP_ClientEntity_Actor::FindEntity( GetGuid() );
		if( pxClientActor 
		 && pxClientActor->GetMovementSystem() )
		{
			m_xControlData.m_xMovementInput = pxClientActor->GetMovementSystem()->GetMoveVelocity();
		}
	}
	else
	{
		m_xControlData.m_xMovementInput.x = (Asura_UserInput::KeyPressed('d') ? 1.0f : 0.0f) - (Asura_UserInput::KeyPressed('a') ? 1.0f : 0.0f);
		m_xControlData.m_xMovementInput.y = (Asura_UserInput::KeyPressed('z') ? 1.0f : 0.0f) - (Asura_UserInput::KeyPressed('q') ? 1.0f : 0.0f);
		m_xControlData.m_xMovementInput.z = (Asura_UserInput::KeyPressed('w') ? 1.0f : 0.0f) - (Asura_UserInput::KeyPressed('s') ? 1.0f : 0.0f);
		if (m_xControlData.m_xMovementInput.MagnitudeSquared() > 0.001f)
		{
			m_xControlData.m_xMovementInput.Normalise();
		}

		if (Asura_UserInput::KeyPressed(ASURA_KEYID_LEFTSHIFT))
		{
			m_xControlData.m_xMovementInput *= 3.0f;
		}
	}

	//Flags
	m_xControlData.m_bChangeCameraType = Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_MOUSEBUTTON2);
	m_xControlData.m_bChangeMovementType = Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_MOUSEBUTTON1);
	m_xControlData.m_bChangePhysicsConstraint = Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_LEFTALT);
	m_xControlData.m_bChangeInvertedLook = Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_MOUSEBUTTON3);
}

void AVP_Player::Platform_Render()
{
	Asura_Bounding_Rectangle xRect(0.05f, 0.05f, 0.95f, 0.95f);

	Asura_Font_System::Print(xRect, 0.0f, 0.0f, 0.0f, 1.4f, 0xFFFFFFFF, ASURA_TEXTFORMAT_LEFTJUSTIFIED | ASURA_TEXTFORMAT_RELATIVEPOSITION, "w - forward; s - back; a - left; d - right; q - up; z - down; mouse - look; left shift - fast movement");
	Asura_Font_System::Print(xRect, 0.0f, 0.07f, 0.0f, 1.4f, 0xFFFFFFFF, ASURA_TEXTFORMAT_LEFTJUSTIFIED | ASURA_TEXTFORMAT_RELATIVEPOSITION | ASURA_TEXTFORMAT_FORMATSTRING, "Fly mode: %s (LMB)", (m_eMovementType == AVP_PLAYER_MOVEMENT_FLYING) ? "On" : "Off");
	Asura_Font_System::Print(xRect, 0.0f, 0.14f, 0.0f, 1.4f, 0xFFFFFFFF, ASURA_TEXTFORMAT_LEFTJUSTIFIED | ASURA_TEXTFORMAT_RELATIVEPOSITION | ASURA_TEXTFORMAT_FORMATSTRING, "3rd person: %s (RMB)", (m_eCameraType == CAMERA_THIRD_PERSON) ? "On" : "Off");
	Asura_Font_System::Print(xRect, 0.0f, 0.21f, 0.0f, 1.4f, 0xFFFFFFFF, ASURA_TEXTFORMAT_LEFTJUSTIFIED | ASURA_TEXTFORMAT_RELATIVEPOSITION | ASURA_TEXTFORMAT_FORMATSTRING, "Axis-aligned collision: %s (L ALT)", (m_ePhysicsConstraint == AVP_PLAYER_PHYSICSOBJECT_CONSTRAINT_ALL_AXES) ? "On" : "Off");
	Asura_Font_System::Print(xRect, 0.0f, 0.28f, 0.0f, 1.4f, 0xFFFFFFFF, ASURA_TEXTFORMAT_LEFTJUSTIFIED | ASURA_TEXTFORMAT_RELATIVEPOSITION | ASURA_TEXTFORMAT_FORMATSTRING, "Inverted look: %s (MMB)", m_bInvertedLook ? "On" : "Off");
}
