#include "lineWalkModule.h"

//-------------------------------------------------
lineWalkModule::lineWalkModule(){
	ofSeedRandom();
	bgColor.set(255, 255, 255);
	img.loadImage("images/finger.png");	
	//SND.loadSound("sounds/finger.aiff");
	//SND.setMultiPlay(true);
}

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

//-------------------------------------------------
void lineWalkModule::reset(){
	
	framecount 		= 0;
	lineCount		= 0;
	pct				= 0;
	

	angle1 =  30 +  rand()%10;
	angle2 = -30 - (rand()%15) ;	
	
	for(int i = 0; i < MAX_LENGTH; i++){
		startDegrees[i]	= degrees[i] = 0;
		rotating[i] = 0;
		pause[i] = false;
		rotating[i] = true;

		radius[i] = 170+rand()%40;

		count[i] = 0;
		pos[i].set( 2, 280, 0);
		leadingPoint = currentPos[i]  = startPos[i] = pos[i];
	}
		
				
}

//-------------------------------------------------
void lineWalkModule::update(){
	
	
	
	if( (framecount + lineCount)%53 == 0){
		lineCount++;
	}
	
	for(int i = 0; i < lineCount; i++){
		
		if( count[i] > 5){
			count[i] = 0;
			pause[i] = false;
			
		}
		
		if( (degrees[i] - startDegrees[i]) >= angle1 || (degrees[i] - startDegrees[i]) <= angle2){
			startDegrees[i] = degrees[i];
			rotating[i] = !rotating[i];
			pause[i] = true;
			//if(!rotating[i])SND.play();
		}
		
		if(!pause[i]){
			
			if(rotating[i]){
				
				pos[i].x = currentPos[i].x + cos(  ( PI * ((degrees[i]-90)/180) ) ) * radius[i];
				pos[i].y = currentPos[i].y + sin(  ( PI * ((degrees[i]-90)/180) ) ) * radius[i];
				
			}else{
				
				currentPos[i].x = pos[i].x + cos(  ( PI * ((degrees[i]+90)/180) ) ) * radius[i];
				currentPos[i].y = pos[i].y + sin(  ( PI * ((degrees[i]+90)/180) ) ) * radius[i];
				
			}
			
			if(rotating[i])degrees[i]+= 1;	
			else degrees[i]-= 1;
		
		}
	
		//if(leadingPoint.x < pos[i].x) leadingPoint = pos[i]; 
		//if(leadingPoint.x < currentPos[i].x) leadingPoint = currentPos[i]; 
						
		if(pause[i])count[i]++;
		
		//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(rotating[i]){ 		 
		   if( pos[i].x > 640 || pos[i].y > 480 || pos[i].y < 0 || framecount >= MAX_LENGTH-1){
				pct = 1.1;	
				addPoint(pos[i]);
				printf("x %f y %f \n", pos[i].x, pos[i].y);
				return;	
			}
		}else{
			if( currentPos[i].x > 640 || currentPos[i].y > 480 || currentPos[i].y < 0 || framecount >= MAX_LENGTH-1){
				pct = 1.1;	
				addPoint(currentPos[i]);
				printf("x %f y %f \n", currentPos[i].x, currentPos[i].y);
				return;
			}
		}		
		
	}
	
	if(rotating[0])leadingPoint = pos[0];
	else leadingPoint = currentPos[0];
			
	if (framecount % 2 == 0) addPoint(leadingPoint);
	framecount++;

}

//-------------------------------------------------
void lineWalkModule::draw(){
		
	ofSetColor(0xFFFFFF);

	ofEnableAlphaBlending();
	
	for(int i = 0; i < lineCount; i++){
		glPushMatrix();
			if(rotating[i]){
				glTranslatef(currentPos[i].x, currentPos[i].y, 0);
				glRotatef(180+degrees[i], 0, 0, 1); 
				img.draw(32, radius[i], -64, -radius[i]);
			}
			else{
				glTranslatef(pos[i].x, pos[i].y, 0);
				glRotatef(degrees[i], 0, 0, 1); 
				img.draw(-32, 0, 64, radius[i]);			
			}
			//ofRect(-8, 0, 16, radius[i]);
		glPopMatrix();
	}	
	
	ofDisableAlphaBlending();

	
}

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

//-------------------------------------------------
ofVec3f lineWalkModule::getStartPosition(){
	
	return ofVec3f(0, 240, 0);
}

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