#include "SparkGenerator.h"
#include <math.h>
#include <stdlib.h>

#include "MemoryManager/debugNew.h"
using namespace ParticleSystem;

SparkGenerator::SparkGenerator(bool fatal, float radius)
{
	this->radius = radius;
	this->fatal = fatal;
	maxGeneration = 1000;
	this->generationDelay = 0.3f;
	this->previousTime = 0.0f;
	loop = true;
}

SparkGenerator::~SparkGenerator(void)
{
}

void SparkGenerator::generate(ParticleEngine::Particle * particle){
	if (canGenerate()){
		if (fatal){
			particle->pos[0] = cosf(rand());
			particle->pos[1] = pos[1];
			particle->pos[2] = cosf(rand());
			float length = sqrt(particle->pos[0]*particle->pos[0] + particle->pos[2]*particle->pos[2]);
			particle->pos[0]/= length;
			particle->pos[2]/= length;
			particle->pos[0]*= radius;
			particle->pos[2]*= radius;
			particle->pos[0]+= pos[0];
			particle->pos[2]+= pos[2];
			particle->direction[0] = 0;
			particle->direction[1] = fabs(cos((float)rand())*20);
			particle->direction[2] = 0;
			particle->life = 0;
			particle->maxLife = cos((float)rand())*2;
			particle->beginCouleur[0] = 1.0f;
			particle->beginCouleur[1] = 1.0f;
			particle->beginCouleur[2] = 1.0f;
			particle->beginCouleur[3] = 0.5f;
			particle->endCouleur[0] = 1.0f;
			particle->endCouleur[1] = 1.0f;
			particle->endCouleur[2] = 1.0f;
			particle->endCouleur[3] = 0.0f;
			particle->masse = 20;
			particle->size = cosf(rand()) + 1;
		}else{
			particle->pos[0] = pos[0];
			particle->pos[1] = pos[1];
			particle->pos[2] = pos[2];
			particle->direction[0] = cos((float)rand())*5;
			particle->direction[1] = fabs(cos((float)rand())*5);
			particle->direction[2] = cos((float)rand())*5;
			particle->life = 0;
			particle->maxLife = cos((float)rand());
			particle->beginCouleur[0] = 1.0f;
			particle->beginCouleur[1] = 1.0f;
			particle->beginCouleur[2] = 1.0f;
			particle->beginCouleur[3] = 0.5f;
			particle->endCouleur[0] = 1.0f;
			particle->endCouleur[1] = 1.0f;
			particle->endCouleur[2] = 1.0f;
			particle->endCouleur[3] = 0.0f;
			particle->masse = cos((float)rand())*10.0f;
			particle->size = cosf(rand()) + 1;
		}
		generation++;
	}
}

