unit uMain;

interface

uses zglHeader,
     uVAr,
     uAdd,
     uTeam,
     uEnemy,
     LightEngine2D,
     uSmplPart,
     uBlood;

procedure GameInit;
procedure GameDraw;
procedure GameUpd;
procedure GameFree;

implementation

procedure GameInit;
var i: integer;
begin

  zgl_Disable( COLOR_BUFFER_CLEAR );
  Snd_Init;

  Camera.X:=0;
  Camera.Y:=0;
  Camera.Angle:=0;
  Camera.Zoom.X:=1;
  Camera.Zoom.Y:=1;
  Cam2d_Set( @Camera );
  // инициируем свет

  le_InitLightEngine(le_ColorRGB(0.7),'data/sys/Penumbra.png',32,32);
 // le_CreateLightSource(le_v(100, 300), 300, le_ColorRGB(0,1,0)) ;

  Light:=le_CreateLightSource(le_v(500, 500), 330, le_ColorRGB(1,1,1));
  Light.sAngle:=50*deg2rad/2;
  Light.sSoft:=15*deg2rad;
  Light.pRadius:=40;

  Light2:=le_CreateLightSource(le_v(0, 0), 330, le_ColorRGB(1,1,1));
  Light2.sAngle:=50*deg2rad/2;
  Light2.sSoft:=15*deg2rad;
  Light2.pRadius:=40;

  Light3:=le_CreateLightSource(le_v(0, 0), 330, le_ColorRGB(1,1,1));
  Light3.sAngle:=50*deg2rad/2;
  Light3.sSoft :=15*deg2rad;
  Light3.pRadius:=40;

  l1 := le_CreateLightSource(le_v(0, 0), 128, le_ColorRGB(1, 0.8, 0.4));
  l2 := le_CreateLightSource(le_v(0, 0), 128, le_ColorRGB(1, 0.8, 0.4));
  l3 := le_CreateLightSource(le_v(0, 0), 128, le_ColorRGB(1, 0.8, 0.4));

  fnt     := font_LoadFromFile( dirGui + 'Impact-Regular-14pt.zfi.zfi');
  texBtn  := tex_LoadFromFile( dirGui + 'button.png');
  texBLt  := tex_LoadFromFile( dirGui + 'button_light.png');

  texTile := tex_LoadFromFile( dirRes + 'back_menu.png');
  texLght := tex_LoadFromFile( dirRes + 'light.png');
  texDirt := tex_LoadFromFile( dirMap + 'dirt.jpg');
  texMan  := tex_LoadFromFile( dirRes + 'Men_Pistol.png');
  texBllt := tex_LoadFromFile( dirRes + 'BShot.png');
  texTrace:= tex_LoadFromFile( dirRes + 'bullet_trace.png');
  texRadar:= tex_LoadFromFile( dirRes + 'radar.png');

  texEn1  := tex_LoadFromFile( dirRes + 'monst1.png');
  texEn2  := tex_LoadFromFile( dirRes + 'monst2.png');
  tex_SetFrameSize(texEn1, 128, 128);
  tex_SetFrameSize(texEn2, 64, 64);

  sndShoot  := Snd_loadfromfile( dirSnd + 'Shoot2.wav');
  sndShoot2 := Snd_loadfromfile( dirSnd + 'MP5.wav');
  sndShoot3 := Snd_loadfromfile( dirSnd + 'P1.wav');

  texBlood := tex_LoadFromFile (dirRes + 'blood02.png');
  rt := rtarget_Add(tex_CreateZero(512, 512, $0606e3), RT_FULL_SCREEN or RT_CLEAR_DEPTH);

  team.hp := 1000;

    with team.members[1] do
    begin
      pos.X := 300 + 1* 50;
      pos.Y := 300;
      ang   := 0;
      rpm   := 40;
      shots := 12;
      max_shots := 12;
      reload:= 60;
      wpnID := 2;
    end;

    with team.members[2] do
    begin
      pos.X := 300 + 2* 50;
      pos.Y := 300;
      ang   := 0;
      rpm   := 40;
      shots := 12;
      max_shots := 12;
      reload:= 60;
      wpnID := 2;
    end;

    with team.members[3] do
    begin
      pos.X := 300 + 3* 50;
      pos.Y := 300;
      ang   := 0;
      rpm   := 10;
      shots := 25;
      max_shots := 25;
      reload:= 60;
      wpnID := 2;
    end;

{ for I := 1 to 1 do
   begin
     enemies[i].exist := true;
     enemies[i].pos.x := 350 + random(400) * m_cos( i * 35 );
     enemies[i].pos.y := 300 + random(400) * m_sin( i * 35 );
     enemies[i].HP := 10;
     enemies[i].eType := 1;
   end;          }
end;

procedure GameDraw;
var i, j : integer;
begin
   case gs of
     gsMenu:
       begin
         SSprite2d_Draw(texTile, 0, 0, 800, 600, 0);
         SSprite2d_draw(texBtn, 400 - 128, 300, 256, 64, 0);
         text_draw(fnt, 400, 325, 'Новая игра', TEXT_HALIGN_CENTER);
         SSprite2d_draw(texBtn, 400 - 128, 360, 256, 64, 0);
         text_draw(fnt, 400, 385, 'Авторы', TEXT_HALIGN_CENTER);
         SSprite2d_draw(texBtn, 400 - 128, 420, 256, 64, 0);
         text_draw(fnt, 400, 445, 'Выход', TEXT_HALIGN_CENTER);
         fx_SetBlendMode(FX_BLEND_ADD);
           if col2d_PointInRect(Mouse_X, Mouse_Y, r( 300, 300, 200, 60)) then
              SSprite2d_draw(texBLt, 400 - 128, 300, 256, 64, 0);
           SSprite2d_draw(texLght, 256, - 512, 1024, 1024, a);
         fx_SetBlendMode(FX_BLEND_Normal);
       end;
     gsGame:
       begin
         SSprite2d_Draw(texDirt, 0, 0, 1024, 1024, 0);
         DrawBlood;
         teamRender;
         enRender;
         DrawParts;
         // рисуем свет и тени
         if lightON then
            begin
              le_EachLightSource(@le_RenderLight);
              le_RenderShadows;
            end;
      //   le_EachLightSource(@le_DrawLightBB);

         SSprite2d_draw(texRadar, 0, 450, 128, 128, 0, 192);
         for I := 1 to 256 do
           if enemies[i].exist then
              if m_distance(team.members[1].pos.x,
                            team.members[1].pos.y,
                            enemies[i].Pos.x,
                            enemies[i].pos.Y) < 500 then
                            pr2d_Rect(64 + (enemies[i].pos.X - team.members[1].pos.x)/500 * 64,
                                       450 + 64 + (enemies[i].pos.y - team.members[1].pos.y)/500 * 64,
                                       2, 2,
                                       $ff0000);

          text_draw(fnt, 10, 10, 'Здоровье отряда:' + u_IntToStr(team.hp));
       end;
   end;
end;

procedure GameUpd;
var i: integer;
begin
   case gs of
     gsMenu:
       begin
         inc(a, 2);
       end;
     gsGame:
       begin
         inc(a);
         le_DarkColor := le_ColorRGB(0.7 - a*0.0001);
         teamUpdate;
         enUpdate;
         bulletupd;
         UpdParts;
         if lightON then
            begin
              le_Update;
            end;
       end;
   end;
end;

procedure GameFree;
begin
  le_EachLightSource( @le_FreeLightSource );
end;

end.
