#include "fallingLeaf.h"

fallingLeaf::fallingLeaf(){
	
	leaf_img.loadImage("leaf.gif");
	float scale = ofRandom(0,1);
	image_w = leaf_img.width * scale;
	image_h = leaf_img.height * scale;
	
	start.x = ofRandom(0,ofGetWidth());
	start.y = ofRandom(0,-20);
	pos.x = ofRandom(0,ofGetWidth());
	pos.y = ofRandom(0,-20);
	
	falling_rate = ofRandom(0,0.35);
	
	angle = 0;
	min_angle = ofRandom(0,PI);
	max_angle = min_angle + PI;
	next_angle = min_angle;
	floating_rate = ofRandom(0,0.001);
	
	float_width = ofRandom(50,200);
}

void fallingLeaf::update(){
	angle = floating_rate * next_angle + (1-floating_rate) * angle;
	if(angle < min_angle){
		next_angle = max_angle+5;
	} else if(angle > max_angle){
		next_angle = min_angle-5;
	}
	cout << angle << endl;
	pos.y += falling_rate;
	pos.x = start.x + float_width*sin(angle);
}

bool fallingLeaf::isOnGround(){
	if(pos.y > ofGetHeight()){
		return true;
	} else {
		return false;
	}
}

void fallingLeaf::draw(){
	ofSetColor(255,255,255);
	glPushMatrix();
	glTranslatef(pos.x,pos.y,0);
	glRotatef((angle-PI)*20,0,0,.02);
		leaf_img.draw(0,0,image_w,image_h);
	//ofCircle(0,0,5);
	glPopMatrix();
		
}
