#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	
	
	ofSetVerticalSync(true);
	ofSetFrameRate(60);
	ofBackground(0, 0, 0);
	flag=false;
	
	for (int i = 0; i < 2000; i++){
		particle myParticle;
		myParticle.setInitialCondition(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),0,0);
		particles.push_back(myParticle);
		r[i]=ofRandom(100, 255);
		g[i]=ofRandom(200, 255);
		b[i]=ofRandom(200, 255);
	}
	
	VF.setupField(60,40,ofGetWidth(), ofGetHeight());
	
}

//--------------------------------------------------------------
void testApp::update(){

	// on every frame 
	// we reset the forces
	// add in any forces on the particle
	// perfom damping and
	// then update
	
	
	for (int i = 0; i < particles.size(); i++){
		particles[i].resetForce();
		
		// get the force from the vector field: 
		ofxVec2f frc;
		frc = VF.getForceFromPos(particles[i].pos.x, particles[i].pos.y);
		particles[i].addForce(frc.x, frc.y);
		particles[i].addDampingForce();
		particles[i].update();
	
	}
	
	//VF.fadeField(1);

}

//--------------------------------------------------------------
void testApp::draw(){
	
	ofEnableAlphaBlending();
	ofSetColor(0,130,130, 200);
	//VF.draw();
	
	//ofSetColor(0x000000);
	
	for (int i = 0; i < particles.size(); i++){
		
			ofSetColor(r[i],g[i],b[i]);
		
		particles[i].draw();
	}
	if(flag)
	{
		for (int i = 0; i < particles.size(); i++){
			particles[i].setInitialCondition(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()), 0,0);
			
			
		}
		VF.setupField(60,40,ofGetWidth(), ofGetHeight());

		flag=false;
	}
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
	if (key == ' '){
		VF.clear();
	} 
}

//--------------------------------------------------------------
void testApp::keyReleased  (int key){ 
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
	float diffx = x - prevMouseX;
	float diffy = y - prevMouseY;
	//if drawn fast you fail
	//if(diffx >20){
	//	VF.addVectorCircle((float)x, (float)y, diffx*10, diffy*10, 60, 0.8f);
		
	//}
	//VF.addVectorCircle((float)x, (float)y, diffx*0.3, diffy*0.3, 60, 0.3f);
	VF.addVectorCircle((float)x, (float)y,diffx, (diffy), 60, 0.1f);

	prevMouseX = x;
	prevMouseY = y;
	
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	prevMouseX = x; 
	prevMouseY = y;
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	int i=int(ofGetElapsedTimef());
		i=i+int(ofGetElapsedTimef());
	printf("%i \n",i);

	if(i%50==0 || i%50==1||i%50==2||i%50==3||i%50==4)
	{
		
		flag=true;
		i=0;
		}
}
