#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	
	
	ofSetVerticalSync(true);
	ofSetFrameRate(60);
	ofEnableAlphaBlending();
	
	landscapeSource.setImageType(OF_IMAGE_COLOR_ALPHA);
	landscapeSource.loadImage("landscape.gif");
	landscapeMap.loadImage("landscape_map.png");
	treasureChest.loadImage("chest.png");
	moneyBag.setImageType(OF_IMAGE_COLOR_ALPHA);
	moneyBag.loadImage("money.png");

	bigFont.loadFont("EMIGREFT.TTF",45,false,false);
	
		
	myParticle.setInitialCondition(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),0,0);
	myParticle.damping = .5f;
	drawVectors = false;
	drawDebug = false;
	
	target.set(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()));
	
	VF.setupField(60,40,ofGetWidth(), ofGetHeight());
	unsigned char * pixels = landscapeSource.getPixels();
	VF.setFromPixels(pixels,false,.6);
	
	score = 0;
	
}

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

	myParticle.resetForce();
	ofxVec2f frc;
	frc = VF.getForceFromPos(myParticle.pos.x, myParticle.pos.y);
	myParticle.addForce(frc.x, frc.y);
	myParticle.addDampingForce();
	myParticle.update();
	if(myParticle.checkTarget(target.x,target.y,15) == true){
			score ++;
			target.set(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()));
	}

	//VF.fadeField(0.99f);

}

//--------------------------------------------------------------
void testApp::draw(){
	
	ofFill();
	ofEnableAlphaBlending();
	//ofSetColor(0x0E7018);
	landscapeMap.draw(0,0,ofGetWidth(),ofGetHeight());
	ofSetColor(0xFFFFFF);
	
	if(drawVectors){
		ofSetColor(0,130,130, 200);
		VF.draw();
	}
	
	ofSetColor(0x000000);
	myParticle.draw();
					   
	ofSetColor(0xFFFFFF);
	treasureChest.draw(target.x-treasureChest.width/2,target.y-treasureChest.height/2);
	
	if(score == 0){
		ofSetColor(0xe2d71f);
		bigFont.drawString("Drive around collecting treasure-chests!",ofGetWidth()/2-350,ofGetHeight()/2);
		ofSetColor(0xFFFFFF);
	}
	
	for(int i = 0; i < score; i++){
		moneyBag.draw(i*33+10,10);
	}
								   
	
	if(drawDebug){
		myParticle.drawDebug();
	}
	
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
	if (key == ' '){
		VF.clear();
	}
	if(key == 'v'){
		if(drawVectors == false){
			drawVectors = true;
		} else {
			drawVectors = false;
		}
	} else if(key == 'd'){
		if(drawDebug == false){
			drawDebug = true;
		} else {
			drawDebug = false;
		}
	}
	if(key == 356){
		myParticle.addEngineForce(-.65,0);
	} else if(key == 357){
		myParticle.addEngineForce(0,-.65);
	} else if(key == 358){
		myParticle.addEngineForce(.65,0);
	} else if(key == 359){
		myParticle.addEngineForce(0,.65);
	}
}

//--------------------------------------------------------------
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;
	
	VF.addVectorCircle((float)x, (float)y, diffx*0.3, diffy*0.3, 60, 0.3f);
	
	prevMouseX = x;
	prevMouseY = y;
}

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

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
