unit uSmplPart;

interface

uses zglHeader;

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;
var
  PartsFadeOut     : array[0..2047] of TPartFadeOut;
  PartsGrowFadeOut : array[0..127] of TPartGrowFadeOut;
  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 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 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
      SSprite2d_draw(partsFadeOut[i].tex,
                     partsFadeOut[i].pos.X,
                     partsFadeOut[i].pos.Y,
                     partsFadeOut[i].wh,
                     partsFadeOut[i].wh,
                     random(360),
                     partsFadeOut[i].alpha);
end;

procedure UpdParts;
var i : integer;
begin
  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.
