unit uHero;

interface
uses
  zglHeader, Math, uMaterials, uObj, uBox, uLiveObj;

type

{ THero }

THero = class(TLiveObj)
  Box : TBox;
  Fail,
  Stelth    : Boolean;
  Lazer     : Boolean;
  LazerLine : zglTLine;
  anim : Single;
  Cat : Integer;
  constructor Create; overload;
  procedure Update; override;
  procedure Draw; override;
  procedure UpdateView;
  procedure DrawView;
  procedure Init(X, Y : Single);  overload;
end;

var
  Hero : THero;

implementation
uses
  uWorld, uEnemy, uLevelEnd, uConst;

{ THero }

procedure THero.Draw;
begin
  with DrawRect do
    asprite2d_Draw(mCat, X-4, Y-4, 32, 32, 0, aFrame1);
 // with DrawRect do
  //  pr2d_Rect(X, Y, W, H, Color, $FF);
  if Box <> nil then
  begin
    if Box.OnHero then
    begin
      Box.DrawRect.X := DrawRect.X - (Box.DrawRect.Wd2 - DrawRect.Wd2);
      Box.DrawRect.Y := DrawRect.Y - (Box.DrawRect.Hd2 - DrawRect.Hd2);
    end;
    Box.Draw;
  end;

  if Lazer then
  DrawView;
end;

constructor THero.Create;
begin
  Cat := 0;
end;

procedure THero.Update;
var
  mX, mY : Integer;
  aSpeed : Single;
  I : Integer;
  Area : zglTCircle;
begin
  mX := 0;
  mY := 0;
  if key_Down(K_W) then mY := mY - 1;
  if key_Down(K_S) then mY := mY + 1;
  if key_Down(K_A) then mX := mX - 1;
  if key_Down(K_D) then mX := mX + 1;

  if key_Press(K_R) then
    Lazer := not Lazer;

  if key_Press(K_B) then
    Bomb.Activate(true);

  if key_Press(K_E) then
  if Box = nil then
  begin
    Area := zCircle;
    Area.Radius := zCircle.Radius + cFishSize;

    for I := 0 to Fishs.Count - 1 do
    with TFish(Fishs[I]).zCircle do
    if col2d_PointInCircle(cX, cY, Area)  then
       TFish(Fishs[I]).Activate(true);

    Area.Radius := zCircle.Radius + cBoxSize;

    for I := 0 to Boxs.Count - 1 do
    with TBox(Boxs[I]).zCircle do
    if col2d_PointInCircle(cX, cY, Area)  then
    begin
      TBox(Boxs[I]).Activate(true);
      Objs.Remove(Hero.Box);
      Break;
    end;

  end
  else
    Box.Activate(false);

  if (mX <> 0) and (mY <> 0) then
    aSpeed := 5 * 0.7
  else
    aSpeed := 5;

  oldCircle := zCircle;
  zCircle.cX := zCircle.cX + mX * aSpeed;
  zCircle.cY := zCircle.cY + mY * aSpeed;
  if Box <> nil then
    Box.Update;

  Collision(Self, Box);
  RectUpdate;


  if anim > 0 then
    anim := anim - 0.1
  else
  if ((mX <> 0) or (mY <> 0)) then
    anim := 1;
  if mX < 0 then
  aFrame2 := 1 else
  if mX > 0 then
  aFrame2 := 3;

  if key_Press(K_5) then
  begin
  if Cat = 0 then
  Cat := 4
  else
  Cat := 0;
  end;
  aFrame1 := aFrame2 + round(anim) + Cat;

  Stelth := (Box <> nil) and (mX = 0) and (mY = 0);

  if Lazer then
  begin
    angle := m_Angle(mouse_X, mouse_Y, zCircle.cX, zCircle.cY);
    UpdateView;
  end;
end;

procedure THero.UpdateView;
var
  A180, ax1, ay1 : Single;
  O, I  : Integer;
  P  : zglTPoint2D;
begin
  A180 := deg2rad * angle;
  ax1 := Cos(A180);
  ay1 := Sin(A180);
  LazerLine.x0 := zCircle.cX;
  LazerLine.y0 := zCircle.cY;
  LazerLine.x1 := LazerLine.x0 + ax1 * maxDist;
  LazerLine.y1 := LazerLine.y0 + ay1 * maxDist;


  for O := 0 to Enemys.Count - 1 do
  if col2d_LineVsCircle(LazerLine, TEnemy(Enemys[O]).eCircle) then
  begin
    LazerLine.x1 := TEnemy(Enemys[O]).eCircle.cX;
    LazerLine.y1 := TEnemy(Enemys[O]).eCircle.cY;
    if m_Distance(LazerLine.x0, LazerLine.y0, LazerLine.x1, LazerLine.y1) > c_AlertDist2 then
    begin
      TEnemy(Enemys[O]).Timer   := 10;
      TEnemy(Enemys[O]).Active  := false;
      TEnemy(Enemys[O]).aFrame1 := 13;
    end;
    exit;
  end;

  for O := 0 to Objs.Count - 1 do
  for I := 1 to 2 do
  if Objs[O] <> Self then
  if col2d_LineVsRect(LazerLine, TObj(Objs[O]).zRect, @P) then
  begin
    LazerLine.x1 := P.X - ax1;
    LazerLine.y1 := P.Y - ay1;
  end;


end;

procedure THero.DrawView;
begin
  if Lazer then
  with LazerLine do
  begin
    pr2d_Line(x0, y0, x1, y1, $FF2222, $AA, PR2D_SMOOTH);
    ssprite2d_Draw(mLazer, x1 - 3.5, y1 - 3.5, 7, 7, random(360));
  end;
end;

procedure THero.Init(X, Y: Single);
begin
  Init(X, Y, 24, 24, $FFFFFF);
  Box := nil;
  Stelth    := false;
  Lazer     := false;
  maxDist  := 400;
  DynamicObjs.Add(Self);
  aFrame2 := 1;
  anim := 0;
  Fail := false;
  Bomb.Count := 3;
  Collision(Self);
  RectUpdate;
  DrawRectUpdate;
end;

end.
