/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GAME_ANGLE_H
#define GAME_ANGLE_H

//=====================================================================================================================
//=====================================================================================================================

#include <cmath>

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	/// @brief		Class that contains utility functions for dealing with angles.
	//=================================================================================================================

	struct Angle
	{	
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================

				//=====================================================================================================
				/// @brief		Converts the given angle from radians to degrees
				/// @param		a - angle to convert to degrees
				/// @returns	Angle in degrees
				//=====================================================================================================

				__forceinline static float ToDegrees( float a )
				{
					return a * 57.295779513082320876798154814105f;
				}

				//=====================================================================================================
				/// @brief		Converts the given angle from degrees to radians
				/// @param		a - angle to convert to radians
				/// @returns	Angle in radians
				//=====================================================================================================

				__forceinline static float ToRadians( float a )
				{
					return a * 0.017453292519943295769236907684886f;
				}

				//=====================================================================================================
				/// @brief		Returns a 2D directional vector for the given angle
				/// @param		a - angle
				/// @returns	Vector for the given angle
				/// @remarks	Given vector is unit length. Angle is presumed to be in radians.
				//====================================================================================================

				__forceinline static Vector2<float> GetDirection( float a )
				{
					return Vector2<float>( Float::Cos(a) , Float::Sin(a) );
				}

				//=====================================================================================================
				/// @brief		Wraps the given angle to be in a -179.9999999999999 to +180 degrees range
				/// @param		a - angle to wrap
				/// @returns	Wrapped version of the angle
				/// @remarks	Angle is presumed to be in radians
				//====================================================================================================

				__forceinline static float Wrap( float a )
				{
					// Wrap first in 0-2PI range:

					a = Float::Mod( a , Float::TwoPI() );

					// See if less than -180 degrees:

					if ( a <= -Float::PI() )
					{
						// Angle is 360 + current angle:

						a += Float::TwoPI();
					}
					else if ( a > Float::PI() )
					{
						// Angle is -360 + current angle:

						a -= Float::TwoPI();
					}

					// Return the wrapped angle:

					return a;
				}

				//=====================================================================================================
				/// @brief		Tells if the given angle is wrapped in a -179.999999999999 to +180 range
				/// @param		a - angle to wrap
				/// @returns	Wrapped version of the angle
				/// @remarks	Angle is presumed to be in radians
				//====================================================================================================

				__forceinline static bool IsWrapped( float a )
				{
					// See if less than or equal to -180 degrees:

					if ( a <= - Float::PI() ) return false;

					// See if greater than 180 degrees:

					if ( a > Float::PI() ) return false;

					// Angle is wrapped: everything is ok:

					return true;
				}

				//=====================================================================================================
				/// @brief		Gets the difference between the two angles, or the amount you would have to turn to get 
				///				from one angle to another. 
				/// @param		a1 - first angle we are turning from
				/// @param		a2 - second angle that we are turning from a1 to
				/// @returns	The amount of turning to get from a1 to a2. 
				/// @remarks	The order of the angles affects the turning amout. It will be the opposite if the 
				///				parameters are passed in the opposite order.
				/// @post		The angle returned should never be more than 180 degrees, positive or negative
				//====================================================================================================

				__forceinline static float Difference( float a1 , float a2 )
				{
					// Get the difference between the two angles:

					float diff = a2 - a1;
					
					// In debug mode validate the post condition
					
					#ifdef DEBUG
					
						// Wrap to a 180 to -180 range:
					
						float wrapped = Wrap(diff);
						
						// Validate:
						
						ENSURE( Float::Abs(wrapped) < Float::PI() * 1.0001f , "The angle returned should never be more than 180 degrees, positive or negative" )
						
						// Return
						
						return wrapped;
					
					#else					

						// Wrap to a 180 to -180 range:

						return Wrap( diff );
						
					#endif
				}

				//=====================================================================================================
				/// @brief		Figures out the fastest way (clockwise or anti-clockwise) of turning towards 
				///				a given angle, from a given angle and returns the turning amount. Does not turn 
				///				past the given angle if the turn amount is too much
				/// @param		turn_amount - maximum amount of turning that can be done
				/// @param		from_angle - angle we are turning from
				/// @param		to_angle - angle we are turning to face
				/// @returns	The amount of turning that should be done
				/// @pre		from_angle should be wrapped. Use Angle::Wrap() to ensure this.
				/// @pre		to_angle should be wrapped. Use Angle::Wrap() to ensure this.
				//====================================================================================================

				__forceinline static float TurnTo( float turn_amount , float from_angle , float to_angle )
				{

					// Get the difference between the two angles:

					float diff = Difference( from_angle , to_angle );

					// See if negative or not:

					if ( diff < 0 )
					{
						// Make turn amount negative:

						turn_amount = -turn_amount;

						// See if turning too much:

						if ( turn_amount < diff ) turn_amount = diff;
					}
					else
					{
						// See if turning too much:

						if ( turn_amount > diff ) turn_amount = diff;
					}

					// Return the turn amount:

					return turn_amount;
				}
	};
};

#endif 
