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 an explosion particle effect 
 * @author Frank Bruns
 *
 */
public class Explosion extends AbstractParticleEffect
{		
	/**
	 * Creates an explosion 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 Explosion(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/dummy.png"));
		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)
	{		
		while(this.particleContainer.getParticleCount() < this.particleContainer.getMaximumParticles())
		{
			// find some ramdom velocity values
			float velX = this.random.nextInt(100)/10f;
			float velY = this.random.nextInt(100)/10f;
			float velZ = this.random.nextInt(100)/10f;
			// flip direction of some particles
			if(this.random.nextBoolean()) velX *=-1;
			if(this.random.nextBoolean()) velY *=-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,0f,0f,1f);
			this.particleContainer.addParticle(particle);
		}
	}
}
