/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csa.tiletools.particle;

import java.awt.Dimension;
import java.util.Vector;

/**
 *
 * @author salchr
 */
public class ParticleEmitter extends Particle 
{
    // hold not used but instantiated particles!
    static Vector<Particle> particlesCache = new Vector<Particle>();
    int emitAngle; // in degrees

    int currentTickCount = 0; 
    ParticleEngine engine; // engine where this emitter belongs to
    
    
    // emitters can be two dimensional
    // emitters can emiit on every "side"
    // width/height by definition of particle
    
    // in coordinates relativ to 
    Dimension getCurrentEmitStartPositon()
    {
        return null;
    }

    public ParticleEmitter(ParticleData config)
    {
        super(config);
    }
    
    
    
    public void setParticleEngine(ParticleEngine e)
    {
        engine = e;
    }
    
    public static void clearCache()
    {
        particlesCache.clear();
    }
    public static int getCacheSize()
    {
        return particlesCache.size(); 
    }
    
    public static Particle getParticle(ParticleData config)
    {
        if (particlesCache.isEmpty())
        {
            return new Particle(config);
        }
        Particle p = particlesCache.remove(0);
        p.init(config);
        return p;
    }
    
    public static void cacheParticle(Particle p)
    {
        // emitters are not reused!
        if (p instanceof ParticleEmitter) return;
        particlesCache.addElement(p);
    }
    
    public boolean isActive()
    {
        return true;
    }
    
    // only updates numbers
    // returns false, if life of particle has ended!
    public boolean update()
    {
        if (config.mGenerateEachTick == 0) return false;
        currentTickCount = (currentTickCount+1)%config.mGenerateEachTick;
        if (currentTickCount != 0) return false;
        
        Particle p = getParticle(config);
        engine.addParticle(p);

        // as settings in emitter allows -> emit new particles to the system
        return true;
    }
    
}
