/*
	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 OBSTACLE_H
#define OBSTACLE_H

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

#include "GameObjects\PositionalObject.h"

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

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	class QuadTreeEntry;

	//=================================================================================================================
	/// @brief		Class representing an obstacle. An obstacle is basically a sphere that has it's points jutted 
	///				inwards by random amounts to give the appearance of being a deformed rock or something.. 
	///				Again like enemies, this object is not designed to be placed directly in the game. Rather it should
	///				be encapsulated in an Obstacle manager which creates and maintains this obstacle.
	//=================================================================================================================

	class Obstacle : public PositionalObject
	{
		public:
	
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates the obstacle with the given parameters
				
				Obstacle( unsigned num_points , const Vec2f & position , float min_radius , float max_radius );
				
				/// Destroys this obstacle
				
				virtual ~Obstacle();
				
				/// Returns the type name for this object

				virtual const char * GetTypeName() const;
				
				/// Gets the radius of this obstacle
				
				inline float GetRadius() const { return m_radius; }
				
				/// Do a buffered render and save the lines in the obstacle to the given stack for later rendering

				unsigned BufferedDraw( Stack & s , const Vec2f & cam_pos , float z ) const;
				
				/// Returns the quad tree entry used by this obstacle
				
				inline QuadTreeEntry * GetQuadTreeEntry(){ return m_quad_tree_entry; } 
				
				/// Sets the quad tree entry for this obstacle
				
				inline void SetQuadTreeEntry( QuadTreeEntry * entry ){ m_quad_tree_entry = entry; }
				
				//=====================================================================================================
				/// @brief		Tells if the given bounding circle collides with this obstacle
				/// @param		circle_pos - position of the circle
				/// @param		circle_radius - radius of the circle
				/// @returns	True if there is a collision
				/// @remarks	Test is a simple bounding circle test. Per line collision is not done for speed.
				//=====================================================================================================

				inline bool CollidesWithCircle( const Vec2f & circle_pos , float circle_radius ) const
				{
					// Need to unwrap our position relative to the position of the circle
					
					Vec2f p_unwrapped = UnwrapCoordinates( circle_pos , GetPosition() );
				
					// See if the square distance from us to the circle is less than the square radius of 
					
					if ( circle_pos.SqDistanceTo( p_unwrapped ) < ( circle_radius * circle_radius ) + m_sq_radius )
					{
						// There is a collision
						
						return true;
					}
					else
					{
						// No collision:
						
						return false;
					}
				}

		private:
		
			//=========================================================================================================
			// Constants
			//=========================================================================================================
			
				/// The color of each obstacle in unsigned format
				
				static const unsigned OBSTACLE_COLOR;
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// An array holding all of the points for the obstacle. These are stored in local coordinates.
			
				Vec2f * m_points;
				
				/// Number of points in this obstacle
				
				unsigned m_num_points;
				
				/// Radius of the obstacle
				
				float m_radius;
				
				/// Square radius of the obstacle
				
				float m_sq_radius;
				
				/// Object's entry in the quad tree
				
				QuadTreeEntry * m_quad_tree_entry;
	};
};

#endif
