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 Rain extends AbstractParticleEffect {

	/**
	 * Creates a rain effect with the specified parameters. Be aware
	 * that an object of this class is just a single rain source with a
	 * limited range. If you would like to have rain all over the map
	 * you should add several rain sources (objects of this class) to 
	 * your map.
	 * @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 Rain(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/rain.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(150)/1.5f;
			float velY = -200f; // negative value, to let the particle fall down
			float velZ = this.random.nextInt(150)/1.5f;
			
			// flip direction of some particles
			if(this.random.nextBoolean()) velX *=-1;
			if(this.random.nextBoolean()) velZ *=-1;
			
			DLParticle particle = new DLParticle(gl,5f,new Point3D(0f,0f,0f), new Point3D(velX,velY,velZ),this.maxPartAge);
			particle.setColor(0.6f,0.6f,0.6f,1f);
			this.particleContainer.addParticle(particle);
			
			// reset cumulated time, if a particle was created
			this.cumulatedTime = 0L;
		}
	}

}
