#ifndef CCOLLISIONPARTICLEAFFECTOR_H_INCLUDED
#define CCOLLISIONPARTICLEAFFECTOR_H_INCLUDED


#include "IParticleSystemSceneNode.h"

class CParticleCollisionAffector : public IParticleAffector
{
public:
   //!
   CParticleCollisionAffector(const core::plane3df& plane)
      : Plane(plane)
   {
   }

   //! Affects an array of particles.
   virtual void affect(u32 now, SParticle* particlearray, u32 count)
   {
      u32 p;
      for (p = 0; p < count; ++p)
      {
         SParticle& particle = particlearray[p];

         f32 distanceToPlane = Plane.getDistanceTo(particle.pos);
         if (distanceToPlane < 0.f)
         {

          // well, we collided


//          printf("vector before (%f,%f,%f)\n",particle.vector.X,particle.vector.Y,particle.vector.Z);
          vector3df n = Plane.Normal.normalize();
          particle.startVector = particle.vector;//(particle.startVector - n * 2.0 * (particle.startVector.dotProduct( n))).normalize() * particle.startVector.getLength();
//          particle.vector = (particle.vector - n * 2.0 * (particle.vector.dotProduct( n))).normalize() * particle.vector.getLength();
          particle.vector = particle.vector*2; //vector3df(0,0,0);
//          particle.startVector = (particle.startVector - n * 2.0 * (particle.startVector.dotProduct( n))).normalize() * particle.startVector.getLength();
//          particle.vector = particle.startVector;
          particle.vector = vector3df(0,0,0);
          particle.startVector = vector3df(0,0,0);

//          printf("vector after (%f,%f,%f)\n",particle.vector.X,particle.vector.Y,particle.vector.Z);

//            particle.vector = particle.vector * -Plane.Normal;
         }
      }
   }


    E_PARTICLE_AFFECTOR_TYPE getType() const { return EPAT_NONE; }



    void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
    {
        out->addPlane3d("plane", Plane);
    }

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

        ++startIndex;

        return startIndex;
    }

private:
   core::plane3df Plane;
};
/*
   // code used to test it...
   IParticleSystemSceneNode* particles = smgr->addParticleSystemSceneNode(false);
   particles->setMaterialFlag(video::EMF_LIGHTING, false);
   particles->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
   particles->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));


   IParticleEmitter* emitter = particles->createBoxEmitter();
      particles->setEmitter(emitter);
   emitter->drop();

   IParticleAffector* affector;

   affector = particles->createGravityAffector(core::vector3df(.02f, -0.03f, 0.f));
      particles->addAffector(affector);
   affector->drop();

   affector = new CCollisionParticleAffector(core::plane3df(0, -10, 0, 0, 1, 0));
      particles->addAffector(affector);
   affector->drop();

*/
#endif // CCOLLISIONPARTICLEAFFECTOR_H_INCLUDED
