/*
	Trig Wars.
	Copyright (c) 2008, 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 POSITIONAL_OBJECT_H
#define POSITIONAL_OBJECT_H

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

#include "GameObjects\GameObjectBase.h"
#include "Game\GameCore.h"

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

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================

	class Camera2D;

	//=================================================================================================================
	/// @brief		Base class for all game entities that have a position and an orientation
	//=================================================================================================================

	class PositionalObject : public GameObjectBase
	{
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				//=====================================================================================================
				/// @brief		Constructor: sets the position and orientation of the object
				/// @param		position - position of the object
				/// @param		angle - angle in which the object is facing
				//=====================================================================================================
				
				inline PositionalObject( const Vec2f & position = Vec2f::Zero() , float angle = 0 )
				{
					m_position	= position;
					m_angle		= angle;
				}

				//=====================================================================================================
				/// @brief		Sets the new position for the object
				/// @param		p - new position
				/// @remarks	This function also wraps the coordinates to stay within map boundaries.
				/// @pre		Given floating point position must be valid
				//=====================================================================================================

				inline void SetPosition( const Vec2f & p )
				{
					ENSURE( Float::IsValid( p.x ) , "Given floating point position must be valid" );
					ENSURE( Float::IsValid( p.y ) , "Given floating point position must be valid" );
				
					m_position = WrapCoordinates(p);
				}

				/// Wraps the given coordinates. If they are off the side of the map they come back at the other side.

				static Vec2f WrapCoordinates( const Vec2f & p );

				/// Unwraps the given coordinates relative to the given point. See full description for more details.

				static Vec2f UnwrapCoordinates( const Vec2f & relative_to , const Vec2f & wrapped );
				
				//=====================================================================================================
				/// @brief		Tells if the given position is wrapped to map coordinates
				/// @param		p - point to check
				/// @returns	True if the point is wrapped
				/// @pre		Given floating point position must be valid
				//=====================================================================================================
				
				static inline bool IsWrapped( const Vec2f & p )
				{
					ENSURE( Float::IsValid( p.x ) , "Given floating point position must be valid" );
					ENSURE( Float::IsValid( p.y ) , "Given floating point position must be valid" );				
				
					if ( p.x < -GameCore::MAP_AREA.x ) return false;
					if ( p.y < -GameCore::MAP_AREA.y ) return false;
					if ( p.x >  GameCore::MAP_AREA.x ) return false;
					if ( p.y >  GameCore::MAP_AREA.y ) return false;

					// If we got to here we are wrapped
					
					return true;
				}
				
				//=====================================================================================================
				/// @brief		Sets the angle for the object
				/// @param		angle - angle for the object
				//=====================================================================================================								
				
				inline void SetAngle( float angle )
				{ 
					// Save the angle and keep to a -180 to 180 range:

					m_angle = Angle::Wrap(angle);
				}
				
				//=====================================================================================================
				/// @brief		Gets the angle for the object
				/// @returns	Angle in which the object is facing
				//=====================================================================================================								
				
				inline float GetAngle() const { return m_angle; }				
				
				//=====================================================================================================
				/// @brief		Gets the position of the object
				/// @returns	Position of the object
				//=====================================================================================================
				
				inline Vec2f GetPosition() const { return m_position; }

				//=====================================================================================================
				/// @brief		Gets the square distance to the other given positional object
				/// @pre		Given object cannot be null
				/// @returns	Square distance to given object
				//=====================================================================================================
				
				inline float SqDistanceTo( PositionalObject * other ) const
				{
					// Make sure this is ok:
					
					ENSURE( other , "Given object cannot be null" );

					// Get a vector to the other object:

					Vec2f to_vec = m_position - other->GetPosition();

					// Return distance:

					return to_vec.SqLength();
				}
				
				//=====================================================================================================
				/// @brief		Gets the distance to the other given positional object
				/// @pre		Given object cannot be null
				/// @returns	Distance to given object
				/// @remarks	This function works correctly with wrapping. I.E it automatically unwraps the other
				///				object's coordinates
				//=====================================================================================================
				
				inline float DistanceTo( PositionalObject * other ) const
				{
					// Make sure this is ok:
					
					ENSURE( other , "Given object cannot be null" );

					// Get a vector to the other object:

					Vec2f to_vec = m_position - PositionalObject::UnwrapCoordinates( m_position , other->GetPosition() );

					// Return distance:

					return to_vec.Length();
				}
				
				//=====================================================================================================
				/// @brief		Tells if the positional object is within a given distance (or less) of a point
				/// @param		p - Point to check that we are within distance of
				/// @param		d - distance to check we are from the point
				/// @pre		Given distance should not be less than zero
				/// @pre		Given floating point position must be valid
				/// @pre		Given floating point distance must be valid
				//=====================================================================================================
				
				inline bool WithinDistance( const Vec2f & p , float d ) const
				{
					ENSURE( Float::IsValid( p.x )	, "Given floating point position must be valid" );
					ENSURE( Float::IsValid( p.y )	, "Given floating point position must be valid" );
					ENSURE( Float::IsValid(d)		, "Given floating point distance must be valid" );
				
					// Make sure ok:
					
					ENSURE( d >= 0 , "Given distance should not be less than zero" );
					
					// See if we are within square distance:
					
					if ( ( p - m_position ).SqLength() <= d * d ) return true; else return false;
				}

				//=====================================================================================================
				/// @brief		Returns the direction of this object
				/// @returns	Direction vector of this object
				//=====================================================================================================

				inline Vec2f GetDirection() const
				{
					return Vec2f( Float::Cos(m_angle) , Float::Sin(m_angle) );
				}

				/// Tells if the game object is onscreen, given a size and the given camera

				bool IsOnscreen( const Vec2f & object_size , Camera2D * c ) const;
				
		private:
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Position of the object
				
				Vec2f m_position;
			
				/// Angle of orientation for the object
				
				float m_angle;
	};
};

#endif
