unit uSmplPart;

interface

uses zglHeader,
     uVar;

type

  TPartFadeOut = packed record
    exist  : boolean;
    tex    : zglPTexture;
    spd,ang: single;
    pos    : zglTPoint2d;
    alpha  : integer;
    dec, wh: byte;
  end;

  TPartGrowFadeOut = packed record
    exist  : boolean;
    tex    : zglPTexture;
    gr     : byte;
    wh     : integer;
    pos    : zglTPoint2d;
    alpha  : integer;
    dec    : byte;
  end;

  TSw = packed record
    exist    : boolean;
    life, lt : integer;
    pos      : zglTPoint2d;
    spd, ang : single;
  end;

var
  PartsFadeOut     : array[0..2047] of TPartFadeOut;
  PartsGrowFadeOut : array[0..127] of TPartGrowFadeOut;
  Sw               : array [0..127] of TSw;
  fdParts : integer;


procedure AddPartFadeOut(tex : zglPTexture; pos : zglTPoint2d; alpha, dec, wh : byte; spd, ang: single);
procedure AddPartGFO(tex : zglPTexture; pos : zglTPoint2d; alpha, dec, gr : byte);
procedure AddSw(pos : zglTPoint2d; ang, spd : single; lt : integer);

procedure DrawParts;
procedure UpdParts;

implementation

procedure AddPartFadeOut(tex : zglPTexture; pos : zglTPoint2d; alpha, dec, wh : byte; spd, ang: single);
var b : boolean;
    i : integer;
begin
  b := false;
  i := 0;
  while (not b) and (i < 2047) do
    if not PartsFadeOut[i].exist then
      begin
        PartsFadeOut[i].exist := true;
        PartsFadeOut[i].tex   := tex;
        PartsFadeOut[i].pos   := pos;
        PartsFadeOut[i].alpha := alpha;
        PartsFadeOut[i].dec   := dec;
        PartsFadeOut[i].spd   := spd;
        PartsFadeOut[i].ang   := ang;
        PartsFadeOut[i].wh    := wh;
        inc(fdParts);
        b := true;
      end else inc(i);
end;

procedure AddPartGFO(tex : zglPTexture; pos : zglTPoint2d; alpha, dec, gr : byte);
var b : boolean;
    i : integer;
begin
  b := false;
  i := 0;
  while (not b) and (i < 127) do
    if not PartsGrowFadeOut[i].exist then
      begin
        PartsGrowFadeOut[i].exist := true;
        PartsGrowFadeOut[i].tex   := tex;
        PartsGrowFadeOut[i].pos   := pos;
        PartsGrowFadeOut[i].alpha := alpha;
        PartsGrowFadeOut[i].dec   := dec;
        PartsGrowFadeOut[i].gr    := gr;
        PartsGrowFadeOut[i].wh    := 32;
        inc(fdParts);
        b := true;
      end else inc(i);
end;

procedure AddSw(pos : zglTPoint2d; ang, spd : single; lt : integer);
var i: integer;
begin
  for I := 0 to 127 do
    if not sw[i].exist then
      begin
        sw[i].pos   := pos;
        sw[i].spd   := spd;
        sw[i].ang   := ang;
        sw[i].exist := true;
        sw[i].lt    := lt;
        sw[i].life  := 0;
        break;
      end;
end;

procedure DrawParts;
var i : integer;
begin
  for I := 0 to 127 do
    if PartsGrowFadeOut[i].exist then
      SSprite2d_draw(PartsGrowFadeOut[i].tex,
                     PartsGrowFadeOut[i].pos.X - PartsGrowFadeOut[i].wh / 2,
                     PartsGrowFadeOut[i].pos.Y - PartsGrowFadeOut[i].wh / 2,
                     PartsGrowFadeOut[i].wh,
                     PartsGrowFadeOut[i].wh,
                     0,
                     PartsGrowFadeOut[i].alpha);

  for I := 0 to 2047 do
    if PartsFadeOut[i].exist then
       begin
         if partsFadeOut[i].tex = texFire then fx_SetBlendMode( FX_BLEND_ADD );
         SSprite2d_draw(partsFadeOut[i].tex,
                        partsFadeOut[i].pos.X,
                        partsFadeOut[i].pos.Y,
                        partsFadeOut[i].wh,
                        partsFadeOut[i].wh,
                        random(360),
                        partsFadeOut[i].alpha);
         fx_SetBlendMode( FX_BLEND_Normal );
       end;
end;

procedure UpdParts;
var i : integer;
begin
   for I := 0 to 127 do
    if sw[i].exist then
      begin
        sw[i].pos.X := sw[i].pos.X + sw[i].spd * m_cos(round(rad2deg * sw[i].ang));
        sw[i].pos.Y := sw[i].pos.Y + sw[i].spd * m_sin(round(rad2deg * sw[i].ang));

        if (sw[i].pos.X < 0  )or
           (sw[i].pos.Y < 0  )or
           (sw[i].pos.X > 800)or
           (sw[i].pos.Y > 600)  then  sw[i].exist := false;

        inc(sw[i].life);
        if sw[i].life >= sw[i].lt then sw[i].exist := false;


        AddPartFadeOut(texFire, sw[i].pos, round(255*(sw[i].lt - sw[i].life)/sw[i].lt), 5, 24, 0, 0);

       end;

  for I := 0 to 2047 do
    if PartsFadeOut[i].exist then
      begin
        PartsFadeOut[i].pos.X := PartsFadeOut[i].pos.X + PartsFadeOut[i].spd * m_cos(round(rad2deg * PartsFadeOut[i].ang));
        PartsFadeOut[i].pos.Y := PartsFadeOut[i].pos.Y + PartsFadeOut[i].spd * m_sin(round(rad2deg * PartsFadeOut[i].ang));

        dec(partsFadeOut[i].alpha, partsfadeout[i].dec);
        if partsFadeOut[i].alpha < 5 then
        begin
          partsfadeout[i].exist := false;
          dec(fdParts);
        end;
      end;

  for I := 0 to 127 do
    if PartsGrowFadeOut[i].exist then
      begin
        dec(PartsGrowFadeOut[i].alpha, PartsGrowFadeOut[i].dec);
        inc (PartsGrowFadeOut[i].wh, PartsGrowFadeOut[i].gr);
        if PartsGrowFadeOut[i].alpha < 5 then
        begin
          PartsGrowFadeOut[i].exist := false;
          dec(fdParts);
        end;
      end;
end;

end.
