# 打地鼠 —— 用 **Perl** 写的手机小游戏
# Whack-a-mole — a small phone game written in **Perl**
#
# 玩法（与 Examples/c/app/whack.c / Examples/python/app/whack.py 同一套）：
#   3×3 九个洞，每 14 拍有一个洞冒出地鼠；在它缩回去之前点中 +1 分，
#   超时没点中扣一条命；三条命用完结束；结束后点屏幕重开。
#
# 地鼠的存活时间记在**拍数**上，不是 time() —— 定时器就是心跳。
#
# ## 本语言的实测约束（见 Examples/perl/app/memory.pl 文件头）
#   · `for (my $i = ...)` 编不过 ⇒ 循环变量先 `my`、用 `while`；
#   · 数字上屏用字符串插值；数组 0 基。
#
# 跑法 —— 手机：vml run examples/perl/whack.pl
# 跑法 —— 桌面：vmlcli Examples/perl/whack.pl --screen 480x640 --frames /tmp/fr

use vml_ui;

my $k = 0;
my $lang = 0;
my $w = 360;
my $h = 620;
my $cell = 120;
my $ox = 0;
my $oy = 130;
my $mole = -1;
my $molett = 0;
my $wait = 6;
my $score = 0;
my $lives = 3;
my $over = 0;
my $flash = 0;
my $frames = 0;
my $seed = 20267;
my $i = 0;
my $x = 0;
my $y = 0;
my $cx = 0;
my $cy = 0;
my $r = 0;
my $t = 0;
my $tx = 0;
my $ty = 0;
my $idx = 0;
my $key = 0;
my $tid = 0;
my $hud = "";
my $hud2 = "";
my $pad = 0;
my $top = 0;
my $bottom = 0;
my $avail = 0;
my $side = 0;
my $hintSize = 12;
my $overSize = 16;

$lang = ui_scr_get_language();
if ($lang == 0) { $k = ui_win_open("打地鼠", 0, 0); }
if ($lang != 0) { $k = ui_win_open("Whack", 0, 0); }

$w = ui_scr_get_w();
$h = ui_scr_get_h();
if ($w <= 0) { $w = 360; }
if ($h <= 0) { $h = 620; }
# 顶部计分 / 底部提示先占掉固定份，3×3 洞位取宽高较小的一边，方屏也铺满可用区。
$pad = int($w / 60);
if ($pad < 8) { $pad = 8; }
$top = 62;
$bottom = 38;
$avail = $h - $top - $bottom;
if ($avail < 30) { $avail = 30; }
$side = $w - 2 * $pad;
if ($avail < $side) { $side = $avail; }
$cell = int($side / 3);
if ($cell < 8) { $cell = 8; }
$ox = int(($w - 3 * $cell) / 2);
$oy = $top + int(($avail - 3 * $cell) / 2);
$overSize = 16;
$hintSize = 12;
if ($w < 360) { $overSize = 13; $hintSize = 10; }

$k = ui_scr_set_keep_on(1);
$tid = ui_timer_set(50, 0);

while (ui_win_is_closed() == 0) {

  # ── draw ──
  $k = ui_2d_clear(-15724520);
  $i = 0;
  while ($i < 9) {
    $cx = $ox + ($i % 3) * $cell + int($cell / 2);
    $cy = $oy + int($i / 3) * $cell + int($cell / 2);
    $r = int($cell / 3);
    $k = ui_2d_ellipse($cx, $cy + int($r / 3), $r + int($cell / 12), int($r / 2) + int($cell / 20), -15066588, 1, 0);
    if ($i == $mole) {
      if ($flash == 1) {
        $k = ui_2d_circle($cx, $cy, $r, -1, 1, 0);
      } else {
        $k = ui_2d_circle($cx, $cy, $r, -2509750, 1, 0);
      }
      $k = ui_2d_circle($cx - int($r / 3), $cy - int($r / 4), int($r / 5), -15724520, 1, 0);
      $k = ui_2d_circle($cx + int($r / 3), $cy - int($r / 4), int($r / 5), -15724520, 1, 0);
    }
    $i = $i + 1;
  }

  $hud = sprintf("%d", $score);
  $k = ui_2d_text(10, 20, $hud, -1513232, 18, 0);
  if ($lang == 0) { $k = ui_2d_text(10, 48, "得分", -6643536, 10, 0); }
  if ($lang != 0) { $k = ui_2d_text(10, 48, "score", -6643536, 10, 0); }
  $hud2 = sprintf("%d", $lives);
  $k = ui_2d_text($w - 10, 20, $hud2, -2069424, 18, 2);
  if ($lang == 0) { $k = ui_2d_text($w - 10, 48, "命", -6643536, 10, 2); }
  if ($lang != 0) { $k = ui_2d_text($w - 10, 48, "lives", -6643536, 10, 2); }

  if ($over == 1) {
    if ($lang == 0) { $k = ui_2d_text(int($w / 2), $h - 30, "结束，点一下再来", -1513232, $overSize, 1); }
    if ($lang != 0) { $k = ui_2d_text(int($w / 2), $h - 30, "Over - tap to retry", -1513232, $overSize, 1); }
  } else {
    if ($lang == 0) { $k = ui_2d_text(int($w / 2), $h - 30, "冒头就点它", -6643536, $hintSize, 1); }
    if ($lang != 0) { $k = ui_2d_text(int($w / 2), $h - 30, "Tap the mole when it pops", -6643536, $hintSize, 1); }
  }
  $k = ui_2d_present();

  $t = ui_msg_fetch_wait(0);
  if ($t == 10) { last; }

  if ($t == 9) {
    $frames = $frames + 1;
    if ($frames >= 6000) { last; }
    if ($over == 0) {
      $flash = 0;
      if ($mole >= 0) {
        $molett = $molett - 1;
        if ($molett <= 0) {
          $mole = -1;
          $lives = $lives - 1;
          $wait = 8;
          if ($lives <= 0) { $over = 1; }
        }
      } else {
        $wait = $wait - 1;
        if ($wait <= 0) {
          $seed = ($seed * 1103 + 12345) % 32768;
          $mole = int($seed / 256) % 9;
          $molett = 14;
        }
      }
    }
  }

  if ($t == 6 || $t == 4) {
    $tx = ui_msg_get_a();
    $ty = ui_msg_get_b();
    if ($over == 1) {
      $score = 0;
      $lives = 3;
      $over = 0;
      $flash = 0;
      $mole = -1;
      $wait = 6;
      $frames = 0;
    } else {
      if ($tx >= $ox && $ty >= $oy) {
        $x = int(($tx - $ox) / $cell);
        $y = int(($ty - $oy) / $cell);
        if ($x >= 0 && $y >= 0 && $x < 3 && $y < 3) {
          $idx = $y * 3 + $x;
          if ($idx == $mole) {
            $score = $score + 1;
            $mole = -1;
            $seed = ($seed * 1103 + 12345) % 32768;
            $wait = 4 + int($seed / 256) % 4;
            $flash = 1;
          }
        }
      }
    }
  }

  if ($t == 1) {
    $key = ui_msg_get_a();
    if ($key == 27) { last; }
    if ($key == 82) {
      $score = 0;
      $lives = 3;
      $over = 0;
      $mole = -1;
    }
  }
}

$k = ui_timer_kill($tid);
$k = ui_scr_set_keep_on(0);
$k = ui_win_close();
