unit UGMPLE;

{$mode delphi}

interface

uses
  uGMPL, uGMPLib;

type

{ TGMPLE }

TGMPLE = class(TGMPL)
  Run : Boolean;
  LastHeroPoint : ZPos;
  constructor Create(X, Y : Single);
  procedure Control; override;
  procedure Live0; override;
end;

implementation
uses
  uGlobal, uGMP, uGMPObject, uGMPBul;
{ TGMPLE }

constructor TGMPLE.Create(X, Y: Single);
begin
  Pos.SetValue(X, Y);
  Size.x := chsize;
  Size.y := chsize;
  Speed := 1;
  Gun := random(4)+1;
  GunMag[Gun].Count1:= 2;//Guns[Gun].MaxCount1;
  GunMag[Gun].Count2:= 15;//Guns[Gun].MaxCount2;

  Color := $F0F0F0;
  Update;
  Live:= 45+random(17);
  //Gun := 0;
end;

procedure TGMPLE.Control;
var
  L : Single;
  TempDir : ZPos;
  SeeH : Boolean;
begin
  Color := $F0F0F0;
  Move.SetValue(0, 0);
  Fire := false;

  TempDir := Map.GH.Pos - Pos;
  L := TempDir.Length;
  TempDir.Normalize;
  SeeH := Map.FreeLineToObj(Self, Map.GH);

  if SeeH and ((L < 400) or ((TempDir * Map.GH.Dir < -0.92) and Map.GH.Fire)) then
  begin
    Dir := TempDir;
    //Move := Dir;
    LastHeroPoint := Map.GH.Pos;
    Run := true;
  end;

  if (Gun > 0) and (GunMag[Gun].Count1 + GunMag[Gun].Count2 < 1) then
    Gun := 0;

  if Run then
  begin
    Color := $E0AF20;
    if SeeH then
    begin
      if (L < Guns[Gun].Dist) then
      begin
        if Gun = 0 then
        begin
          if Timer <= 0 then
          begin
            Map.GH.OnBul(2, 0, Dir);
            Color := $CC1010;
            Timer := Guns[Gun].Time;
          end;
        end
        else
        if (random > 0.4) then
        begin
          Fire := true;
        end;
      end
      else
        Move := Dir;
    end
    else
    begin
      if LastHeroPoint.LengthTo(Pos.X, Pos.Y) > chsize then
        Move := Dir
      else
        Run := false;
    end;
  end;

  {if L < 500 then

  if Gun <> 2 then

  Move := Dir;
  Fire := (random > 0.7) and Map.FreeLineToObj(Self, Map.GH);
  }

  //writeln(Dir.X:0:2, ' ', Dir.Y:0:2)
end;

procedure TGMPLE.Live0;
begin
  ListMapObj.Delete(ListMapObj.IndexOf(Self));
  ListLObj.Delete(ListLObj.IndexOf(Self));

  if random > 0.5 then

  begin
  if random > 0.8 then
    Map.AddBonus(Pos.X, Pos.Y, 5)
  else
  if (random >= 0.5) and  (GunMag[Gun].Count2 + GunMag[Gun].Count1 > 0) then
    Map.AddBonus(Pos.X, Pos.Y, Gun)
  else
    Map.AddBonus(Pos.X, Pos.Y, 0);
  end;

  Self.Free;
end;

end.

