unit ParticleUnit;

interface

uses
  OpenGL,
  MyUnit;

var
  //global
  ParticleTex:TextureImage;

procedure MakeParticle(x,y:Single);
procedure InitParticle;

implementation

type
Tpart=record
x,y,z:Single;
dx,dy,dz:Single;
r,g,b:Single;
Gx,Gy,Gz:Single;
end;
TParticles=record
X,Y,Z:Single;
dX,dY,dZ:Single;
Parts:array[0..63] of Tpart;
Life:Single;
Fade:Single;
Style:integer;
end;

var
  particles:array[0..49] of TParticles;
  CntParticle:Integer;
  Delay:Integer;

procedure InitParticle;
begin
  LoadTGA(ParticleTex, 'spark.tga');
  randomize;
  CntParticle:=0;
end;

procedure DrawParticle(const N : Integer);
var I : Integer;
    SlowDown : glFloat;
begin
  glDisable(GL_DEPTH_TEST);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  glCallList(ParticleTex.texID);
  glBegin(GL_QUADS);
    for I :=0 to 63 do
      with particles[N].Parts[I] do
      begin
        glColor4f(r, g, b, particles[N].life);
        glTexCoord2f(0.0, 0.0); glVertex3f(x-5.0, y-5.0, 0);
        glTexCoord2f(1.0, 0.0); glVertex3f(x+5.0, y-5.0, 0);
        glTexCoord2f(1.0, 1.0); glVertex3f(x+5.0, y+5.0, 0);
        glTexCoord2f(0.0, 1.0); glVertex3f(x-5.0, y+5.0, 0);

        // Calculate the new coords based on the change, dX, in a direction
        // and slow the change down as time goes by.
        slowdown :=particles[N].life/particles[N].fade;
        gy := gy+0.01;
        x :=(dX*slowdown*0.1) + x;
        y :=(dY*slowdown*0.1) + y+gy;
      end;
  glEnd();
  glEnable(GL_DEPTH_TEST);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  particles[N].life:=particles[N].life-particles[N].fade;
end;

procedure Eraseparticle(number:integer);
  var
    cnt:integer;
begin
  for cnt:=number+1 to CntParticle-1 do
    particles[cnt-1]:=particles[cnt];
    dec(CntParticle);
end;

procedure CreateParticle(N : Integer; x_coord,y_coord:Single);
var I : Integer;
begin
  Randomize;

  particles[N].life :=1;
  particles[N].fade :=random/40 +0.009;
  particles[N].X :=x_coord;//50+ Random(600) ;
  particles[N].Y :=y_coord;//50+ Random(400) ;
  //particles[N].Style :=Random(10);             // if style = 0, 1 then its a ring.  20% chance

  // setup all parts
  for I :=0 to 63 do
    with particles[N].Parts[I] do
    begin
      {if particles[N].Style < 3 then
        R := (random/10 +0.4)/5*0.2
      else}
        R := (random/10 -0.05)*0.2;
      gy := random*0.05;
      dX :=(R*cos(I/10))+gx*2;
      dY :=(R*sin(I/10))+gy*0.3;
      X :=particles[N].X+(random-0.5)*10;
      Y :=particles[N].Y;
      R :=random/3 + 0.4;
      G :=random/3 + 0.4;
      B :=random/3 + 0.7;
    end;
end;

procedure MakeParticle(x,y:Single);
var N : Integer;
begin
  if (CntParticle<50) and (Delay>2) then
         begin
           CreateParticle(CntParticle,x,y);
           Inc(CntParticle);
           Delay:=0;
         end
  else Inc(Delay);
  for N :=0 to CntParticle-1 do
  begin
        DrawParticle(N);
      // create a new particle
      if particles[N].life<=0 then
       EraseParticle(N);
  end;
end;

end.
