#include "GunBoat.h" 
#include "GunBullet.h"  
#include "GunMissle.h"   
#include "PlayerShip.h"
#include "Smoke.h"
#include <windows.h>

int RandomNumber(int minvalue,int maxvalue);

extern CPlayerShip playerShip;
extern CGunBullet gunBullets;
extern CGunMissle gunMissles;
extern CSmoke smoke;
      
sVector directionFromAngles(sVector rotationVector);

CGunBoat::CGunBoat()
{
     sVector scale, rotation;
     
     scale.x = 10;
     scale.y = 10;
     scale.z = 10;
     
     rotation.x = 0;
     rotation.y = 180;
     rotation.z = 0;
     
     model.LoadObjFile("./models/GunBoat.obj");
     model.setScale(scale);
     model.setRotation(rotation);
}

CGunBoat::~CGunBoat()
{
}

void CGunBoat::update()
{
     
     if((RandomNumber(0, 1000) > 996) && generate)
     {
             sVector pos;
             pos.x = RandomNumber(-700, 700);
             pos.y = RandomNumber(150,300);;
             pos.z = 4000;
             
             addBoat(pos);
     }
     
     int numBoats = boatList.GetNumItems();
     
     for(int i = 0; i < numBoats; i++)
     {
             sBoat *boat = (sBoat *)boatList.GetItem(i);
             if(boat->state == HIT)
             {
                 boat->pos.z -= 10;
                 boat->pos.y -= boat->rotation.x;
                 boat->rotation.z += 0.8;
                 boat->rotation.x += 0.2;
                 boat->rotation.y += 0.2;
                 
                 sVector newSmokePos = boat->pos;
                 newSmokePos.x += RandomNumber(-50, 50);
                 newSmokePos.z += RandomNumber(-50, 50);
                 smoke.addSmoke(newSmokePos);
             }
             else
             {
                 boat->pos.z -= 10;  
                 
                 if(gunBullets.hitTest(boat->pos))
                 {
                          boat->state = HIT;                   
                 }  
                 
                 if(RandomNumber(0, 200) > 190)
                 {
                          gunMissles.addMissle(boat->pos);               
                 }
             } 
             
             sVector distance = playerShip.getPos().Difference(&boat->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 < 60)
                if(distance.y < 60)
                   if(distance.z < 60)
                       playerShip.Hit();

             
             if(boat->pos.z < -100 || boat->pos.y < 0)
             {
                      delete boat;
                      boatList.DeleteItem(i);
                      numBoats = boatList.GetNumItems();
                      i--;
             }
     }  
}

void CGunBoat::setGenerate(bool generate)
{
     this->generate = generate;
}

void CGunBoat::Render()
{
     int numBoats = boatList.GetNumItems();
     
     for(int i = 0; i < numBoats; i++)
     {
             sBoat *boat = (sBoat *)boatList.GetItem(i);
     
             model.setPos(boat->pos);
             model.setRotation(boat->rotation);
             model.Render();
     }
}

void CGunBoat::addBoat(sVector pos)
{
     if(boatList.GetNumItems() > 2000)
       return;
      
     sBoat *boat = new sBoat();
     if(boat == NULL)
      return;
      
     sVector rotation;
     rotation.x = 0;
     rotation.y = 180;
     rotation.z = 0; 
      
     boat->pos = pos;
     boat->rotation = rotation;
     boat->state = ALIVE;
     
     boatList.AppendItem(boat);
}
