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

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

#include "GameObjects\MoveableObject.h"
#include "GameObjects\DamageableObject.h"
#include "Game\PlayerBulletManager.h"

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

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

	class Camera;
	class PlayerBullet;
	class PlayerBulletManager;
	class ObstacleManager;

	//=================================================================================================================
	/// @brief		Represents the player's ship.
	//=================================================================================================================

	class PlayerShip : public MoveableObject , public DamageableObject
	{
		public:

			//=========================================================================================================
			// Constants
			//=========================================================================================================

				/// Amount of acceleration the player ship has

				static const float LIN_ACCELERATION;
				
				/// Amount of acceleration from the directional thrusters
				
				static const float DIRECTIONAL_THRUST_LIN_ACCELERATION;

				/// Amount of angular acceleration the player ship has

				static const float ANG_ACCELERATION;

				/// Size of the player ship

				static const Vec2f SIZE;

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

				/// Creates a player ship
				
				PlayerShip
				( 
					Vec2f lin_vel	= Vec2f::Zero()	,
					float ang_vel	= 0				,
					Vec2f pos		= Vec2f::Zero() ,
					float ang		= 0				
				);

				/// Destroys the player ship:

				virtual ~PlayerShip();

				/// Draw function. Draws the player ship

				virtual void OnDraw( Camera2D * c );

				/// Ship controls and that

				virtual void OnUpdate( float time_delta );
				
				/// Called when the player ship is removed from the game
				
				virtual void OnGameRemoved();

				/// Returns the type name for this object

				virtual const char * GetTypeName() const;
				
				/// Returns the cached reference this object has to the obstacle manager
				
				inline ObstacleManager * GetObstacleManager(){ return m_obstacle_mgr; }
				
		private:
		
			//=========================================================================================================
			// Constants
			//=========================================================================================================
							
				/// Maximum number of bullets the player can fire at any one time
				
				static const unsigned MAX_BULLETS;
				
				/// Color of the player's bullets
				
				static const Color BULLET_COLOR;
				
				/// Length of the player's bullets
				
				static const float BULLET_LENGTH;
				
				/// Interval between when the player can fire bullets
				
				static const float BULLET_FIRE_INTERVAL;
				
				/// How quickly to smooth the ships current direction into the movement direction when using directional thrust: higher values mean more smoothing
				
				static const float DIRECTIONAL_THRUST_TURN_SMOOTH;
				
				/// Points for the player's ship

				static const Vec3f SHAPE_POINTS[];
				
				/// Hexidecimal color of the ship
				
				static const unsigned SHIP_COLOR;
				
				/// How quickly the ship turns to face the ships current velocity from 0-1
				
				static const float TURN_SMOOTHING;
				
				/// Amount of smoothing for gamepad input from 0-1
				
				static const float GAMEPAD_SMOOTHING;
				
				/// Amount of smoothing for keyboard input from 0-1
				
				static const float KEYBOARD_SMOOTHING;

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Does controls for the player
				
				void DoControls( float time_delta );
			
				/// Returns true if the player has the buttons pressed for using directional thrusters instead of normal thrusters
				
				bool UsingDirectionalThrusters( const Gamepad::GamepadState & s ) const;
				
				//=====================================================================================================
				/// @brief		Fires one bullet if possible
				/// @param		dir - direction to fire in
				/// @pre		Given direction must be a valid float
				//=====================================================================================================
				
				inline void Fire( const Vec2f & dir )
				{
					// Make sure ok:
					
					ENSURE( Float::IsValid(dir.x) , "Given direction must be a valid float" )
					ENSURE( Float::IsValid(dir.y) , "Given direction must be a valid float" )
				
					// See if we can fire: 
					
					if ( m_time_till_bullet <= 0 )
					{
						// Fire:
						
						m_bullet_mgr->FireBullet( GetPosition() , dir.GetAngle() , GetLinearVelocity().Length() );
						
						// Reset time till the next bullet 
						
						m_time_till_bullet = BULLET_FIRE_INTERVAL;
					}						
				}
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================
				
				/// Manager for our bullets
				
				PlayerBulletManager * m_bullet_mgr;
				
				/// Reference to the obstacle manager for the game. Cached locally here.
				
				ObstacleManager * m_obstacle_mgr;
				
				/// Time until when the next bullet can be fired
				
				float m_time_till_bullet;	
				
				/// Current movement direction from the controller. Values are smoothed and kept in -1 to 1 range.
				
				Vec2f m_ctrl_move_dir;
				
				/// Current fire direction from the controller. Values are smoothed and kept in -1 to 1 range. If length > 0.5 then the ship fires.
				
				Vec2f m_ctrl_fire_dir;
	};
};

#endif
