#include "lineWaveModule.h"

//-------------------------------------------------
lineWaveModule::lineWaveModule(){
	ofSeedRandom();
	bgColor.set(255, 255, 255);
	img.loadImage("images/finger.png");
	
}

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

//-------------------------------------------------
void lineWaveModule::reset(){
	
	radius			= 120+rand()%80;
	framecount 		= 0;
	lineCount		= 0;
	count			= 0;
	pct				= 0;
	rotating		= true;
	startDegrees = degrees = 0;
	
	pos.set( radius, 240, 0); 
	
	currentPos = startPos = pos;
	
}

//-------------------------------------------------
void lineWaveModule::update(){
	
	if(!rotating && count > 10){
		rotating = true;
		count = 0;
	}
	
		
	if(degrees - startDegrees >= 180){
		startDegrees = degrees = 0;
		rotating = false;
		currentPos.x += radius;
		count = 0;
	}
	
	if(!rotating)count++;
	
	pos.x = currentPos.x + cos( PI - ( PI * (degrees/180) ) ) * radius;
	pos.y = currentPos.y + sin( PI - ( PI * (degrees/180) ) ) * radius;
		
	//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.x >= 640 || framecount >= MAX_LENGTH-1){
		pct = 1.1;	
		addPoint(pos);
		rotating = false;
	}else{
		
		if (framecount % 2 == 0) addPoint(pos);
		framecount++;

	}
		
	if(rotating)degrees+=4;	
	
}

//-------------------------------------------------
void lineWaveModule::draw(){
		
	ofSetColor(0x111122);
	ofEnableAlphaBlending();

	glPushMatrix();
		glTranslatef(currentPos.x, currentPos.y, 0);
		glRotatef(180 - degrees, 0, 0, 1); 
		ofRect(0, -8, radius, 16);
	glPopMatrix();
	
	ofDisableAlphaBlending();	
}

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

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

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