/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function Name: CheckDynamicCollisions()
// Purpose: Check collisions with all dynamic objects
// Original Author: Rueben Massey
// Creation Date: 6/4/2012
// Last Modification By: 
// Last Modification Date: 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CComponent_PlayerCollision::CheckDynamicCollisions( CComponent_Collision* pCurCollision, CCollisionVolume* pCurVolume, float fDT )
{
	if( m_pParent->GetWorldPos().x > 5000 )
	{
		// Do stuff
		int x = 50;
	}
	vec2f Direction = vec2f(0.0f, 0.0f);
	vec3f CP = vec3f(0.0f, 0.0f, 0.0f);

	D3DXMATRIX mat;
	D3DXMatrixIdentity (&mat);

	if( pCurCollision && pCurCollision != this )
	{
		switch( pCurVolume->GetVolumeType() )
		{
		//case VMT_AABB:
		//	{
		//		if(pCurCollision->GetParent()->GetType() == OBJ_PIT)
		//		{
		//			CComponent_Sound* pPitSound = (CComponent_Sound*)pCurCollision->GetParent()->GetComponent(ECOMP_SOUND);
		//			if(!pPitSound->GetClosestGooPit())
		//			{
		//				pPitSound->SetClosestGooPit(m_pParent);
		//			}
		//			else
		//			{
		//				vec3f lineToGooPit = pPitSound->GetAudio()->GetListenerPosition() - m_pParent->GetWorldPos();
		//				vec3f lineToClosestGooPit = pPitSound->GetAudio()->GetListenerPosition() - pPitSound->GetClosestGooPit()->GetWorldPos();
		//				float distToGoo = dot_product(lineToGooPit, lineToGooPit);
		//				float distToClosestGoo = dot_product(lineToClosestGooPit, lineToClosestGooPit);
		//
		//				if(distToGoo < distToClosestGoo)
		//				{
 		//					pPitSound->SetClosestGooPit(pCurCollision->GetParent());
		//
		//					char message[128];
		//					sprintf(message, "Closest Goo Pit X: %f		Y: %f", pPitSound->GetClosestGooPit()->GetWorldPos().x, pPitSound->GetClosestGooPit()->GetWorldPos().y);
		//					LOG(LOG_DEBUG, "CComponent_GooPitCollision::DynamicCollision()", message);
		//				}
		//			}
		//		}
		//	}
		//	break;
		case VMT_BOX:
			{	
				//check the item component target to see if the box is being pulled.
				CComponent_Item* boxItemCmp = (CComponent_Item*)pCurCollision->GetParent()->GetComponent(ECOMP_ITEM);
				if(boxItemCmp)
				{
					if(boxItemCmp->GetTarget() == m_pParent || boxItemCmp->GetAttached())
					{
						//we are pulling the box so don't check collision with it
						return;
					}
				}

				switch( pCurCollision->GetParent()->GetType() )
				{
				case OBJ_GOO:
					{
						return;
					}
				case OBJ_PLUG:
					{
						if( ((CComponent_PlugCollision*)pCurCollision)->GetPlugged() )
						{
							return;
						}
						break;
					}
				case OBJ_CORK:
					{
						if( ((CComponent_CorkCollision*)pCurCollision)->GetCorked() )
						{
							return;
						}
						break;
					}
				};

				// make sure there is a collision component and that it is not us, also make sure that the object we 
				// are checking against is a box.
				// get a pointer to that box
				Box* crate = (Box*)( pCurCollision->GetParent()->GetCollidableObject() );

				CComponent_Item* pCurItem = (CComponent_Item*)pCurCollision->GetParent()->GetComponent( ECOMP_ITEM );
				if( pCurItem )
				{
					if( pCurItem->GetTarget() )
						return;
				}

				// check the players aabb against the crate or cork
				if (crate->BoxToAABB ( *m_pPlayerCollisionVolume, vec2f( CP.x, CP.y ) , Direction))
				{
					m_fLastOffset = vec3f( Direction.x, Direction.y, 0.0f );
					vec3f MoveDirection = vec3f (Direction.x, Direction.y, 0.0f).normalize();

					float fTimePercentage = 1.0f - ( pCurCollision->GetTimeLeft() / 0.5f );
					if( fTimePercentage > 1.0f ) fTimePercentage = 1.0f;
					if( fTimePercentage < 0.0f ) fTimePercentage = 0.0f;

					float fDirection = dot_product( MoveDirection, vec3f( 0.0f, -1.0f, 0.0f ) );
					if( fDirection > 0.5f )
					{
						CleanOffset( vec2f( Direction.x, Direction.y ) * -1.0f * fTimePercentage );
						if( !( pCurCollision->GetTimeLeft() > 0.0f ) )
							CleanForce( vec2f( Direction.x, Direction.y ) * -1.0f );
						if( pCurCollision->CollidingGround() )
							m_bCollidingGround = true;
					}
					else if( fDirection < -0.5f )
					{
						float fSided = pCurCollision->GetPos2DClean().x - GetPos2DClean().x;
						// Normalize the float.
						if( fSided > 0.0f )
							fSided = 1.0f;
						else
							fSided = -1.0f;

						pCurCollision->CleanOffset( vec2f( Direction.x + fSided * 4.0f, Direction.y ) * 0.75f * fTimePercentage );
						pCurCollision->CleanForce( vec2f( Direction.x + fSided * 2.0f, Direction.y ) * 0.25f * fTimePercentage );
					}
					else
					{
						pCurCollision->CleanOffset( vec2f( Direction.x, Direction.y ) * 0.5f * fTimePercentage );
						pCurCollision->CleanForce( vec2f( Direction.x, Direction.y ) * 0.5f * fTimePercentage );
						CleanOffset( vec2f( Direction.x, Direction.y ) * -0.5f * fTimePercentage );
						CleanForce( vec2f( Direction.x, Direction.y ) * -0.5f * fTimePercentage );
					}
				}
				break;
			}
		case VMT_SPHERE:
			{
				break;
			}
		default:
			break;
		};

	}
	if( m_pParent->GetWorldPos().x > 5000 )
	{
		// Do stuff
		int x = 50;
	}
}