unit Otshelm_C;

interface
uses
  Map, MForm, Model, Otshelm_A, Windows, Messages,
  Forms, SysUtils, GLObjects, GLWin32Viewer,
  GLGeomObjects, GLVectorFileObjects, Otshelm_B,
  Keyboard, Setup, ghload, GLScene, Lives, Sound;

procedure loadlevel(FN : string);
procedure presskey;
procedure MClick(X,Y: integer);
procedure ScrCM;

procedure MLGe;

var
  SCRIPTF : string;
  NHero, TofG, OTofG, CorI, TCWin : byte;
  WMenu, WGame: boolean;
  dt : double;
  SCM, OCM  : Model.Tpoint;
  SCMB : boolean = false;

implementation
uses
logaut;

procedure loadlevel(FN : string);
var
  F : textfile;
  S : string;
begin
  FN:='\Map\1.txt';
  FN:=ExtractFilePath(Application.ExeName)+FN;
  if FileExists(FN) then
    begin
      assign(F, FN);
      reset(F);
      readln(F, NHero);
      readln(F, S);
      closefile(F);
    end;
   Map.Load(S);
   Otshelm_A.OCamera.TargetObject:=Model.Pers[NHero].A;
   Model.Pers[NHero].Au.Visible:=false;
   Model.Pers[NHero].A.Animation:='0';

   Liy:=TGLLightSource(Model.Pers[NHero].A.AddNewChild(TGLLightSource));
   Liy.Position.SetPoint(17,-14,57);


   WMenu:=true;
   WGame:=true;
end;

procedure MLGe;
var
  FN : string;
begin
  Sound.PSoundY('mtd');
  Model.Corob[CorI].F.SwitchToAnimation('cor2');
  Model.Corob[CorI].F.AnimationMode:=aamPlayOnce;
  TofG:=1;

  if (Shadow<>nil) then
  begin
    Shadow.ShadowingObject:=L.GLVis;
    Shadow.Position.Z:=0;
  end;

          If FullWin=true then OForm.FullViewer.Camera:=OCamera
          else
          Viewer.Camera:=OCamera;
end;

procedure MLGb(I : byte);
var
  FN : string;
begin
  if Model.Corob[I].Br then
  begin
  Sound.PSoundY('mtd');
  CorI:=I;
  logaut.GLlgCube.Position:=Model.Corob[I].F.Position;
  logaut.GLlgCube.Position.Z:=0.17;
  FN:=extractfilepath(Application.ExeName)+Model.Corob[I].L;
  if FileExists(FN)
  then logaut.LoadLG(FN);

  GLlgCube.Visible:=true;

  Model.Corob[I].F.SwitchToAnimation('cor1');
  Model.Corob[I].F.AnimationMode:=aamPlayOnce;
  SCMB:=true;
  LGGameCube.Position:=logaut.GLlgCube.Position;
  SCM.X:=0;
  SCM.Y:=-0.2;
  SCM.Z:=0.7;
  LGCamera.Position:=OCamera.Position;
  If FullWin=true then OForm.FullViewer.Camera:=LGCamera
  else
  Viewer.Camera:=LGCamera;
  TofG:=2;
  if (Shadow<>nil) then
  begin
    Shadow.ShadowingObject:=logaut.GlLgVis;
    Shadow.Position.Z:=0.175;
  end;

 end
 else
 GetGuiMess('Нет-нет-нет!','Вы уже починили эти трубы, поищите другие!','Хорошо');

end;


procedure ScrCM;
var
  i2 : real;
begin
  if  (round(LGCamera.Position.X*10)=round(SCM.X*10))
  and (round(LGCamera.Position.Y*10)=round(SCM.Y*10))
  and (round(LGCamera.Position.Z*10)=round(SCM.Z*10))
  then
    SCMB:=false
  else
    begin
      i2:=0.01;
      if LGCamera.Position.X>SCM.X then lGCamera.Position.X:=lGCamera.Position.X-i2
      else
      if LGCamera.Position.X<SCM.X then lGCamera.Position.X:=lGCamera.Position.X+i2;

      if lGCamera.Position.Y>SCM.Y then lGCamera.Position.Y:=lGCamera.Position.Y-i2
      else
      if LGCamera.Position.Y<SCM.Y then lGCamera.Position.Y:=lGCamera.Position.Y+i2;

      if LGCamera.Position.Z>SCM.Z then lGCamera.Position.Z:=lGCamera.Position.Z-i2
      else
      if lgCamera.Position.Z<SCM.Z then lGCamera.Position.Z:=lGCamera.Position.Z+i2;

    end;
end;

procedure MClick(X,Y: integer);
var
O : TGLCustomSceneObject;
begin
  if Setup.FullWin then
  O := OForm.FullViewer.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject
  else
  O := Viewer.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject;
  if (O<>nil) then
   begin
    if (Model.Pers[Nhero].A.Position.X>O.Position.X-2)
    and (Model.Pers[Nhero].A.Position.X<O.Position.X+2)
    and (Model.Pers[Nhero].A.Position.Y>O.Position.Y-2)
    and (Model.Pers[Nhero].A.Position.Y<O.Position.Y+2)
    then
    begin
      if (copy(O.Name,0,3)='COR') then
      MLGb(strtoint(copy(O.Name,5,3)))
      else
      if (copy(O.Name,0,3)='APP') then
     PersDialogb(strtoint(copy(O.Name,5,3)));
    end;

    if (copy(O.Name,0,3)='MPO') or (copy(O.Name,0,3)='APP')
    or (copy(O.Name,0,3)='MPD') or (copy(O.Name,0,3)='MPC')
    or (copy(O.Name,0,3)='MPA') or (copy(O.Name,0,3)='ATR')
    or (copy(O.Name,0,3)='COR')
    then
      begin
        Model.Pers[Nhero].MP.X:=O.Position.X;
        Model.Pers[Nhero].MP.Y:=O.Position.Y;
        Model.Pers[Nhero].Moved:=true;
        whatmodelroll(Nhero);
       end;
   end;
end;


// Обработка нажатия клавиш
procedure presskey;
var
  X,Y, OX,OY ,R, MR, MY :real;
begin
  MY:=0;
  R:=0;
  if IsKeyDown(VK_Down) or IsKeyDown('s') or IsKeyDown('ы') then
  MY:=MY-C*dT;
  if IsKeyDown(VK_Up) or IsKeyDown('w') or IsKeyDown('ц') then
  MY:=MY+C*dT;
  if IsKeyDown(VK_Left) or IsKeyDown('a') or IsKeyDown('ф') then
  R:=R+2*C*dT;
  if IsKeyDown(VK_Right) or IsKeyDown('d') or IsKeyDown('в') then
  R:=R-2*C*dT;

  with Model.Pers[Nhero] do
    begin
      if (MY<>0) or (R<>0) then Moved:=false;
      if not Moved then
      A.RollAngle:=A.RollAngle+15*R
      else
      begin
        MoveToCel(NHero);
        if roll(NHero) then
        MY:=C*dT;
        roll(NHero);
      end;

      X:=A.Position.X;
      Y:=A.Position.Y;
      OX:=X;
      OY:=Y;
      MR:=pi*(A.RollAngle)/180;
      if MoveObj(cos(MR)*MY,sin(MR)*MY,Model.EtLiveObj[Et].R, X,Y,Nhero, X,Y, False )
      then
        Moved:=false;

      if (OX=X) and (OY=Y) then
        begin
          if A.Animation<>'0' then
          A.Animation:='0';
        end
      else
       if A.Animation<>'1' then
        A.Animation:='1';

      A.Position.X:=X;
      A.Position.Y:=Y;
    end;
  GameCube.Position:=Model.Pers[Nhero].A.Position;
  BotsWork;
end;



end.
