unit uGame;

interface
uses
  zglHeader, uScene, uMap,
  LexUtils, uList, uParticle,
  uGun, uBullet, uZepelin,
  uBomb, uBoom, uWinPage, uGamePage;

const
  BonusCount = 70;

type
  TGame = class(TScene)
    Map  : TMap;
    Gun1 : TGun;
    Guns : TGunList;
    Bullets : TBulletList;
    Bombs : TBombList;
    Zepelins : TZepelinList;
    Booms : TBoomList;

    LosePage,
    WinPage : TGamePage;
    Particles : TParticles;

    procedure   Start;
    procedure   Draw; override;
    procedure   Update; override;
    constructor Create(const SceneMng : tSceneMng); override;
    constructor Free;
    procedure   SetParam(Param: Integer); override;
  end;

implementation
uses
  uMaterials, uConst, uMyApp;
{ TGame }

constructor TGame.Create;
begin
  inherited;
  Map := TMap.Create(WinW, WinH);
  Bombs := TBombList.Create(Map);
  Zepelins := TZepelinList.Create(Map, Bombs);

  Booms := TBoomList.Create(Bombs, Zepelins);
  Bullets := TBulletList.Create(Map, Booms);
  Guns := TGunList.Create(Map, Bullets);

  Gun1 := Guns.Add(TGun.Create()) as TGun;



  WinPage  := TWinPage.Create(Self);
  LosePage := TLosePage.Create(Self);

  Particles := TParticles.Create;
  Map.PParticles := Particles;
end;

procedure TGame.Draw;
begin
  Map.Draw;
  Bombs.Next.Draw;

  Bullets.Next.Draw;
  Guns.Next.Draw;

  Zepelins.Next.Draw;

  Booms.Next.Draw;

  Particles.Draw;

  LosePage.Draw;
  WinPage.Draw;

  pr2d_Rect(WinW/2 - 200, 15 - 8, 400, 38, $353535, 200, PR2D_FILL);
  pr2d_Rect(WinW/2 - 200, 15 - 8, 400, 38, $FFFFFF, 200);
  text_Draw( mFont_1, WinW/2, 15,
  '   Врагов = ' + INt2S(Zepelins.Count) +
  '   Очки = ' + INt2S(Points)
  , TEXT_HALIGN_CENTER );
end;

constructor TGame.Free;
begin

end;

procedure TGame.SetParam(Param: Integer);
begin
  Start;
end;

procedure TGame.Start;
var
  I : Integer;
begin
  Map.Start;
  Bombs.Clear;
  Zepelins.Clear;

  Booms.Clear;
  Bullets.Clear;

  Particles.Update;

  for I := 1 to 12 + random(5) do
    Zepelins.Add(TZepelin.Create);

  LosePage.Enabled := false;
  WinPage.Enabled := false;
end;

procedure TGame.Update;
var
  I : Integer;
  X, Y : Integer;
begin
  Guns.Next.Update;
  Bullets.Next.Update;
  Zepelins.Next.Update;
  Bombs.Next.Update;
  Booms.Next.Update;
  Particles.Update;

  if WinPage.NoActive then
  if LosePage.NoActive then
  begin
    if Zepelins.Count <= 0 then
      WinPage.Show;

    if Map.BunkerNoLive then
      LosePage.Show;

    if mouse_Click(M_BLEFT) then
    begin
      Gun1.MouseWork(mouse_X, mouse_Y);
    end;
  end;

  if key_Press(K_ESCAPE) then
  with (PSceneMng as TMyApp) do
    ChangeScene(MainMenu, 0);
end;

end.
