#ifndef CALIGNPARTICLEAFFECTOR_H_INCLUDED
#define CALIGNPARTICLEAFFECTOR_H_INCLUDED

#include "IParticleAffector.h"

class CAlignParticleAffector : public IParticleAffector
{
public:
   //!
   CAlignParticleAffector(f32 offset):
    m_offset(offset)
   {
   }

   //! 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];
            vector3df ang = particle.vector.getHorizontalAngle();
            ang.Y = (f32)atan2(particle.vector.X, particle.vector.Z);
            ang.Y *= (f32)RADTODEG64;
            f32 angle=(ang.Y<0)?ang.X:360.0f-ang.X;
            particle.rotation = m_offset -(f32)angle;

//        printf("vector (%f,%f,%f) Y=%f angle is %f\n",particle.pos.X,particle.pos.Y,particle.pos.Z,ang.Y,(f32)angle);

      }
   }


    E_PARTICLE_AFFECTOR_TYPE getType() const { return EPAT_NONE; }



    void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
    {
        out->addFloat("offset", m_offset);
    }

    s32 deserializeAttributes(s32 startIndex, io::IAttributes* in, io::SAttributeReadWriteOptions* options)
    {

        const char* name = in->getAttributeName(startIndex);
        if (!name || strcmp(name, "offset"))
            return startIndex; // attribute not valid
        When = in->getAttributeAsFloat(startIndex);
        ++startIndex;
        return startIndex;

    }

private:
   f32 m_offset;

};



#endif // CALIGNPARTICLEAFFECTOR_H_INCLUDED
