#ifndef STOPPARTICLEAFFECTOR_H_INCLUDED
#define STOPPARTICLEAFFECTOR_H_INCLUDED

#include "os.h"
#include "IParticleSystemSceneNode.h"
namespace irr
{
namespace scene
{

// Thanx to vitek
// http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=15605&highlight=emitter
class StopParticleAffector : public IParticleAffector
{
public:
    StopParticleAffector(IParticleSystemSceneNode* ps, ISceneManager* msmgr, u32 when)
    : ParticleSystem(ps)
        , SceneManager(msmgr)
        , When(when)
    {}
    virtual void affect(u32 now, SParticle* particlearray, u32 count)
    {
        if (now < When)
            return; // nothing to do, not enough time has elapsed

        // specified time has been reached

        // clear the particle emitter so no new particles are generated
        ParticleSystem->setEmitter(0);

        // if the particle count every goes to zero, delete the particle system
        if (!count)
            SceneManager->addToDeletionQueue(ParticleSystem);
    }
// virtual E_PARTICLE_AFFECTOR_TYPE getType() { return EPAT_GRAVITY; };

    E_PARTICLE_AFFECTOR_TYPE getType() const { return EPAT_GRAVITY; }



    void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
    {
        out->addColor("When", When);
    }

    s32 deserializeAttributes(s32 startIndex, io::IAttributes* in, io::SAttributeReadWriteOptions* options)
    {
        const char* name = in->getAttributeName(startIndex);
        if (!name || strcmp(name, "When"))
            return startIndex; // attribute not valid
        When = in->getAttributeAsInt(startIndex);

        ++startIndex;
        return startIndex;
    }

private:
    IParticleSystemSceneNode* ParticleSystem;
    ISceneManager* SceneManager;
    u32 When; // time that we clear the particle system emitter
};
/*
// usage
IParticleSystemSceneNode* psn = smgr->addParticleSystemSceneNode(...);

IParticleAffector* affector;

// fade the particle to some transparent color. every particle takes 2/10 sec to fade
affector = psn->addFadeOutAffector(video::SColor(0,0,0,0), 200)
  psn->addAffector(affector);
affector->drop();

// this is when the emitter should stop emitting
u32 when = Device->getTimer()->getTime() + 400;

// this affector will stop the particle system from creating new particles
// and will remove the particle system when no more particles exist
affector = new MyParticleAffector(psn, smgr, when)
  psn->addAffector(affector);
affector->drop();
*/
}
}


#endif // STOPPARTICLEAFFECTOR_H_INCLUDED
