#include "Smoke.h" 
#include <gl/gl.h>
#include <gl/glu.h>

int RandomNumber(int minvalue,int maxvalue);

extern CPlayerShip playerShip;
extern CGunBullet gunBullets;
extern CGunMissle gunMissles;

extern int smokeTexture;
      
sVector directionFromAngles(sVector rotationVector);

// class constructor
CSmoke::CSmoke()
{
	// insert your code here
}

// class destructor
CSmoke::~CSmoke()
{
	while(smokeList.GetNumItems() != 0)
  {
     delete (sSmoke *)smokeList.GetItem(0);
     smokeList.DeleteItem(0);
  }
}

void CSmoke::addSmoke(sVector pos)
{
  if(smokeList.GetNumItems() > 2000)
     return;
     
	sSmoke *newSmoke = new sSmoke();
	
	newSmoke->pos = pos;
	newSmoke->timeToLive = 60;
	
	smokeList.AppendItem(newSmoke);
}

// No description
void CSmoke::update()
{
     int numSmoke = smokeList.GetNumItems();
     
     for(int i = 0; i < numSmoke; i++)
     {
             sSmoke *smoke = (sSmoke *)smokeList.GetItem(i);   
             smoke->pos.y += 2.0;
             smoke->pos.x += sin(smoke->timeToLive * 0.5f) * 2;
             smoke->pos.z += sin(smoke->timeToLive * 0.5f) * 2  - 5;
             smoke->timeToLive -= 1;
             
             if(smoke->timeToLive == 0)
             {
                  delete smoke;
                  smokeList.DeleteItem(i);
                  numSmoke = smokeList.GetNumItems();
                  i--;
         }
     }     
}

void CSmoke::Render()
{ 
     //prepare the texture
     glBindTexture (GL_TEXTURE_2D, smokeTexture);
     glDisable(GL_CULL_FACE);
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glDepthMask(false);
     
     //render the smoke
     int numSmoke = smokeList.GetNumItems();
     for(int i = 0; i < numSmoke; i++)
     {
            sSmoke *smoke = (sSmoke *)smokeList.GetItem(i);
            sVector pos = smoke->pos;
            sVector upVector(0,1,0);
            
            sVector zVector = pos.Difference(&playerShip.getPos());
            sVector xVector = zVector.CrossProduct(&upVector);
            sVector yVector = zVector.CrossProduct(&xVector);
            
            xVector.Normalize();
            yVector.Normalize();
            
            xVector.Scale(30);
            yVector.Scale(30);
            
            sVector top;
            sVector bottom;
            sVector left;
            sVector right;
            
            top = pos.Sum(&yVector);
            yVector.Scale(-1);
            bottom = pos.Sum(&yVector);
            
            left = pos.Sum(&xVector);
            xVector.Scale(-1);
            right = pos.Sum(&xVector);
            
            glColor4f(0.7,0.7,0.4, smoke->timeToLive * 0.015);
            glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex3f(top.x, top.y, top.z);
            glTexCoord2f(0,1);
            glVertex3f(right.x, right.y, right.z);
            
            glTexCoord2f(1,1);
            glVertex3f(bottom.x, bottom.y, bottom.z);
            glTexCoord2f(1,0);
            glVertex3f(left.x, left.y, left.z);  
            glEnd();
     }   
     
     //disable the texture
     glBindTexture (GL_TEXTURE_2D, 0);
     glEnable(GL_CULL_FACE);
     glDisable(GL_BLEND);
     glDepthMask(true);
}
