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;

/**
 * This class represents the particle effect that can be used as chimney smoke
 * for buildings.
 * @author Frank Bruns
 *
 */
public class ChimneySmoke extends AbstractParticleEffect
{	
	/**
	 * Creates an chimney smoke effect with the specified parameters.
	 * It is a relativly constant stream of particles that float to the
	 * sky and have a little deviation to the left or right. 
	 * @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 ChimneySmoke(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/smoke.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)/250f;
			float velY = this.random.nextInt(100)/10f;
			float velZ = this.random.nextInt(100)/250f;
			
			// 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(0.2f,0.2f,0.2f,1f);
			this.particleContainer.addParticle(particle);
			
			// reset cumulated time, if a particle was created
			this.cumulatedTime = 0L;
		}
	}
}
