#include "main.h"

particle::particle() {
	this->ticks = SDL_GetTicks();
	this->x = 0;
	this->y = 0;
	this->speedx = 0;
	this->speedy = 0;
	this->mass = 20;
	if (rand()%2 == 1) {
		this->gravity = GRAVITY;
	} else {
		this->gravity = ANTIGRAVITY;
	}
}

particle::~particle() {
	
}
	
void particle::reset(screen *scr) {
	switch (rand()%4) {
		case 0: //top
			this->y = scr->top;
			this->x = rand()%((int)(scr->right - scr->left));
			break;
		case 1: //bottom
			this->y = scr->bottom;
			this->x = rand()%((int)(scr->right - scr->left));
			break;
		case 2: //left
			this->x = scr->left;
			this->y = rand()%((int)(scr->bottom - scr->top));
			break;
		case 3: //right
			this->x = scr->right;
			this->y = rand()%((int)(scr->bottom - scr->top));
			break;
		default:
			break;
	}
/*	this->x = rand()%((int)(scr->bottom - scr->top));
	this->y = rand()%((int)(scr->right - scr->left));*/
	this->speedx = 0;
	this->speedy = 0;
	switch(rand()%7) {
		case 0: //red
			this->red = 1;
			this->green = 0;
			this->blue = 0;
			break;
		case 1: //green
			this->red = 0;
			this->green = 1;
			this->blue = 0;
			break;
		case 2: //blue
			this->red = 0;
			this->green = 0;
			this->blue = 1;
			break;
		case 3: //yellow
			this->red = 1;
			this->green = 1;
			this->blue = 0;
			break;
		case 4: //magenta
			this->red = 1;
			this->green = 0;
			this->blue = 1;
			break;
		case 5: //cyan
			this->red = 0;
			this->green = 1;
			this->blue = 1;
			break;
		case 6: //white
			this->red = 1;
			this->green = 1;
			this->blue = 1;
			break;
		default:
			break;
	}
	if (rand()%2 == 1) {
		this->gravity = GRAVITY;
// 		this->red = 0;
// 		this->green = 1;
// 		this->blue = 0;
	} else {
		this->gravity = ANTIGRAVITY;
// 		this->red = 0;
// 		this->green = 0;
// 		this-> blue = 1;
	}
}
	
void particle::draw(GLuint texture) {
	glPushMatrix(); //begin drawing the ship

	/* Rotate. */
	glTranslatef(this->x, this->y, 0.0); 
	glRotatef(atan2(this->speedy, this->speedx)*180/M_PI + 90,0,0,1);

	glBindTexture(GL_TEXTURE_2D, texture);

	glEnable(GL_BLEND);
	glBegin(GL_QUADS);
	glColor4f(this->red, this->green, this->blue, 0.3f);
	glTexCoord2i(0, 0);
	glVertex3f((-1)*this->width/2, (-1)*this->height/2, 0);

	glTexCoord2i(1, 0);
	glVertex3f(this->width/2, (-1)*this->height/2, 0);

	glTexCoord2i(1, 1);
	glVertex3f(this->width/2, this->height/2, 0);

	glTexCoord2i(0, 1);
	glVertex3f((-1)*this->width/2, this->height/2, 0);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	glEnd();

	glPopMatrix();
}	
	
	
void particle::move(vector<planet*> &planetlist, screen *scr) {
	GLuint tmp = SDL_GetTicks();
	float theta;
	float distance;
	
	for (unsigned int i = 0; i < planetlist.size(); i++) {
		if (planetlist[i]->mass == 0)
			continue;
		theta = atan2(this->y - planetlist[i]->y, 
			      this->x - planetlist[i]->x);
		distance = sqrt(pow(this->x - planetlist[i]->x, 2) +
				pow(this->y - planetlist[i]->y, 2));
		if (distance < planetlist[i]->width/2 + this->width/2) {
			this->reset(scr);
			this->move(planetlist, scr);
			return;
		}
		if (planetlist[i]->gravity == this->gravity) {
			this->speedx -= (planetlist[i]->mass/(this->mass*distance)) * 
					GRAVITYCONSTANT * cos(theta) * 
					(TIMESTEP)/1000;
			this->speedy -= (planetlist[i]->mass/(this->mass*distance)) *
					GRAVITYCONSTANT * sin(theta) * 
					(TIMESTEP)/1000;
		} else {
			this->speedx += (planetlist[i]->mass/(this->mass*distance)) * 
					GRAVITYCONSTANT * cos(theta) * 
					(TIMESTEP)/1000;
			this->speedy += (planetlist[i]->mass/(this->mass*distance)) *
					GRAVITYCONSTANT * sin(theta) * 
					(TIMESTEP)/1000;
		}
	}
		
	this->x += this->speedx * (TIMESTEP)/1000;
	this->y += this->speedy * (TIMESTEP)/1000;


	if (this->x < scr->left) {
		this->reset(scr);
		this->move(planetlist, scr);
	}
	if (this->x > scr->right) {
		this->reset(scr);
		this->move(planetlist, scr);
	}
	if (this->y < scr->top) {
		this->reset(scr);
		this->move(planetlist, scr);
	}
	if (this->y > scr->bottom) {
		this->reset(scr);
		this->move(planetlist, scr);
	}
	
	this->ticks = tmp;
}

