unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, System.Generics.Collections,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Media;

type
  TForm1 = class(TForm)
    background: TImage;
    BackgroundTimer: TTimer;
    Image1: TImage;
    timelbl: TLabel;
    MediaPlayer1: TMediaPlayer;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    gunshotimg: TImage;
    Image7: TImage;
    enemygen: TTimer;
    enemymove: TTimer;
    shoottimer: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    shotplay: TMediaPlayer;
    shootanim: TTimer;
    Label3: TLabel;
    Label4: TLabel;
    MediaPlayer2: TMediaPlayer;
    bs1: TMediaPlayer;
    bs2: TMediaPlayer;
    bs3: TMediaPlayer;
    bs4: TMediaPlayer;
    Label5: TLabel;
    Label6: TLabel;
    Image8: TImage;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    procedure BackgroundTimerTimer(Sender: TObject);
    procedure enemygenTimer(Sender: TObject);
    procedure enemymoveTimer(Sender: TObject);
    procedure shoottimerTimer(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure shootanimTimer(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Image8Click(Sender: TObject);
  private
    fdmg: integer;
    fspeed: integer;
    fmoney: integer;
    flevel: integer;
    fenemies: TObjectList<TImage>;
    fgameover: boolean;
    fkills: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.BackgroundTimerTimer(Sender: TObject);
var
  ltime: tdatetime;
  s: string;
begin
  if flevel = 11 then
  begin
    Label7.Visible := true;
    Label8.Visible := true;
    Label9.Visible := true;
    enemygen.Enabled := false;
  end;
  Button1.Text := inttostr(fspeed * 1000) + '$';
  Button2.Text := inttostr(fdmg * 1000) + '$';
  Label4.Text := inttostr(fmoney) + '$';
  Label3.Text := 'Волна:' + inttostr(flevel);
  Label1.Text := 'Скорострельность:' + inttostr(fspeed);
  Label2.Text := 'Урон:' + inttostr(fdmg);
  ltime := now;
  s := TimeToStr(ltime);
  delete(s, length(s) - 2, 3);
  timelbl.Text := s;
  if fgameover = false then

    if background.Position.X > -3414 then
      background.Position.X := background.Position.X - (0.5 * flevel)
    else
      background.Position.X := -0;

  if fgameover = false then
  begin
    if Image3.Position.X > -683 then
      Image3.Position.X := Image3.Position.X - (1 * flevel)
    else
      Image3.Position.X := -0;
  end
  else
  begin
    Label5.Visible := true;
    Label6.Visible := true;
    if Image3.Position.X < 0 then
      Image3.Position.X := Image3.Position.X + (1 * flevel)
    else
      Image3.Position.X := -683;
  end;

  if fgameover = false then

    if Image2.Position.X > -800 then
      Image2.Position.X := Image2.Position.X - (4 * flevel)
    else
      Image2.Position.X := -0;
  if fgameover = false then

    if Image4.RotationAngle < 360 then
      Image4.RotationAngle := Image4.RotationAngle + (6 * flevel)
    else
      Image4.RotationAngle := 0;
  if fgameover = false then
    if Image5.RotationAngle < 360 then
      Image5.RotationAngle := Image5.RotationAngle + (6 * flevel)
    else
      Image5.RotationAngle := 0;

  if (MediaPlayer1.State = TMediaState.Stopped) and
    (MediaPlayer1.CurrentTime < (MediaPlayer1.Duration - 10)) then
  begin
    MediaPlayer1.CurrentTime := 0;
    MediaPlayer1.Play;
  end;

end;

function CloneComponent(aSource: TComponent): TComponent;
var
  mem: TMemoryStream;
begin
  mem := TMemoryStream.Create;
  try
    mem.WriteComponent(aSource);
    mem.Seek(0, soFromBeginning);
    Result := mem.ReadComponent(nil);
  finally
    mem.free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if fmoney > (fspeed * 1000) then
  begin
    fmoney := fmoney - (fspeed * 1000);
    Inc(fspeed);
    if fspeed = 9 then
      Button1.Visible := false;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if fmoney > (fdmg * 1000) then
  begin
    fmoney := fmoney - (fdmg * 1000);
    Inc(fdmg);
    if fspeed = 5 then
      Button1.Visible := false;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  fgameover := true;
end;

procedure TForm1.enemygenTimer(Sender: TObject);
var
  limg: TImage;
  i: integer;
begin
  if fgameover then
    exit;
  enemygen.Enabled := true;
  randomize();
  i := round((random()) * 10);
  enemygen.Interval := 500 + (i * 100);
  if i > flevel then
    exit;

  limg := TImage.Create(self);
  limg.Parent := Form1;
  limg.Position.X := Image7.Position.X;
  limg.Position.y := Image7.Position.y;
  limg.Height := Image7.Height;
  limg.Width := Image7.Width;

  limg.Bitmap.Assign(Image7.MultiResBitmap.Bitmaps[1]);
  limg.Visible := true;

  fenemies.Add(limg);

end;

procedure TForm1.enemymoveTimer(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to fenemies.Count - 1 do
  begin
    if fgameover = false then
    begin
      fenemies.Items[i].Position.X := fenemies.Items[i].Position.X -
        (1 * flevel);
      fenemies.Items[i].Visible := true;
    end
    else
    begin
      if fenemies.Items[i].Position.X < 900 then

        fenemies.Items[i].Position.X := fenemies.Items[i].Position.X +
          (1 * flevel);
    end;
    if fenemies.Items[i].RotationAngle > -80 then
      if fenemies.Items[i].Position.X < -100 then
      begin
        if fgameover = false then
        begin
          bs3.CurrentTime := 0;
          bs3.Play;
        end;

        fgameover := true;
      end;

  end;
  enemymove.Enabled := true;
end;

procedure TForm1.Image8Click(Sender: TObject);
begin

  fkills := 0;
  fgameover := false;
  fdmg := 1;
  fspeed := 1;
  flevel := 1;
  MediaPlayer1.Play;
  gunshotimg.Visible := false;
  fenemies := TObjectList<TImage>.Create;
  shoottimer.Enabled := true;
  enemygen.Enabled := true;
  enemymove.Enabled := true;
  BackgroundTimer.Enabled := true;
  Image8.Visible := false;
end;

procedure TForm1.Label1Click(Sender: TObject);
begin
  Inc(fspeed);
end;

procedure TForm1.Label2Click(Sender: TObject);
begin
  Inc(fdmg);
end;

procedure TForm1.shootanimTimer(Sender: TObject);
begin
  gunshotimg.Visible := false;
  shootanim.Enabled := false;
end;

procedure TForm1.shoottimerTimer(Sender: TObject);
var
  i, j: integer;
begin
  for i := 0 to fenemies.Count - 1 do
    if fenemies.Items[i].RotationAngle > -80 then
    begin
      shotplay.Stop;
      shotplay.CurrentTime := 0;
      shotplay.Play;
      shootanim.Enabled := true;
      gunshotimg.Visible := true;
      Application.ProcessMessages;
      fenemies.Items[i].RotationAngle := fenemies.Items[i].RotationAngle -
        (10 * (10 - flevel) * 0.8 * (fdmg * 0.5));
      fenemies.Items[i].Position.y := fenemies.Items[i].Position.y +
        (10 * (10 - flevel) * 0.5 * (fdmg * 0.5));
      shoottimer.Interval := (10 - fspeed) * 100;

      if not(fenemies.Items[i].RotationAngle > -80) then
      begin
        fenemies.Items[i].RotationAngle := -81;
        fenemies.Items[i].Position.y := 200;
        Inc(fkills);
        fmoney := fmoney + 100;
      end;
      if fkills > flevel * 5 then
      begin
        fmoney := fmoney + 500;
        Inc(flevel);
        fkills := 0;
        randomize();
        j := round(random() * 6);
        if j = 1 then
        begin
          bs1.CurrentTime := 0;
          bs1.Play;
        end;
        if j = 2 then
        begin
          bs2.CurrentTime := 0;
          bs2.Play;
        end;
        if j = 3 then
        begin
          bs4.CurrentTime := 0;
          bs4.Play;
        end;

      end;
      shoottimer.Enabled := true;
      exit;
    end;
  shoottimer.Enabled := true;
end;

end.
