program game;

//{$APPTYPE CONSOLE}

uses
  SysUtils,
  Audio in 'Uses\Audio.pas',
  Bass in 'Uses\bass.pas',
  JSMEngine in 'Uses\JSMEngine.pas',
  Sys_U in 'Uses\Sys_U.pas',
  th in 'Uses\th.pas';
var logo1,logo2,menu_bg: integer;
    tim,ma: single;
    k,ks: integer;
    menupos: integer = 1;  aut: boolean = false;
    tx,ty: array[1..50] of string;

procedure Render;
var i: integer;
begin
// Весь рендер осуществляется здесь...
  R2D.BeginBatch;

  case Mode of
  MLogo:
    begin
      if tim<=5 then
      R2D.Render4(0,0,Width,Height,0,logo1,0)
      else
      R2D.Render4(0,0,Width,Height,0,logo2,0);
    end;
  MGame:
    begin
      DrawMap;
      DrawGamer;
      r2d.Render4(mouse.x-15,mouse.y-15,30,30,ma,mo,0);
    end;
  MLoad:
    begin
      if level>4 then
      r2d.Render4(0,150,800,450,0,im[3],0);
    end;  
  MOut:
    begin
      DrawMap;
      DrawGamer;
      //r2d.Render4(mouse.x-15,mouse.y-15,30,30,ma,mo,0);
      r2d.Render4(200,250,400,100,0,im[4],0);
    end;  
  MMenu:
    begin
      R2D.Render4(0,0,width,height,0,menu_bg,0);
      r2d.Render4(mouse.x-15,mouse.y-15,30,30,ma,mo,0);
    end;
  MPred:
    begin
      R2D.Render4(0,0,width,height,0,im[2],0);
      r2d.Render4(mouse.x-15,mouse.y-15,30,30,ma,mo,0);
    end;
  end;
  R2D.EndBatch;
  case Mode of
  MMenu:
    begin
      Eng.ShowText(width-230,Height-250+50,'Atakon de la Robotoj',lime);
      if (level<=4) and (level>1) then
      Eng.ShowText(width-200,Height-250+80,'Продолжить',white)
      else
      Eng.ShowText(width-200,Height-250+80,'Продолжить',$ffaaaaaa);
      Eng.ShowText(width-200,Height-250+110,'Новая игра',white);
      Eng.ShowText(width-200,Height-250+140,'Авторы',white);
      Eng.ShowText(width-200,Height-250+170,'Выход',white);
      Eng.ShowText(width-230,Height-250+80+(menupos-1)*30,'>',lime);
      if menupos=3 then aut:=true else aut:=false;
      if aut then
      begin
        Eng.ShowText(40,200,'Авторы',Lime);
        Eng.ShowText(40,230,'Pea Games',white);
        Eng.ShowText(60,260,'Л.Кирилл (Tiger1025)',white);
        Eng.ShowText(60,290,'Б.Дмитрий',white);
        Eng.ShowText(40,height-50,'Для конкурса 81 на IGDC.RU',green);
      end;
    end;
  MPred:
    begin
      for i:=1 to ks do
      begin
        Eng.ShowText(20,20*i,tx[i],white);
      end;
      Eng.ShowText(width-200,height-50,'Продолжить',lime);
    end;
  MGame:
    begin
        DrawStat;
        Eng.ShowText(30,550,'Меню',white);
    end;
  MLoad:
    begin
      for i:=1 to 50 do
      begin
        Eng.ShowText(20,20*i,ty[i],white);
      end;
    end;     
  end;
end;

procedure Update;
var f: TextFile; aaa,i: integer;
begin
  tim:=tim+0.01;
  ma:=ma+1;
  if Mode<>OldMode then
  begin
    OldMode:=Mode;
    tim:=0;
    k:=0;
    if Mode=MMenu then begin Audio.play(1); Audio.stop(12); end;
    if Mode=MGame then begin Audio.stop(1); Audio.play(12); end;
    if (Mode=MLoad) then
      if level<6 then
      begin
        Assign(F,'res\cfg\b'+IntToStr(level)+'.ini');
        Reset(f);
        aaa:=1;
        while not eof(f) do
          begin
            ReadLn(F,ty[aaa]);
            aaa:=aaa+1;
          end;
          for i:=aaa to 50 do ty[aaa]:=' ';
          ty[aaa+2]:='[Далее...]';
        Close(f);
      end;
  end;
  case mode of
  MLoad: if M_Click(1) then begin if level<5 then Mode:=MGame else begin Mode:=MMenu; level:=1; SaveCFG; end; end;
  MOut: if tim>5 then Mode:=MMenu;
  MLogo: begin if tim>10 then Mode:=MMenu; end;
  MGame:
    begin
      steep;
      if JSMEngine.M_Click(1) then
        begin
        k:=1;
        if (mouse.x<150) and (mouse.y>550) then Mode:=MMenu;
        end;
      if k>0 then shoot;
      if gameMode=GMAuto then
        begin
          if tim>20 then
           begin
           tim:=0;
          AddBot(-100,300,2);
          end;
        end;

          if K_DOWNA(KeyUP) or K_DOWNA(KeyW) then gamerMove(0.5);
          if K_DOWNA(KEYDOWN) or K_DOWNA(KEYS) then GAMERmove(-0.3);

    end;
  MPred:
    begin
      if tim>2 then begin if ks<100 then ks:=ks+1; tim:=0; end;
      if M_Click(1) then begin Mode:=MGame; loadMap; end;
    end;  
  MMenu:
    begin
       if tim>0.5 then
       begin
       if JSMEngine.K_DOWNA(KeyUP) then if menupos=1 then menupos:=4 else menupos:=menupos-1;
       if JSMEngine.K_DOWNA(KeyDown) then if menupos=4 then menupos:=1 else menupos:=menupos+1;
       tim:=0;
       end;
       if JSMEngine.K_DOWNA(KeyEnter) or M_Click(1) then
         case menupos of
         1: if (level<=4) and (level>1) then begin Mode:=MGame; LoadCFG; LoadMap; end;
         2: begin Mode:=MPred; level:=1; SaveCFG; loadCFG; end;
         4:   Eng.CloseEngine;
         end;
       if mouse.Y>Height-250+80 then menupos:=1;
       if mouse.Y>Height-250+110 then menupos:=2;
       if mouse.Y>Height-250+140 then menupos:=3;
       if mouse.Y>Height-250+170 then menupos:=4;
    end;  
  end;
  mouse.x:=M_X;
  mouse.y:=M_Y;
  if k>0 then k:=k-1;
end;

procedure Create;
var f: TextFile;  i: integer; s: string;
begin
  logo1:=r2d.LoadTexture('res\tex\logo0.jpg',0,0);
  logo2:=r2d.LoadTexture('res\tex\logo1.jpg',0,0);
  im[1]:=r2d.LoadTexture('res\tex\fon.jpg',0,0);
  im[2]:=r2d.LoadTexture('res\tex\st.jpg',0,0);
  im[5]:=r2d.LoadTexture('res\tex\bot1.png',0,0);
  im[6]:=r2d.LoadTexture('res\tex\bot2.png',0,0);
  im[3]:=r2d.LoadTexture('res\tex\win.jpg',0,0);
  im[4]:=r2d.LoadTexture('res\tex\end.png',0,0);
  im[7]:=r2d.LoadTexture('res\tex\auto.png',0,0);
  im[8]:=r2d.LoadTexture('res\tex\wp.png',0,0);
  im[9]:=r2d.LoadTexture('res\tex\at.png',0,0);
  for i:=0 to 24 do mt[i]:=r2d.LoadTexture('res\til\'+IntToStr(i)+'.bmp',0,0);
  gamer:=r2d.LoadTexture('res\tex\pers.png',0,0);
  mo:=r2d.LoadTexture('res\tex\mouse.png',0,0);
  menu_bg:=r2d.LoadTexture('res\tex\menu_bg.jpg',0,0);  
  Audio.inits(0);
  Audio.load(1,'res\mus\intro.ogg',true);
  Audio.load(12,'res\mus\game2.ogg',true);
  if Mode=MLogo then
  begin
  Audio.play(1);
  end;
  au:=2;
  Audio.load(2,'res\mus\wp1_1.wav',false);
  Audio.load(3,'res\mus\wp1_1.wav',false);
  Audio.load(4,'res\mus\wp1_1.wav',false);
  Audio.load(5,'res\mus\wp1_1.wav',false);
  Audio.load(6,'res\mus\wp1_1.wav',false);
  Audio.load(7,'res\mus\wp1_1.wav',false);
  Audio.load(8,'res\mus\wp1_1.wav',false);
  Audio.load(9,'res\mus\wp1_1.wav',false);
  Audio.load(10,'res\mus\wp1_1.wav',false);
  Audio.load(11,'res\mus\wp1_2.wav',false);
  Audio.load(13,'res\mus\wp1_3.wav',false);
  Assign(F,'res\cfg\st.ini');
  Reset(F);
  i:=1;
    while not Eof(F) do
    begin
      Readln(F,s);
      tx[i]:=s;
      i:=i+1;
    end;
  ks:=1;
  Close(f);
  LoadCFG;
  //LoadMap;
end;

begin
  Width:=800; Height:=600;
  CreateEngine ( Eng ); // Create Engine / Создаем движок
  CreateR2D (R2D); // Access to Render / Получаем доступ к выводу графики

  // Ширина, Высота, Хендл, Полный экран либо окно, Показывать курсор, Показывать сплеш, Фильтр.
  // Width, Height, Handle, Fullscreen or Window, Cursor, Splash Screen, Texture Filter.

  Eng.Init ( width, height, 0,true, false, true, F_BILINEAR);
  //Eng.Init ( width, height, 0,false, false, false, F_NONE);

  Create; // Your initializations / Инициализации...

  Eng.RenderProc ( Render ); // Point to Render Procedure / указатель на процедуру рендера Render
  Eng.UpdateProc ( Update ); // Point to Update Procedure / указатель на процедуру логики Update
  Eng.EngineLoop; // Main Loop / Главный цикл
end.
