#include "GunMissle.h"
#include "PlayerShip.h"
#include "Smoke.h"

extern CSmoke smoke;
extern CPlayerShip playerShip;

CGunMissle::CGunMissle()
{
      sVector scale;
      scale.x = -10;
      scale.y = -10;
      scale.z = -10;
      
      mesh.LoadObjFile("./models/Missle.obj");
      mesh.setScale(scale);
}

CGunMissle::~CGunMissle()
{
}

void CGunMissle::Render()
{
     int numMissles = missleList.GetNumItems();
     
     for(int i = 0; i < numMissles; i++)
     {
             sMissle *missle = (sMissle *)missleList.GetItem(i);
             mesh.setRotation(missle->direction);
             mesh.setPos(missle->pos);
             mesh.Render();
     }
}

void CGunMissle::update()
{
     int numMissles = missleList.GetNumItems();
     
     for(int i = 0; i < numMissles; i++)
     {
             sMissle *missle = (sMissle *)missleList.GetItem(i);
             
             missle->pos.x += missle->direction.x * missle->time * 0.9;
             missle->pos.y += missle->direction.y * missle->time * 0.9;
             missle->pos.z += missle->direction.z * missle->time * 0.9;  
             missle->time ++; 
             missle->smokeTime++;
             
             if(missle->smokeTime > 2)
             {
                 missle->smokeTime = 0;
                 smoke.addSmoke(missle->pos);
             }
             
             //check for hits with the player
             sVector distance = playerShip.getPos().Difference(&missle->pos);
             if(distance.x < 0)
                distance.x = -distance.x;
             if(distance.y < 0)
                distance.y = -distance.y;
             if(distance.z < 0)
                distance.z = -distance.z;
             if(distance.x < 30)
                if(distance.y < 30)
                   if(distance.z < 30)
                       playerShip.Hit();
                 
             
             if(missle->pos.z > 3000 || missle->pos.y < 0 || missle->pos.z < -100)
             {
                      delete missle;
                      missleList.DeleteItem(i);
                      numMissles = missleList.GetNumItems();
                      i--;
             }
     }     
}

bool CGunMissle::hitTest(sVector bound1, sVector bound2)
{
     int numMissles = missleList.GetNumItems();
     
     for(int i = 0; i < numMissles; i++)
     {
             sMissle *missle = (sMissle *)missleList.GetItem(i);
             
             if((bound1.x < missle->pos.x) && (bound2.x > missle->pos.x))
              if((bound1.y < missle->pos.y) && (bound2.y > missle->pos.y))
               if((bound1.z < missle->pos.z) && (bound2.z > missle->pos.z))
                return true;
     }
     
     return false;
}

void CGunMissle::addMissle(sVector pos)
{
     if(missleList.GetNumItems() > 2000)
       return;
       
     //prevent "cheap shots"  
     if(pos.z < 700)
       return;
      
     sMissle *missle = new sMissle();
     if(missle == NULL)
      return;
      
     missle->pos = pos;
     missle->direction = playerShip.getPos().Difference(&missle->pos);
     missle->direction.Normalize();
     missle->time = 0;
     missle->smokeTime = 0;
     
     missleList.AppendItem(missle);
}
