#include "PlayerShip.h"
#include "GunBullet.h"
#include "GunMissle.h"
#include <gl/gl.h>

sVector directionFromAngles(sVector rotationVector);
int RandomNumber(int minvalue,int maxvalue);

extern CGunBullet gunBullets;
extern CGunMissle gunMissiles;

CPlayerShip::CPlayerShip()
{
   Reset();
}

CPlayerShip::~CPlayerShip()
{
}

void CPlayerShip::Load()
{
     sVector scale;
     sVector pos;
     
     pos.x = 0;
     pos.y = 30;
     pos.z = 20;
     
     scale.x = 2;
     scale.y = 2;
     scale.z = 2;
     
     model.LoadObjFile("./models/Arwing.obj");
     model.setScale(scale);
     model.setPos(pos);
}

void CPlayerShip::update()
{
     shipTimer -= 1;
     if(shipTimer < 0)
          shipTimer = 0;
     
     sVector pos = model.getPos();
     sVector rotation;
     
     if(status == ALIVE)
     {
         if(pos.x > 250)
              pos.x = 250;
         if(pos.x < -250)
              pos.x = -250;
         if(pos.y > 250)
              pos.y = 250;
         if(pos.y < 10)
              pos.y = 10;
              
         pos.x += acceleration.x;
         pos.y += acceleration.y;
         pos.z += acceleration.z;
         
         if(shipTimer > 0)
         {
              pos.x += RandomNumber(-4, 4);
              pos.y += RandomNumber(-4, 4);      
         }
         
         rotation.x = -acceleration.y * 6;
         rotation.y = acceleration.x * 6;
         rotation.z = -rotation.y * 0.6;
         model.setRotation(rotation);
         
         acceleration.x -= acceleration.x * 0.1;
         acceleration.y -= acceleration.y * 0.1;
         acceleration.z -= acceleration.z * 0.1;
     }
     else if(status == DYING)
     {
          rotation = model.getRotation();
          pos.y -= 1;
          rotation.z += 1;
          model.setRotation(rotation);
          
          if(pos.y < 0)
             status = DEAD;
     }
     
     model.setPos(pos);
}

void CPlayerShip::Accelerate(sVector accel)
{
     acceleration.x += accel.x;
     acceleration.y += accel.y;
     acceleration.z += accel.z;
}


void CPlayerShip::Render(float alpha)
{
     glColor4f((shipTimer * 0.1), 0.7, 0.7, alpha);
     if(status != DEAD)
          model.Render();
}

sVector CPlayerShip::getPos()
{
        return model.getPos();
}

sVector CPlayerShip::getDirection()
{
        sVector vector = model.getRotation();
        
        return directionFromAngles(vector);
}

void CPlayerShip::setPos(sVector pos)
{
     model.setPos(pos);
}

void CPlayerShip::Hit()
{
     if(shipTimer != 0)
         return;
               
     if(health >= 9.99)
         health -= 10;
               
     if(health <= 0.01)
         status = DYING; 

     shipTimer = 50;
}

float CPlayerShip::getHealth()
{
      return health;
}

int CPlayerShip::getStatus()
{
	return status;
}

// No description
void CPlayerShip::Reset()
{
   health = 100.0;
   status = ALIVE;
   shipTimer = 0;
   
   sVector pos;
   pos.x = 0;
   pos.y = 30;
   pos.z = 20;
   setPos(pos);
}
