unit uLiveObj;

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

{ TLiveObj }

TLiveObj = class(TObj)
  angle : Single;
  aFrame1, aFrame2,
  maxAngle, maxDist, dist4tex, Dist2Hero, Timer : Integer;
  TexArr, Arr : array of array[0..2] of zglTPoint2D;
  eCircle : zglTCircle;

  constructor Create(const X, Y, W, H : Single; const Color, maxAngle_, maxDist_ : Integer); overload;
  procedure Init(const X, Y, W, H : Single; const Color, maxAngle_, maxDist_ : Integer); overload;

  procedure UpdateView;
  procedure DrawView;
  function  Tictac : boolean;
  procedure Event; virtual;
end;

var
  LiveObjs : TList;

implementation
uses
  uWorld, uHero;

const c_shAngle = 5;

{ TLiveObj }

constructor TLiveObj.Create(const X, Y, W, H: Single; const Color, maxAngle_,
  maxDist_: Integer);
begin
  Init(X, Y, W, H, Color, maxAngle_, maxDist_);
end;

procedure TLiveObj.Init(const X, Y, W, H: Single; const Color, maxAngle_,
  maxDist_: Integer);
begin
  Init(X, Y, W, H, Color);
  maxAngle := trunc(maxAngle_ / c_shAngle);
  maxDist  := maxDist_;
  dist4tex := 2 * maxDist + 2;
  SetLength(Arr, 2*maxAngle + 1);
  SetLength(TexArr, 2*maxAngle + 1);
end;

procedure TLiveObj.DrawView;
var
  I : Integer;
begin
  pr2d_TriList(mAngleArea, @Arr[0], @TexArr[0], 0, 3*2*maxAngle+1, $FFFFFF, $BB, PR2D_FILL or FX_BLEND);
//  pr2d_TriList(mAngleArea, @Arr[0], @TexArr[0], 3, 3*2*maxAngle+1, $00FF00, $AA, PR2D_SMOOTH);
{  for I := 1 to Length(Arr) - 1 do
//  pr2d_Line(Arr[I][0].X, Arr[I][0].Y, Arr[I][1].X, Arr[I][1].Y, $22FF22, $FF, PR2D_SMOOTH);
  begin
   // pr2d_Line(100 + TexArr[I][0].X, 100 + TexArr[I][0].Y, 100 + 100*TexArr[I][1].X, 100 + 100*TexArr[I][1].Y, $22FF22, $FF, PR2D_SMOOTH);
   // pr2d_Line(100 + TexArr[I][0].X, 100 + TexArr[I][0].Y, 100 + 100*TexArr[I][2].X, 100 + 100*TexArr[I][2].Y, $22FF22, $FF, PR2D_SMOOTH);
  end;   }
end;

procedure TLiveObj.UpdateView;
var
  I, O, A1 : Integer;
  A180 : Single;
  P, Temp, TexTemp, Cen, TexCen : zglTPoint2D;
  Ln : zglTLine;
begin
  I := -1;
  Cen.X := eCircle.cX;
  Cen.Y := eCircle.cY;
  Temp := Cen;
  Ln.x0 := eCircle.cX;
  Ln.y0 := eCircle.cY;

  TexCen.X := 0.5;
  TexCen.Y := 0.5;
  TexTemp := TexCen;

  Dist2Hero := 2 * maxDist;

  for A1 := trunc(angle / c_shAngle) - maxAngle
         to trunc(angle / c_shAngle) + maxAngle do
  begin
    A180 := deg2rad * A1 * c_shAngle  ;
    Ln.x1 := Ln.x0 + Cos(A180) * maxDist;
    Ln.y1 := Ln.y0 + Sin(A180) * maxDist;
    for O := 0 to Objs.Count - 1 do
    if Objs[O] <> Self then
    if col2d_LineVsRect(Ln, TObj(Objs[O]).zRect, @P) then
    begin
      Ln.x1 := P.X;
      Ln.y1 := P.Y;
    end;

    if not Hero.Stelth and col2d_LineVsCircle(Ln, Hero.zCircle) then
       Event;

//  pr2d_Line(Ln.x0, Ln.y0, Ln.x1, Ln.y1, $22FF22, $10, PR2D_SMOOTH);
    I := I + 1;
    Arr[I, 0] := Cen;
    Arr[I, 1] := Temp;
    Arr[I, 2].X := Ln.x1;
    Arr[I, 2].Y := Ln.y1;
    Temp := Arr[I, 2];

    TexArr[I, 0] := TexCen;
    TexArr[I, 1] := TexTemp;
    TexArr[I, 2].X := 0.5+(Ln.x1 - Ln.x0) / dist4tex;
    TexArr[I, 2].Y := 0.5+(Ln.y1 - Ln.y0) / dist4tex;
    TexTemp := TexArr[I, 2];
  end;

end;

function TLiveObj.Tictac: boolean;
begin
  if Timer > 0 then
  begin
    Timer := Timer - 1;
    result := false;
  end
  else
    result := true;
end;

procedure TLiveObj.Event;
begin

end;

end.
