unit uGMPL;

{$mode delphi}

interface

uses
  uGMPObject, uGMPLib;

const
  cGunsCount = 4;

type
  TGunMag = record
    Count1, Count2 : Integer;
  end;
  { TGMPL }

  TGMPL = class(TGMPObject)
    Dir,
    Move : ZPos;
    Regun,
    DrawFire,
    Fire  : Boolean;
    Speed : Single;
    Gun   : Integer;
    Timer : Integer;
    Live  : Single;
    TempC : Integer;
    GunMag : array[1..cGunsCount] of TGunMag;
    PowerTime : Integer;
   // RegunSountTimer : Integer;
    procedure Control; virtual;
    procedure Draw; override;
    procedure Update; override;
    procedure AddGun(G, C : Integer);
    procedure OnBul(Demage : Single; ATempC : Integer; Dir : ZPos); virtual;
    procedure Live0; virtual;
    procedure SetPower;
  end;

implementation
uses
  uGMP, uGMPBul, uGlobal, ugmppart, zglHeader, GSound;
{ TGMPL }

procedure TGMPL.Control;
begin

end;

procedure TGMPL.Draw;
begin
  inherited Draw;
  if PowerTime > 0 then
    pr2d_Rect(DrawPos.X - 3, DrawPos.Y - 3, DrawSize.X + 6, DrawSize.Y + 6, $faf75c, 120, PR2D_FILL);
  if Gun > 0 then
  begin
    if DrawFire then
      pr2d_Rect(Pos.X + Dir.X * 10 - 1.5, Pos.Y + Dir.Y * 10 - 1.5, 3, 3, $ffa500, 240, PR2D_FILL);
    pr2d_Rect(Pos.X + Dir.X * 7 - 2, Pos.Y + Dir.Y * 7 - 2, 4, 4, $FFFFFF, 255, PR2D_FILL);
  end;
end;

procedure TGMPL.Update;
var
  vSpeed : single;
  I, I2 : Integer;
  vPos{, vPosT} : ZPos;
  ColX, ColY : Boolean;
  vRect, vRect2 : zglTRect;
begin
  Control;
  Move.Normalize;
 { if (Move.X <> 0) and (Move.Y <> 0) then
    vSpeed := Speed * cgspeed * 0.707
  else  }
    vSpeed := Speed * cgspeed;

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

  ColX := true;
  ColY := true;

  vRect.X := Pos.x - Size.x / 2;
  vRect.Y := Pos.y - Size.y / 2;
  vRect.W := Size.x;
  vRect.H := Size.y;
  vRect2 := vRect;

  for I := 0 to ListMapObj.Count - 1 do
  if (ListMapObj.Items[I] <> Self) and TGMPObject(ListMapObj.Items[I]).RectColission(vRect) then
  begin
    vRect2.Y := Rect.Y;
    for I2 := 0 to ListMapObj.Count - 1 do
    if (ListMapObj.Items[I2] <> Self) and TGMPObject(ListMapObj.Items[I2]).RectColission(vRect2) then
    begin
      Pos.X := vPos.X;
      ColX := false;
      Break;
    end;

    vRect2.X := Rect.X;
    vRect2.Y := vRect.Y;
    for I2 := 0 to ListMapObj.Count - 1 do
    if (ListMapObj.Items[I2] <> Self) and TGMPObject(ListMapObj.Items[I2]).RectColission(vRect2) then
    begin
      Pos.Y := vPos.Y {- Move.Y/5};
      ColY := false;
      Break;
    end;
    if ColX and ColY then
      Pos := vPos;
  end;

  if Regun then
    Fire := false;

  DrawFire := false;

  if Timer > 0 then
    Timer := Timer - 1
  else
  begin
    if Regun then
    begin
       if (GunMag[Gun].Count1 < Guns[Gun].MaxCount1)
       and (GunMag[Gun].Count2 > 0) then
       begin
         GunMag[Gun].Count1 := GunMag[Gun].Count1 + 1;
         GunMag[Gun].Count2 := GunMag[Gun].Count2 - 1;
         Timer := Guns[Gun].Time2
       end
       else
         Regun := false;
    end
    else
    if Fire and (Gun > 0) then
    begin
      Regun := false;

      if GunMag[Gun].Count1 > 0 then
        PlaySound(1 + Gun);

      for I := 1 to Guns[Gun].Count do
      if GunMag[Gun].Count1 > 0 then
      begin
        TGMPBul.Create(Self, I);
        if PowerTime < 1 then
          GunMag[Gun].Count1 := GunMag[Gun].Count1 - 1;
        DrawFire := true;
      end;
      if GunMag[Gun].Count1 <= 0 then
      begin
        PlaySound(5 + Gun);
        Regun := true;
      end;

      Timer := Guns[Gun].Time;
    end;
  end;

  inherited Update;

  if TempC > 0 then TempC := TempC - 1
  else
  if TempC < 0 then TempC := TempC + 1;

  if TempC > 100 then
  begin
    if Gun = 2 then
      Live := Live - 0.05
    else
      Live := Live - 0.1;
    TParFire.Create(Pos);
  end;

  if PowerTime > 0 then
  begin
    PowerTime := PowerTime - 1;

  end;

  if Live > 200 then
    Live := 200
  else
  if Live < 0 then
  Live0;

end;

procedure TGMPL.AddGun(G, C: Integer);
begin
{  GunMag[Gun].Count1 := 50;
  GunMag[Gun].Count2 := C;
  Gun:=G;   }

end;

procedure TGMPL.OnBul(Demage: Single; ATempC: Integer; Dir : ZPos);
begin
  Color := $FF0000;
  if PowerTime <= 0 then
  begin
  Live := Live - Demage;
  TempC := TempC + ATempC;
  end;
  if TempC > 200 then TempC := 200;


  if Demage > 1 then
  begin
    if Random > 0.5 then
    begin
      PlaySound(10);
      TParBlood.Create(Pos, Dir*1.4);
      TParBlood.Create(Pos, Dir*1.2);
    end;
    TParBlood.Create(Pos, Dir);
    TParBlood.Create(Pos, Dir);
    TParBlood.Create(Pos, Dir);
  end;
end;

procedure TGMPL.Live0;
begin

end;

procedure TGMPL.SetPower;
begin
  PowerTime:=1000;
  GSound.TrackPlay(NTrackMusicBox);
end;

end.

