#include <iostream>
#define GLUT_DISABLE_ATEXIT_HACK 
#include "constants.h"
#include "vector.h"
#include "bullet.h"
#include <GL/glut.h>


using namespace std;

/*
 * Implement the Sphere class
 */

/*
 * Constructors
 */

// Default constructor
Bullet::Bullet(void):acceleration(0,GRAVITY,0),
					 color(COLORR, COLORG, COLORB),
					 radius(BULLET_RADIUS)
{
}

// Constructor sets location, velocity
Bullet::Bullet(const Vector& _loc, const Vector& _vel)
	:loc(_loc),
	 vel(_vel),
	 acceleration(0,GRAVITY,0),
	 life(BULLET_LIFE),
	 color(COLORR, COLORG, COLORB),
	 radius(BULLET_RADIUS)
{}

Bullet::~Bullet(void)
{}

void Bullet::draw(void)
{
	glColor3f(color[0], color[1], color[2]);  // establish color in RGB
	glPushMatrix();
	glTranslatef(loc[0], loc[1], loc[2]); // move center
	glutSolidSphere(radius, RESOLUTION, RESOLUTION);
	glPopMatrix(); //end sphere draw
}

/*
 * Collision detection
 */

Vector Bullet::getNextLoc(double time) const
{
	double nextx = loc[0] + vel[0] * time;
	double nexty = loc[1] + vel[1] * time;
	double nextz = loc[2] + vel[2] * time;

	return Vector(nextx, nexty, nextz);
}

// Given a triangle's transformation matrix
// and a Vector, transform the vector by the
// triangle's transformation matrix
Vector Bullet::transformPosition(float* tmatrix) const
{
	Vector retval;
	// compute kth vertex
	for (int i=0;i<3;i++) {
		retval[i] = 0;
		for (int j=0;j<3;j++)
			retval[i] += tmatrix[4*j+i]*loc[j];
		retval[i] += tmatrix[12+i];
	}

	return retval;
}

Vector Bullet::transformVelocity(float* tmatrix) const
{
	Vector retval;
	// compute kth vertex
	for (int i=0;i<3;i++) {
		retval[i] = 0;
		for (int j=0;j<3;j++)
			retval[i] += tmatrix[4*j+i]*vel[j];
	}

	return retval;
}

/*
 * Time Step Movement
 */
Vector Bullet::move(double time)
{
	// Change the ball.
	loc[0] += vel[0] * time;
	loc[1] += vel[1] * time;
	loc[2] += vel[2] * time;

	// Accelerate the ball.
	vel[0] += acceleration[0] * time;
	vel[1] += acceleration[1] * time;
	vel[2] += acceleration[2] * time;

	return loc;
}

/*
 * Getters
 */
Vector Bullet::getLoc() const
{
	return loc;
}

Vector Bullet::getVel() const
{
	return vel;
}



/*
 * Setters
 */
void Bullet::setLoc(const Vector& newloc)
{
	loc = newloc;
}

void Bullet::setVel(const Vector& newvel)
{
	vel = newvel;
}

void Bullet::setColor(const Vector& col)
{
	color = col;
}
/*
 * Bullet Life functions
 */ 
int Bullet::getLife() const
{
	return life;
}

void Bullet::decrementLife()
{
	--life;
}