package simogl.effect.particle.custom;

import java.io.File;

import javax.media.opengl.GL;

import simogl.effect.particle.AbstractParticleEffect;
import simogl.effect.particle.DLParticle;
import simogl.geometry.Point3D;

public class Fire extends AbstractParticleEffect
{

	/**
	 * Creates a fire effect with the specified parameters
	 * @param x x-coordinate
	 * @param y y-coordinate
	 * @param z z-coordinate
	 * @param numberOfParticles maximum number of particles to use
	 * @param maxPartAge maximum particle age (in miliseconds)
	 * @param creationIntervall intervall between the generation of two particles (in miliseconds)
	 */
	public Fire(float x, float y, float z, int numberOfParticles, long maxPartAge, long creationIntervall)
	{
		super(x,y,z,numberOfParticles,maxPartAge,creationIntervall);
		this.loadTexture(new File("./resources/textures/particle/fire.jpg"));
		this.maxPartAge = maxPartAge;
	}
	
	/**
	 * Helper method to continuously fill the particle container with new
	 * particles, if it is not full
	 *
	 */
	@Override
	protected synchronized void generateParticles(GL gl,long elapsedTime)
	{		
		this.cumulatedTime += elapsedTime;
		
		if(this.particleContainer.getParticleCount() < this.particleContainer.getMaximumParticles()
				&& this.creationIntervall <= this.cumulatedTime)
		{
			// find some ramdom velocity values
			float velX = this.random.nextInt(100)/80f;
			float velY = this.random.nextInt(100)/10f;
			float velZ = this.random.nextInt(100)/80f;
			
			// flip direction of some particles
			if(this.random.nextBoolean()) velX *=-1;
			if(this.random.nextBoolean()) velZ *=-1;
			
			DLParticle particle = new DLParticle(gl,0.5f,new Point3D(0f,0f,0f), new Point3D(velX,velY,velZ),this.maxPartAge);
			particle.setColor(1f,0.5f,0.3f,1f);
			this.particleContainer.addParticle(particle);
			
			// reset cumulated time, if a particle was created
			this.cumulatedTime = 0L;
		}
	}
}
