#include "Islands.h"

#include <windows.h>

int RandomNumber(int minvalue,int maxvalue);
      

CIsland::CIsland()
{
     sVector scale;
     
     scale.x = 80;
     scale.y = 80;
     scale.z = 80;
     
     model[0].LoadObjFile("./models/Island1.obj");
     model[0].setSmoothRender(true);
     model[0].setScale(scale);
}

CIsland::~CIsland()
{
}

void CIsland::update()
{
     
     if((RandomNumber(0, 1000) > 995) && generate)
     {
             sVector pos;
             pos.x = RandomNumber(-3000,3000);
             pos.y = 0;
             pos.z = 4400;
             
             addIsland(pos);
     }
     
     int numIslands = islandList.GetNumItems();
     
     for(int i = 0; i < numIslands; i++)
     {
             sIsland *island = (sIsland *)islandList.GetItem(i);
             
             island->pos.z -= 5;
             
             if(island->pos.z < -400 || island->pos.y < 0)
             {
                      delete island;
                      islandList.DeleteItem(i);
                      numIslands = islandList.GetNumItems();
                      i--;
             }
     }  
}

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

void CIsland::Render()
{
     int numIslands = islandList.GetNumItems();
     
     for(int i = 0; i < numIslands; i++)
     {
             sIsland *island = (sIsland *)islandList.GetItem(i);
     
             model[island->islandType].setPos(island->pos);
             model[island->islandType].setRotation(island->rotation);
             model[island->islandType].Render();
     }
}

void CIsland::addIsland(sVector pos)
{
     if(islandList.GetNumItems() > 2000)
       return;
      
     sIsland *island = new sIsland();
     if(island == NULL)
      return;
      
     sVector rotation;
     
     rotation.x = 0;
     rotation.y = RandomNumber(-180,180);
     rotation.z = 0; 
     island->pos = pos;
     island->rotation = rotation;
     
     island->islandType = 0;
          
     islandList.AppendItem(island);
}
