unit uEnemy;

interface
uses
  Classes, zglHeader, Math, uConst, uMaterials, uObj, uLiveObj;

type

{ TEnemy }

TEnemy = class(TLiveObj)
  sinAngle, angle_m : Single;
  alertLevel : Integer;
  angle_b, angle_e : Integer;
  Active : Boolean;
  testDist : Single;
  procedure Update; override;
  procedure Draw; override;
  procedure Event; override;
  procedure setAngle(const angle_b_, angle_e_: Integer);
  procedure Look4Point(X, Y : Single);
  constructor Create(X, Y : Single; maxAngle_, maxDist_, angle_b_, angle_e_ : Integer); overload;
end;

var
  Enemys : Tlist;
  
implementation
uses
  uHero, uWorld;
{ TEnemy }

procedure TEnemy.Draw;
var
  I : Integer;
begin
  with DrawRect do
  begin
    asprite2d_Draw(mMar, X - 12, Y - 24, 64, 64, 0, aFrame1);
   // pr2d_Rect(X, Y, W, H, Color, $FF{, PR2D_FILL});
    if alertLevel > 0 then
      asprite2d_Draw(mMar, X - 12, Y - 24, 64, 64, 0, 13 + alertLevel);
    //    pr2d_Circle(X -10 + I * 10, Y - 10, 4, $FFFFFF, $FF, 20, PR2D_FILL);

   // text_Draw(mFont_1, X, Y, u_IntToStr(trunc(angle)));
  end;
end;

procedure TEnemy.Event;
begin
  angle     := m_Angle(Hero.zCircle.cX, Hero.zCircle.cY, zCircle.cX, zCircle.cY);
  Dist2Hero := trunc(m_Distance(Hero.zCircle.cX, Hero.zCircle.cY, zCircle.cX, zCircle.cY));
  Timer     := 100;
  sinAngle  := 0;
end;

procedure TEnemy.setAngle(const angle_b_, angle_e_: Integer);
begin
  angle_b := angle_b_;
  angle_e := angle_e_;
  angle_m := +1;
  angle   := angle_b;
end;

procedure TEnemy.Look4Point(X, Y: Single);
begin
  angle     := m_Angle(X, Y, zCircle.cX, zCircle.cY);
  Timer     := 100;
  sinAngle  := 0;
end;

procedure TEnemy.Update;
begin
  if Tictac then
  begin
    if m_Distance(Hero.zCircle.cX, Hero.zCircle.cY, zCircle.cX, zCircle.cY) < c_AlertDist1 then
      Event;

    sinAngle := sinAngle + 0.02;
    if sinAngle > 3.14 then
      sinAngle := 0;

    angle := angle + (sin(sinAngle) + 1) * angle_m;

    if ((angle_m > 0) and (angle >= angle_e))
    or ((angle_m < 0) and (angle <= angle_b)) then
    begin
      Timer := 50;
      angle_m := -angle_m;
      sinAngle := 0;
    end;
    Active := true;

  end;


  if Dist2Hero < testDist + 75 then
    alertLevel := 3
  else
  if Dist2Hero < testDist + 120 then
    alertLevel := 2
  else
  if Dist2Hero < maxDist then
    alertLevel := 1
  else
    alertLevel := 0;

  if alertLevel > 0 then
  begin
    if testDist < maxDist - 50 then
      testDist := testDist + alertLevel/2;
      if alertLevel = 3  then  begin
      Hero.Fail := true;

      end;
  end
  else
    testDist := 0;

  if Active then
  begin
    UpdateView;
    if (angle < 300) and (angle >= 240) then aFrame1 := 4
    else
    if (angle < 240) and (angle >= 135) then aFrame1 := 3
    else
    if (angle < 135) and (angle >= 45) then aFrame1 := 2
    else
      aFrame1 := 1;
  end;

  if alertLevel > 1 then
     aFrame1 := aFrame1 + 4*(alertLevel - 1);

  Collision(Self);
  RectUpdate;
  DrawRectUpdate;
  eCircle.cX := zRect.X + 37;
  eCircle.cY := zRect.Y + 17;
end;

constructor TEnemy.Create(X, Y: Single; maxAngle_, maxDist_, angle_b_,
  angle_e_: Integer);
begin
  Create(X, Y, 48, 40, $FF0000, maxAngle_, maxDist_);
  setAngle(angle_b_, angle_e_);
  RectUpdate;
  Collision(Self);
  RectUpdate;
  DrawRectUpdate;
  eCircle.cX := zRect.X + 35;
  eCircle.cY := zRect.Y + 21;
  eCircle.Radius := 7;
  Enemys.Add( Self );
  DynamicObjs.Add(Self);
  alertLevel := 0;

  Active := true;

  UpdateView;
  Dist2Hero := 2000;
  testDist := 0;
  Timer := 0;
end;

end.
