/*
	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.
*/

//=====================================================================================================================
/// @brief		Base class for all game entities that can have both linear and angular velocity
//=====================================================================================================================

#include "Precompiled.h"
#include "Game\GameCore.h"
#include "GameObjects\MoveableObject.h"
#include "GameObjects\ObstacleManager.h"

using namespace Game;

//=====================================================================================================
/// @brief		Sets the linear friction per second for this object
/// @param		f - linear friction per second
/// @pre		friction value must be between 0 and 1
//=====================================================================================================

void MoveableObject::SetLinearFriction( float f )
{
	ENSURE( f >= 0 && f <= 1 , "friction value must be between 0 and 1" );

	//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	// Take a number of iterations to figure out a multiplier that will produce 
	// the given speed reduction in a second. Use binary refinement to figure this out.
	// This is a differential equation that would be tricky or impossible to solve exactly.
	//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

	// Save:

	m_lin_friction = f;
}

//=====================================================================================================
/// @brief		Gets the angular friction per second for this object
/// @param		f - linear friction per second
/// @pre		friction value must be between 0 and 1
//=====================================================================================================

void MoveableObject::SetAngularFriction( float f )
{
	ENSURE( f >= 0 && f <= 1 , "friction value must be between 0 and 1" );

	//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	// Take a number of iterations to figure out a multiplier that will produce 
	// the given speed reduction in a second. Use binary refinement to figure this out.
	// This is a differential equation that would be tricky or impossible to solve exactly.
	//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

	// Save:

	m_ang_friction = f;
}

//=====================================================================================================
/// @brief		Updates the moveable object and moves it along according to it's speed:
/// @param		time_delta - amount of time since the last frame
//=====================================================================================================

void MoveableObject::OnUpdate( float time_delta )
{
	// Move along by speed:

	SetPosition( GetPosition() + m_lin_vel * time_delta );

	// Adjust angular direction:

	SetAngle( GetAngle() + m_ang_vel * time_delta );

	// Do friction:

	m_lin_vel *= m_lin_friction;
	m_ang_vel *= m_ang_friction;
}

//=====================================================================================================================
/// @brief		Does bounding sphere collision with the obstacles present in the given obstacle manager
/// @param		m - obstacle manager who has the obstacles to collide with
/// @param		circle_radius - radius of the bounding circle for this object
/// @returns	True if there was a collision with the obstacles present in the given obstacle manager
/// @pre		Given obstacle manager must not be null
/// @pre		The game must exist
/// @pre		Given circle radius must be > 0
//=====================================================================================================================

bool MoveableObject::ObstacleCollide( ObstacleManager * m , float circle_radius )
{	
	// Safety checks:

	ENSURE( m != 0							, "Given obstacle manager must not be null" );
	ENSURE( GameCore::GetSingleton() != 0	, "The game must exist"						);
	ENSURE( circle_radius > 0				, "Given circle radius must be > 0"			);
	
	// Get the game:
	
	GameCore * game = GameCore::GetSingleton();
	
	// Get the two stacks from the game
	
	Stack & s1 = game->GetStack();
	Stack & s2 = game->GetSecondaryStack();
	
	// Save the current stack pointer:
	
	void * sp = s1.GetPointer();
	
	// Do the collision:
	
	unsigned num_results = m->ObstacleCollide( GetPosition() , circle_radius , s1 , s2 );
	
	// Restore the previous stack pointer:
	
	s1.SetPointer(sp);
	
	// Return true if there was a collision
	
	if ( num_results != 0 ) return true; else return false;
}
