-- 打砖块 —— 用 **Ada** 写的手机小游戏
-- Breakout -- a small phone game written in **Ada**
--
-- 玩法（与 Examples/c/app/breakout.c / Examples/python/app/breakout.py 同一套）：
--   8 列 × 5 行砖块，球撞到砖就消掉 +10 分；挡板跟手指，球掉下去扣一条命，
--   三条命用完结束；清完所有砖算通关；结束后点屏幕重开。
--
-- ## 本语言的实测约束（见 Examples/ada/app/stopwatch.adb 文件头）
--
-- 跑法 —— 手机：vml run examples/ada/breakout.adb
-- 跑法 —— 桌面：vmlcli Examples/ada/breakout.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Breakout is
   type Brick_Arr is array (1 .. 40) of Integer;
   Brick : Brick_Arr;
   K : Integer := 0;
   W : Integer := 360;
   H : Integer := 620;
   Bw : Integer := 45;
   Bh : Integer := 18;
   Padw : Integer := 70;
   Padh : Integer := 12;
   Ballr : Integer := 6;
   Padx : Integer := 145;
   Bx : Integer := 180;
   By : Integer := 400;
   Bvx : Integer := 3;
   Bvy : Integer := -3;
   Score : Integer := 0;
   Lives : Integer := 3;
   Won : Integer := 0;
   Dead : Integer := 0;
   Left : Integer := 40;
   Frames : Integer := 0;
   I : Integer := 0;
   Row : Integer := 0;
   Px : Integer := 0;
   Py : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Pady : Integer := 0;
   Hit : Integer := 0;
   Col : Integer := 0;
   Lang : Integer := 0;
begin
   Lang := ui_scr_get_language();
   K := ui_win_open(0, 0, 0);
   K := ui_scr_set_keep_on(1);
   W := ui_scr_get_w();
   H := ui_scr_get_h();
   if W <= 0 then
      W := 360;
   end if;
   if H <= 0 then
      H := 620;
   end if;
   Bw := W / 8;
   Bh := H / 34;
   if Bh < 12 then
      Bh := 12;
   end if;
   Padw := W / 5;
   if Padw < 40 then
      Padw := 40;
   end if;
   Padh := H / 50;
   if Padh < 8 then
      Padh := 8;
   end if;
   Ballr := W / 60;
   if Ballr < 4 then
      Ballr := 4;
   end if;
   Padx := W / 2;
   Bx := W / 2;
   By := H - 120;

   I := 1;
   while I <= 40 loop
      Brick(I) := 1;
      I := I + 1;
   end loop;

   Tid := ui_timer_set(16, 0);

   while ui_win_is_closed() = 0 loop
      if Won = 0 and Dead = 0 then
         Bx := Bx + Bvx;
         By := By + Bvy;
         if Bx < Ballr then
            Bx := Ballr;
            Bvx := 0 - Bvx;
         end if;
         if Bx > W - Ballr then
            Bx := W - Ballr;
            Bvx := 0 - Bvx;
         end if;
         if By < Ballr then
            By := Ballr;
            Bvy := 0 - Bvy;
         end if;
         Pady := H - 70;
         if Bvy > 0 and By + Ballr >= Pady and By - Ballr <= Pady + Padh then
            if Bx >= Padx - Padw / 2 and Bx <= Padx + Padw / 2 then
               By := Pady - Ballr;
               Bvy := 0 - Bvy;
            end if;
         end if;
         I := 1;
         Hit := 0;
         while I <= 40 and Hit = 0 loop
            if Brick(I) /= 0 then
               Px := ((I - 1) mod 8) * Bw;
               Py := 70 + ((I - 1) / 8) * Bh;
               if Bx + Ballr >= Px and Bx - Ballr <= Px + Bw and By + Ballr >= Py and By - Ballr <= Py + Bh then
                  Brick(I) := 0;
                  Left := Left - 1;
                  Score := Score + 10;
                  Bvy := 0 - Bvy;
                  Hit := 1;
                  if Left = 0 then
                     Won := 1;
                  end if;
               end if;
            end if;
            I := I + 1;
         end loop;
         if By > H + Ballr then
            Lives := Lives - 1;
            if Lives <= 0 then
               Dead := 1;
            end if;
            Bx := W / 2;
            By := H - 120;
            Bvx := 3;
            Bvy := -3;
         end if;
      end if;

      K := ui_2d_clear(-15724520);
      I := 1;
      while I <= 40 loop
         if Brick(I) /= 0 then
            Row := (I - 1) / 8;
            Px := ((I - 1) mod 8) * Bw;
            Py := 70 + Row * Bh;
            if Row = 0 then
               Col := -2069424;
            elsif Row = 1 then
               Col := -2509750;
            elsif Row = 2 then
               Col := -11409298;
            elsif Row = 3 then
               Col := -11890471;
            else
               Col := -6591248;
            end if;
            K := ui_2d_rect(Px + 1, Py + 1, Bw - 2, Bh - 2, Col, 1, 0, 3);
         end if;
         I := I + 1;
      end loop;
      K := ui_2d_rect(Padx - Padw / 2, H - 70, Padw, Padh, -11409298, 1, 0, 4);
      K := ui_2d_circle(Bx, By, Ballr, -1513232, 1, 0);

      K := ui_2d_text(10, 20, Integer'Image(Score), -1513232, 15, 0);
      if Lang = 0 then
         K := ui_2d_text(10, 46, "得分", -6643536, 10, 0);
      else
         K := ui_2d_text(10, 46, "score", -6643536, 10, 0);
      end if;
      K := ui_2d_text(W - 10, 20, Integer'Image(Lives), -2069424, 16, 2);
      if Lang = 0 then
         K := ui_2d_text(W - 10, 46, "命", -6643536, 10, 2);
      else
         K := ui_2d_text(W - 10, 46, "lives", -6643536, 10, 2);
      end if;

      if Won /= 0 then
         K := ui_2d_text(W / 2, H / 2, "全打光了！点一下再来", -1513232, 18, 1);
      elsif Dead /= 0 then
         K := ui_2d_text(W / 2, H / 2, "没命了，点一下再来", -2069424, 18, 1);
      else
         K := ui_2d_text(W / 2, H - 34, "点 / 拖屏幕控制挡板", -6643536, 11, 1);
      end if;
      K := ui_2d_present();

      T := ui_msg_fetch_wait(0);
      if T = 10 then
         exit;
      end if;

      if T = 6 or T = 4 then
         Tx := ui_msg_get_a();
         if Won /= 0 or Dead /= 0 then
            I := 1;
            while I <= 40 loop
               Brick(I) := 1;
               I := I + 1;
            end loop;
            Left := 40;
            Score := 0;
            Lives := 3;
            Won := 0;
            Dead := 0;
            Bx := W / 2;
            By := H - 120;
         else
            Padx := Tx;
            if Padx < Padw / 2 then
               Padx := Padw / 2;
            end if;
            if Padx > W - Padw / 2 then
               Padx := W - Padw / 2;
            end if;
         end if;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            exit;
         end if;
         if I = 37 then
            Padx := Padx - 24;
         end if;
         if I = 39 then
            Padx := Padx + 24;
         end if;
         if Padx < Padw / 2 then
            Padx := Padw / 2;
         end if;
         if Padx > W - Padw / 2 then
            Padx := W - Padw / 2;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 20000 then
            exit;
         end if;
      end if;
   end loop;

   K := ui_timer_kill(Tid);
   K := ui_scr_set_keep_on(0);
   K := ui_win_close();
end Breakout;
