#include "Particle.h"
#include "LD.h"
#include "Level.h"

#include <list>



Particle::Particle() :
  GameObject( LD::GO_PARTICLE ),
  DX( 0.0f ),
  DY( 0.0f ),
  LifeTime( 0.0f ),
  AliveTime( 0.0f )
{
  m_Texture = LD::TEX_PLANKTON;
  Width = 8;
  Height = 8;
  SizeFactor = 0.25f;
  Color = 0xffffffff;
}


void Particle::Display()
{
  int     trueWidth = (int)( Width * SizeFactor );
  int     trueHeight = (int)( Height * SizeFactor );

  theApp.RenderTextureSectionRotated2d( X, Y, theApp.m_TexSec[m_Texture], Angle, Color, Color, Color, Color,
                                 trueWidth, trueHeight );
}



void Particle::Update( Level& Level, float ElapsedTime )
{
  Angle += (float)( rand() % 360 );

  XFract += DX * ElapsedTime * ( LifeTime - AliveTime ) / LifeTime;
  YFract += DY * ElapsedTime * ( LifeTime - AliveTime ) / LifeTime;

  while ( XFract >= 1.0f )
  {
    XFract -= 1.0f;
    ++X;
  }
  while ( XFract < 0.0f )
  {
    XFract += 1.0f;
    --X;
  }
  while ( YFract >= 1.0f )
  {
    YFract -= 1.0f;
    ++Y;
  }
  while ( YFract < 0.0f )
  {
    YFract += 1.0f;
    --Y;
  }

  AliveTime += ElapsedTime;
  if ( AliveTime >= LifeTime )
  {
    RemoveMe = true;
    AliveTime = LifeTime;
  }
}



