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

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

#include "GameObjects/GameObjectBase.h"
#include "Graphics/Texture.h"
#include "Graphics/PixelShader.h"
#include "Graphics/VertexShader.h"
#include "Graphics/VertexFormat.h"
#include "Graphics/ShaderUniform.h"

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

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

	class GridMeshSoftBody;

	//=================================================================================================================
	/// @brief		This object represents the trampoline in the game.
	//=================================================================================================================

	class GameTrampoline : public GameObjectBase
	{
		public:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
				
				/// Creates the trampoline with the specified position and xz size and number of squares
				
				GameTrampoline( const Vec3f & position , const Vec2f & xz_size , unsigned num_squares , float mass );

				/// Destroys the trampoline

				~GameTrampoline();
				
				/// Renders the trampoline
				
				virtual void OnDraw( Camera3D * c );
				
				/// Returns the type name of this object
				
				virtual const char * GetTypeName() const { return "GameTrampoline"; }		
				
		private:

			//=========================================================================================================
			// Constants
			//=========================================================================================================
			
				/// Scale of the trampoline texture
				
				static const Vec2f TEXTURE_SCALE;
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Fills the index array: this only needs to be done once
				
				void FillIndexArray();
				
				/// Fills the vertex array:
				
				void FillVertexArray();
		
				/// Copying is disallowed
				
				GameTrampoline( const GameTrampoline & other );

				/// Copying is disallowed
				
				GameTrampoline & operator = ( const GameTrampoline & other );		
				
				// Debug only
				
				#ifdef USE_DEBUG_CONSOLE
				
					/// Enables / disables normal drawing
				
					static bool ConCmd_P_DrawTrampolineNormals( bool arg  , bool arg_given );
					
				#endif
		
			//=========================================================================================================
			// Structures
			//=========================================================================================================
			
				/// Structure of a tramoline vertex for rendering
				
				struct Vertex
				{
					/// Position of the vertex
					
					Vec3f m_position;
					
					/// Texture coordinate of the vertex 
					
					Vec2f m_texcoord;
					
					/// Normal at the vertex
					
					Vec3f m_normal;
				};	
				
				#ifdef USE_DEBUG_CONSOLE
				
					/// Structure of a tramoline normal vertex for rendering
					
					struct NormalVertex
					{
						/// Position of the vertex
						
						Vec3f m_position;
					};
					
				#endif
	
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Vertex shader used to render the plane
				
				VertexShader m_vs;
			
				/// Pixel shader used to render the plane
				
				PixelShader m_ps;
				
				/// Vertex format for the plane
				
				VertexFormat m_vf;
				
				/// The world transform uniform variable on the vertex shader
				
				ShaderUniform m_u_world_transform;

				/// The view/projection transform uniform variable on the vertex shader
				
				ShaderUniform m_u_view_projection_transform;
				
				/// The light position uniform variable on the pixel shader
				
				ShaderUniform m_u_light_position;

				/// The camera position uniform variable on the pixel shader
				
				ShaderUniform m_u_camera_position;
				
				/// Texture used to draw the plane
				
				Texture m_texture;
				
				/// True if all our graphics resources are ok
				
				bool m_valid_resources;

				/// This is the soft body used by the trampoline

				GridMeshSoftBody * m_body;
				
				/// This is a vertex array used for rendering
				
				Vertex * m_va;
				
				/// This is an index array used for rendering
				
				unsigned * m_ia;
				
				///	Number of squares in the trampoline mesh
				
				unsigned m_num_squares;	
				
				#ifdef USE_DEBUG_CONSOLE	
				
					/// True if to draw normals
					
					static bool s_draw_normals;
					
					/// Vertex format for drawing normals:
					
					VertexFormat m_vf_normals;
					
					/// Pixel shader for drawing normals:
					
					PixelShader m_ps_normals;

					/// Vertex shader for drawing normals:
					
					VertexShader m_vs_normals;
					
					/// Shader transform uniform for drawing normals:
					
					ShaderUniform m_u_normals_transform;
					
					/// Vertex array for drawing normals:
					
					NormalVertex * m_va_normals;
					
				#endif
	};
}

#endif
