unit Lives;

interface
uses
  Model, Map, MForm, GLScene, SysUtils, VectorGeometry;


function whatroll(x2, x1, y2, y1 :real) : integer;
function roll(i: word): boolean;
function moveobj(MX, MY, R,X,Y : real; N:byte; var RX, RY : real): boolean;

var
  PinP : boolean;
  Bot  : word;

implementation

function colissionMap(X,Y : real): boolean;
begin
  if  (X > 5)
  and (Y > 5)
  and (X < (L.X-2.5)*2)
  and (Y < (L.Y-2.5)*2)
  then    
  result:=true
  else
  result:=false;
end;

function colissionC(X,Y,R, CX,CY, SX,SY: real): boolean;
begin
  if  (X+R > CX - SX/2)
  and (X-R < CX + SX/2)
  and (Y+R > CY - SY/2)
  and (Y-R < CY + SY/2)
  then
  result:=true
  else
  result:=false;
end;

function colissionR(X,Y,R, CX,CY,CR : real): boolean;
begin
  result:=false;
  if colissionC(X,Y,R, CX,CY,CR,CR) then
    begin
      X:=X-CX;
      Y:=Y-CY;
      CR:=CR+R;
      if  (Y > -sqrt(sqr(CR)-sqr(X)))
      and (Y <  sqrt(sqr(CR)-sqr(X)))
      then
      result:=true
    end;
end;

function colission(X,Y,R : real; N: byte): boolean;
var
  i: word;
begin
  result:=true;
  if colissionMap(X,Y) then
  begin
    result:=false;
    for i:=1 to Map.L.MofC do
    if colissionC(X,Y,R,
         L.Cube[i].Position.X, L.Cube[i].Position.Y,
         L.Cube[i].Scale.X, L.Cube[i].Scale.Y)
    then
    result:=true;

    if not result then
    for i:=1 to Model.MofD do

    if colissionR(X,Y,R,
         Model.DecO[i].F.Position.X, Model.DecO[i].F.Position.Y,
         Model.EtDecO[Deco[i].Et].R)
    then
    result:=true;

    if not result then
    for i:=1 to Model.MofP do
    if (I<>N)
    and (colissionR(X,Y,R,
         Model.Pers[i].A.Position.X, Model.Pers[i].A.Position.Y,
         Model.EtLiveObj[Pers[i].Et].R))
    then
      begin
        result:=true;
        PinP:=true;
        Bot:=N;
      end;

 end
end;

function whatroll(x2, x1, y2, y1 :real) : integer;
var
  c, a : real;
begin
  a:=abs(x2-x1);
  c:=sqr(x1-x2)+sqr(y2-y1);
  c:=sqrt(c);
  c:=a/c;
  a:=arcsin(c)*180/pi;

  if (x1<=x2) and (y1<y2) then a:=-a;
  if (x1<=x2) and (y1>=y2) then a:=180+a;
  if (x1>x2) and (y1<=y2) then a:=a;
  if (x1>x2) and (y1>y2) then a:=180-a;
  result:=round(a)-90;
end;

procedure whatmodelroll(i : word);
var
 x1, x2, y1, y2 : real;
begin
  x1:=Model.Pers[i].A.Position.X;
  y1:=Model.Pers[i].A.Position.Y;

  x2:=Model.Pers[i].MP.X;
  y2:=Model.Pers[i].MP.Y;

  Model.Pers[i].MA:=whatroll(x1,x2,y1,y1);
end;

function roll(i : word): boolean;
begin
  result:=false;
  if (Model.Pers[i].A.RollAngle>Model.Pers[i].MA-3)
  and (Model.Pers[i].A.RollAngle<Model.Pers[i].MA+3)

   then
  result:=true
  else
    begin
      if Model.Pers[i].A.RollAngle>Model.Pers[i].MA then
      Model.Pers[i].A.RollAngle:=Model.Pers[i].A.RollAngle-1
      else
      if Model.Pers[i].A.RollAngle<Model.Pers[i].MA then
      Model.Pers[i].A.RollAngle:=Model.Pers[i].A.RollAngle+1;
    end;
end;

function moveobj(MX, MY, R,X,Y : real; N:byte; var RX, RY : real): boolean;
begin
  result:=false;
  if not colission(X+MX, Y+MY, R+0.3, N) then
    begin
      RX:=X+MX;
      RY:=Y+MY;
      result:=true;
    end;
end;




end.
