#include "loopModule.h"

//-------------------------------------------------
loopModule::loopModule(){
	ofSeedRandom();
	bgColor.set(0,0,0);
	
}

//-------------------------------------------------	
void loopModule::setup(){
	reset();
}

//-------------------------------------------------
void loopModule::reset(){
		
	
	pct 			= 0;			
	framecount 		= 0;
	radius			= 8;	
	speed			= 0.0974;										//for both rotation and straight movement
	loopSize		= 20  + ofRandomuf()*60.0;						//radius of loop
	distToLoop		= 200 + ofRandomuf()*300.0;						//
	exitAngle		= 180 + 90*(int)(ofRandomuf()*1.0);				//calculate in degrees at what point we should exit loop
	startLoop		= false;										//
		
	vel.set( speed*70, 0, 0);										//speed*70 roughly equal to the speed of rotation
	pos.set( 0, 120 + rand()%240, 0);								//set our pos to somewhere along the left edge
	
	beginPos		= startPos	= pos;						
	counter			= offSet	= atan2f(vel.y, vel.x) - PI*0.5;	//the starting angle on the circle - so that curve starts at the tangent.


}

//-------------------------------------------------
void loopModule::update(){
	
	//record our trail
	trail[framecount] = pos;
	
	//we should begin our loop
	if( (pos-beginPos).length() >= distToLoop && !startLoop ){ 
		startLoop = true;
		beginPos  = pos;
	}
	
	//if looping
	if(startLoop){
		
		//if we have completed our loop
		if( 180.0*( (counter-offSet)/PI ) >= exitAngle){
			
			//calculate the exit velocity so we can continue in a straight line
			vel.x	= ( (beginPos.x - cos(offSet) * loopSize) + cos(counter) * loopSize ) - pos.x;
			vel.y	= ( (beginPos.y - sin(offSet) * loopSize) + sin(counter) * loopSize ) - pos.y;
			
			//calculate the angle of the tangent for the upcoming curve
			counter		= offSet = atan2f(vel.y, vel.x) - PI*0.5; 
			
			//reset our vars
			startLoop	= false;
			loopSize	= 20  + ofRandomuf()*60.0;
			distToLoop	= 50 + ofRandomuf()*200.0;
			beginPos	= pos;
			exitAngle	= 90 + (int)(ofRandomuf()*270.0);
		} 
		else{
			//we are still looping
			pos.x = (beginPos.x - cos(offSet) * loopSize) + cos(counter) * loopSize;
			pos.y = (beginPos.y - sin(offSet) * loopSize) + sin(counter) * loopSize;
		
			//incr our angle
			counter+= speed;
		}
		
	}
	
	//if we aren't looping lets keep drawing in a straight line
	if(!startLoop){
		pos.x += vel.x;
		pos.y += vel.y;
	}
	
		
	//detect when we have gone off the screen
	//also if we run out of points in our line array lets also finish
	//we don't want to crash
	if( 
	    (pos.y >= 480 + radius) || pos.y <= -radius || pos.x >= 640 + radius || pos.x <= -radius || framecount >= MAX_LENGTH-1){
		pct = 1.1;	
	}
	
	if (framecount % 5 == 0) addPoint(pos);
	framecount++;
	
}

//-------------------------------------------------
void loopModule::draw(){
	
	ofSetColor(0x33DD00);
	
	//draw trail
	glBegin(GL_LINE_STRIP);
	for(int i = 0; i < framecount; i++){
		glVertex2f(trail[i].x, trail[i].y);
	}
	glEnd();
	
	//draw particle
	ofCircle(pos.x, pos.y, radius);
}

//-------------------------------------------------
bool loopModule::bAmIdone(){
	return pct > 1 ? true : false;
}

//-------------------------------------------------
ofVec3f loopModule::getStartPosition(){
	
	return startPos;
}

//-------------------------------------------------
float loopModule::getStartAngle(){
	return 0;
}
		
		