#include "bouncerScene.h"


//---------------------------------------
bouncerScene::bouncerScene(){

}

//---------------------------------------
void bouncerScene::exit(){
	system("open ../../../data/quitAU.app");	
}

//---------------------------------------
void bouncerScene::setupScene(){
	
	nCards = 0;
	
	sceneName = "my good bouncer scene!";
	addAppListener();
	addMouseListener();
	addKeyboardListener();
	setAutoRender(true);
	
	bouncerRed.setup(camW, camH, 60, 1, 1);
	bouncerBlack.setup(camW, camH, 60, 5, 2);

	energyLevel = 0;
	energyTarget = 0;
}


//---------------------------------------
void bouncerScene::update(){
	if(pause)return;
	
	bouncerRed.drawWidth		= drawWidth;
	bouncerRed.drawHeight		= drawHeight;
	bouncerBlack.drawWidth		= drawWidth;
	bouncerBlack.drawHeight		= drawHeight;
	
	bouncerRed.theApp = theApp;
	bouncerBlack.theApp = theApp;
	
	
	redCards.clear();
	blackCards.clear();
	
	if (nCards != allCards.size()){
		energyLevel += 0.2f;
		energyLevel = MAX(energyLevel, 0.8f);
	}
	energyLevel = 0.985f * energyLevel + 0.015f * energyTarget;
	nCards = allCards.size();
	
	for(int i = 0; i < allCards.size(); i++){
		if( allCards[i].energySmoothed < 0.5 )continue;
		if(  allCards[i].bRedSuit ){
			redCards.push_back( allCards[i] );
		}else{
			blackCards.push_back( allCards[i] );
		}
	}
	
	
	//open up AU LAB to test the audio
	//need to open after OF hence this
	if( ofGetFrameNum() == 30 ){
		
		//these are for four channels 
		//each one panned differently
		//system("open ../../../data/basicMidi.trak");	
		system("open ../../../data/basicMidi2.trak");	

	}
	
	if( ofGetFrameNum() == 240 ){
		system("open ../../../data/hideAU.app");	
	}

	//tie the animation speed to the fps?
	//not such a good idea?
	float speed = (1.2 + energyLevel) * ( 1.0 / ofGetFrameRate());
	
	bouncerRed.update(redCards, speed, speed * 1.5);
	bouncerBlack.update(blackCards, speed, speed * 1.5);
	
}

//---------------------------------------
void bouncerScene::render(){
	if( hide ) return;
	
	bouncerRed.draw(planePtr);
	bouncerBlack.draw(planePtr);
	
	ofDisableAlphaBlending();

	renderGui();
}

//---------------------------------------
void bouncerScene::renderGui(){

	if(debug){
		//drawTxtMsg("scene is: "+sceneName, 0, camH - 40);
		//drawDebugTxt(0, camH - 20);	
	}
}

//---------------------------------------		
void bouncerScene::keyPressed( int key ){
	if(pause)return;

}

//---------------------------------------
void bouncerScene::keyReleased( int key ){
	if(pause)return;
	
}
	
//---------------------------------------
void bouncerScene::mouseMoved(int x, int y ){
	if(pause)return;
	
}

//---------------------------------------
void bouncerScene::mouseDragged( int x, int y, int button ){
	if(pause)return;

}

//---------------------------------------
void bouncerScene::mousePressed( int x, int y, int button ){
	if(pause)return;

}

//---------------------------------------
void bouncerScene::mouseReleased(int x, int y, int button ){
	if(pause)return;	
	
}	

