#include "cSmokeTextParticle.h"

cSmokeTextParticle::cSmokeTextParticle(cParticleSystem* ps) : cParticle(ps) 
{
    uniqueness_ = rand()%1000;
    maxAge_ = 10000;
}

void cSmokeTextParticle::update(float dt)
{
    
    age_ += dt;
    if(age_ > maxAge_) {dead_ = true; return; }
    
    position_ += velocity_;
    
    cVector positionToGoal = goal_ - position_;
    velocity_ += positionToGoal * .003;
    
    float a = ((float)(rand()%100))/100.0f;
    float b = ((float)(rand()%100))/100.0f;
    float c = ((float)(rand()%100))/100.0f;
    //velocity_ += .001 * cVector(cos(age_ + uniqueness_ + sin(age_*3 + uniqueness_) ), cos(age_ + 324 + uniqueness_)*sin(age_*2.142 + 3522 + uniqueness_), cos(age_ + 32 + uniqueness_));
    velocity_ += .001 * cVector(a-.5, b-.5, c-.5);
    velocity_ *= .8;
    
    /*
    velocity_ *= .99;
    
    float a = ((float)(rand()%100))/100.0f;
    float b = ((float)(rand()%100))/100.0f;
    float c = ((float)(rand()%100))/100.0f;
    velocity_ += .01 * (age_/maxAge_) * (age_/maxAge_) * cVector(a-.5, b-.4, c-.5);
    */
}

    
void cSmokeTextParticle::draw()
{	
    float scale = size_ + .5 * size_ * cos(age_ + uniqueness_);

    glColor4f(r_,g_,b_, (1 - age_/maxAge_)  );
    
    glPushMatrix();
    
    glTranslatef(position_.x_, position_.y_, 0);
    
    glBegin(GL_TRIANGLE_STRIP);					// Build Quad From A Triangle Strip
        glTexCoord2d(1,1); glVertex3f(scale, scale, 0); 	// Top Right
        glTexCoord2d(0,1); glVertex3f(-scale, scale, 0); 	// Top Left
        glTexCoord2d(1,0); glVertex3f(scale, -scale, 0); 	// Bottom Right
        glTexCoord2d(0,0); glVertex3f(-scale, -scale, 0); 	// Bottom Left
    glEnd();
    
    glPopMatrix();
    
    /*
    // billboard
    float matrix[16];
    glPushMatrix();
    glTranslatef(position_.x_, position_.y_, position_.z_);
    glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
    matrix[0]=1.0; matrix[1] = 0.0; matrix[2] = 0.0;
    matrix[4]=0.0; matrix[5] = 1.0; matrix[6] = 0.0;
    matrix[8]=0.0; matrix[9] = 0.0; matrix[10] = 1.0;
    glLoadMatrixf(matrix);
    
    glBegin(GL_TRIANGLE_STRIP);					// Build Quad From A Triangle Strip
        glTexCoord2d(1,1); glVertex3f(scale, scale, 0); 	// Top Right
        glTexCoord2d(0,1); glVertex3f(-scale, scale, 0); 	// Top Left
        glTexCoord2d(1,0); glVertex3f(scale, -scale, 0); 	// Bottom Right
        glTexCoord2d(0,0); glVertex3f(-scale, -scale, 0); 	// Bottom Left
    glEnd();
    
    glPopMatrix();
    */
}


