//	ParticleGenerator.cpp - Simple particle generator.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "ParticleGenerator.h"
#include "drawing/Drawer.h"
#include "MiscHelper.h"
#include "Random.h"

#include <cmath>

//------------------------------------------------------------------------------
ParticleGenerator::ParticleGenerator():
minSpeed(0.5f),
maxSpeed(6.0f),
colour(1.0f),
fadeColour(1.0f)
{

}

//------------------------------------------------------------------------------
ParticleGenerator::~ParticleGenerator()
{

}

//------------------------------------------------------------------------------
void ParticleGenerator::update()
{
	int i;

	for(i=(particles.size()-1);i>=0;--i)
	{
		particles[i].position += particles[i].direction * particles[i].speed;
		particles[i].direction += gravity;
		particles[i].direction = particles[i].direction.normal();
		particles[i].life -= (particles[i].speed/(maxSpeed*maxSpeed)) * 0.75f;
		if(particles[i].life <= 0.0f)
			particles.erase(particles.begin()+i);
	}
}

//------------------------------------------------------------------------------
void ParticleGenerator::draw(shared_ptr<Drawer>& d, float timestepInterp)
{
	unsigned int i;
	const TwoFloats size(0.5f);
	ThreeFloats col;

	for(i=0;i<particles.size();++i)
	{
		col = fadeColour + (particles[i].life * (colour-fadeColour));

		d->drawImage(L"Blob",
					 particles[i].position-(16.0f*size*particles[i].life),
					 size * particles[i].life,
					 col,
					 particles[i].life);
	}
}

//------------------------------------------------------------------------------
void ParticleGenerator::trigger(const TwoFloats& pos)
{
	int i;
	float dir;
	const int NumTriggered = 8;

	for(i=0;i<NumTriggered;++i)
	{
		particles.push_back(Particle());
		particles.back().position = pos;
		dir = Random::getGlobal().getRandomFloat() * 2.0f * NIALL_PI;
		particles.back().direction.x = sinf(dir);
		particles.back().direction.y = cosf(dir);
		particles.back().speed = minSpeed + (Random::getGlobal().getRandomFloat() * (maxSpeed-minSpeed));
		particles.back().life = 1.0f;
	}
}

//------------------------------------------------------------------------------
void ParticleGenerator::setGravity(const TwoFloats& val)
{
	gravity = val;
}

//------------------------------------------------------------------------------
void ParticleGenerator::setColour(const ThreeFloats& val)
{
	colour = val;
}

//------------------------------------------------------------------------------
void ParticleGenerator::setFadeColour(const ThreeFloats& val)
{
	fadeColour = val;
}
