unit uGMPBul;

{$mode delphi}

interface

uses
  uGMPObject, uGMPLib, uGMPL;

type



TGun = record
  Name : string;
  Speed  : Single;
  Demage : Single;
  Sz : Single;
  AddSz : Single;
  AddAlpha : Single;
  Area  : Integer;
  Shift : Integer;
  Count : Byte;
  Time  : Byte;
  Time2  : Byte;
  Color : Byte;
  TempC : Integer;
  MaxCount1 : Integer;
  MaxCount2 : Integer;
  Dist : Single;
end;

const

Guns : array[0..cGunsCount] of TGun =
(
  (
    Demage : 4;
    Time   : 8;
    Dist   : 15;
  ),

// 1
  (
    Name : 'Пистолет';
    Speed : 12;
    Demage : 4;
    Sz : 4;
    AddSz : 0;
    AddAlpha : 0;
    Area  : 0;
    Shift : 0;
    Count : 1;
    Time  : 25;
    Time2 : 3;
    TempC : 0;
    MaxCount1 :  30;
    MaxCount2 : 120;
    Dist : 420;
  ),
// 2
  (
    Name : 'Огнемет';
    Speed : 1.75;
    Demage : 0.07;
    Sz : 2.5;
    AddSz : 1.07;
    AddAlpha : -8;
    Area  : 3;
    Shift : 5;
    Count : 3;
    Time  : 1;
    Time2 : 1;
    Color : 1;
    TempC : 10;
    MaxCount1 : 300;
    MaxCount2 : 1500;
    Dist : 90;
    ),
  // 3
  (
    Name : 'Дробовик';
    Speed : 12;
    Demage : 15;
    Sz : 4;
    AddSz : 0;
    AddAlpha : 0;
    Area  : 5;
    Shift : 3;
    Count : 3;
    Time  : 32;
    Time2 : 7;
    TempC : 0;
    MaxCount1 :  12;
    MaxCount2 : 120;
    Dist : 250;
    ),
  // 4
  (
    Name : 'Автомат';
    Speed :17;
    Demage : 2;
    Sz : 4;
    AddSz : 0;
    AddAlpha : 0;
    Area  : 5;
    Shift : 2;
    Count : 1;
    Time  : 4;
    Time2 : 2;
    TempC : 0;
    MaxCount1 :  50;
    MaxCount2 : 400;
    Dist : 400;
  )
);


{ TGMPBul }
type
TGMPBul = class(TGMPObject)
  Move  : ZPos;
  Dir : ZPos;
  Gun : TGun;
  Alpha : Single;
  DAlpha : Byte;
  fSzAddOn : Boolean;
  fAlphaAddOn : Boolean;
  TimeLive : Integer;
  Color : Integer;
  procedure Draw; override;
  procedure Update; override;
  constructor Create(Src : TGMPL; N : Byte);
end;



implementation
uses
  uGlobal, uGMP, ugmppart, zglHeader;
{ TGMPBul }

procedure TGMPBul.Draw;
begin
  pr2d_Rect(Pos.X - Size.X/2, Pos.Y - Size.Y/2, Size.X, Size.Y, Color, DAlpha, PR2D_FILL);
end;

procedure TGMPBul.Update;
var
  I, O : Integer;
  vPos : ZPos;
  vRect : zglTRect;
  //ColObj : TGMPObject;
begin
  TimeLive := TimeLive + 1;

  if fSzAddOn then
    Size.MultiplyBy(Gun.AddSz);

  vPos := Pos;
  Pos.AddBy(Move);

  if Gun.Speed < 3 then
  begin
    vRect.X := Pos.x - Size.x / 2;
    vRect.Y := Pos.y - Size.y / 2;
    vRect.W := Size.x;
    vRect.H := Size.y;
    for I := ListMapObj.Count - 1 downto 0 do
    if TGMPObject(ListMapObj.Items[I]).RectColission(vRect) then
    begin
      if TGMPL(ListMapObj.Items[I]) is TGMPL then
      begin
        TGMPL(ListMapObj.Items[I]).OnBul(Gun.Demage, Gun.TempC, Dir);
        Pos.AddBy(-Move/2);
      end
      else
      begin
        Pos := vPos;
        Enabled := false;
      end;
    end;
  end
  else
  begin
    O := -1;
    for I := 0 to ListMapObj.Count - 1 do
    if TGMPObject(ListMapObj.Items[I]).LineColission(vPos, Pos) then
      O := I;

    if O >= 0 then
    begin
      //TGMPObject(ListMapObj.Items[O]).Color := true;
      if TGMPL(ListMapObj.Items[O]) is TGMPL then
        TGMPL(ListMapObj.Items[O]).OnBul(Gun.Demage, Gun.TempC, Dir);
      Enabled := false;
    end;
    if TimeLive > 0 then
      TParLine.Create(vPos, Pos);
  end;



  //drawUpdate;
  if fAlphaAddOn then
  begin
    Alpha := Alpha + Gun.AddAlpha;
    if Alpha < 0 then
      Enabled := false;
    DAlpha := trunc(Alpha);
  end;

  if (TimeLive > 1000) or (Enabled = false) then
  begin
    ListBul.Delete(ListBul.IndexOf(Self));
    Self.Free;
  end;
end;

const
  pi_d_180 = pi/180;

constructor TGMPBul.Create(Src: TGMPL; N: Byte);
var
  A : Single;
begin
  TimeLive := 0;

  Gun := Guns[Src.Gun];

  if (Gun.Area <> 0) and (N > 1) then
  begin
    if N Mod 2 = 0 then
      A := Gun.Area
    else
      A := -Gun.Area;
  end
  else
    A := 0;

  if (Gun.Shift <> 0) then
    A := A + (random*2*Gun.Shift - Gun.Shift);

  Dir := Src.Dir;

  if A <> 0 then
    Move := Src.Dir.GetRotate(A * pi_d_180) * (Gun.Speed * cgspeed)
  else
    Move := Src.Dir * (Gun.Speed * cgspeed);

  Size.SetValue(Gun.Sz, Gun.Sz);
  Pos := Src.Pos + Src.Dir*chsize*(1+random);
  fSzAddOn := Gun.AddSz <> 0;
  fAlphaAddOn := Gun.AddAlpha <> 0;
  Alpha:=255;
  DAlpha := trunc(Alpha);

  case Gun.Color of
    0: Color := $FFFFFF;
    1: Color := cFireColor[random(Length(cFireColor))];
  end;

  Enabled:=true;
  ListBul.Add(Self);
end;

end.

