#include "bouncerShape.h"

//-----------------------------------------------------------------------
bouncerShape::bouncerShape(){
	reset();

}
	
//-----------------------------------------------------------------------
bouncerShape::~bouncerShape(){


}

//-----------------------------------------------------------------------
void bouncerShape::setPosition(ofPoint newPos){
	pos = newPos;
	src = newPos;
	checkSrcDst();		
}

//-----------------------------------------------------------------------
void bouncerShape::setTarget(ofPoint target){
	dst = target;
	vec = (dst - src);
	vec.normalize();
	checkSrcDst();		
}

//-----------------------------------------------------------------------
void bouncerShape::setEnergy(float energyIn){
	energy = energyIn;
	energy = MIN(1, energy);
	energy = MAX(0, energy);
}

//-------------------------------------------
void bouncerShape::setDirection(ofPoint vecIn, float distance){
	ofxVec2f tmpVec;
	tmpVec = vecIn;
	tmpVec.normalize();
	
	vec = tmpVec;	
	//set the destination based on the energy 
	dst = src + (vec  * distance);
	
	reachedTarget = 1;
	pct		= 0.0;
}

//-----------------------------------------------------------------------
void bouncerShape::checkSrcDst(){
	if( src == 0 && dst == 0  ){
		pct				= 0.0;
		reachedTarget	= 0;
		pos				= 0;
	}
}

//-----------------------------------------------------------------------
void bouncerShape::begin(ofPoint srcIn, ofPoint dstIn){
	src = srcIn;
	pos = srcIn;
	dst = dstIn;
	pct = 0.0;
	reachedTarget = 1;
	vec = (dst - src);
	vec.normalize();
	checkSrcDst();	
}

//-----------------------------------------------------------------------
void bouncerShape::begin( ofPoint dstIn){
	dst = dstIn;
	pct = 0.0;
	reachedTarget = 1;
	vec = (dst - src);
	vec.normalize();
	checkSrcDst();			
}

//-----------------------------------------------------------------------
void bouncerShape::reset(){
	pct = 0.0;
	reachedTarget = 0;
	energy = 1.0;
	vec = 0.0;
}

//-----------------------------------------------------------------------
float bouncerShape::update(float speed, bool affectByDistance){
	
	
	
	if( reachedTarget == 2) return 1.0; 
	if( reachedTarget == 0) return 0.0; 

	//the amount of the energy should effect the speed
	if( affectByDistance ){
		float distance = dst.distance( src );
		distance /= 300;
		distance = MAX(0, distance);
		distance = MIN(1, distance);
		pct += speed * (0.75 +  0.75 * (1.0 - distance) );
	}
	else pct += speed;
	
	pct = MAX(0, pct);
	pct = MIN(1, pct);
	
	//good to have - go between 0 and 0 in a shaped way
	//also 1 to 1 in a shaped way
	sinPct = sin(pct * PI);
	cosPct = cos(pct * PI);
	
	hSinPct = sinPct * 0.5;		// 0 to 1
	hCosPct = cosPct * 0.5;		// 1 to 0	
	
	pos = dst * pct + (1.0 - pct)*src;
	
	if( pct >= 1.0 ){
		pct = 1.0;
		pos = src = dst;
		reachedTarget = 2;
		return pct;
	}else{
		reachedTarget = 1;
	}
	
	
	
}







