(() => {
  const picId = 1;
  const loopFrameCount = 8;
  const ejaculationFrameCount = 10;
  const loopWaits = [6, 4, 3];

  let currentFrame = 0;
  let wait = 0;
  let speedIndex = 0;
  let state = 'piston';
  let isFinished = false;

  // キャラクターごとのベース名を自動生成
  const loopBaseNames = [];
  const ejaculationBaseNames = [];
  for (let i = 0; i < 10; i++) {
    const letter = String.fromCharCode(65 + i); // A〜J
    loopBaseNames.push(`girl${letter}_`);
    ejaculationBaseNames.push(`girl${letter}_2s_`);
  }

  function getCharacterIndex() {
    const v = $gameVariables.value(11);
    return Math.max(0, Math.min(9, v - 1)); // 1=girlA, 10=girlJ
  }

  function showPicture(baseName, frame, scale = 4) {
    const filename = baseName + String(frame + 1).padStart(2, '0');
    const x = Graphics.width / 2;
    const y = Graphics.height / 2 - 25 * scale;
    $gameScreen.showPicture(
      picId,
      filename,
      1,
      x,
      y,
      100 * scale,
      100 * scale,
      255,
      0
    );
  }

  function erasePicture() {
    $gameScreen.erasePicture(picId);
  }

  const _Scene_Map_update = Scene_Map.prototype.update;
  Scene_Map.prototype.update = function () {
    _Scene_Map_update.call(this);
    if (isFinished) return;

    const charIndex = getCharacterIndex();
    const loopBaseName = loopBaseNames[charIndex];
    const ejaculationBaseName = ejaculationBaseNames[charIndex];

    if (!$gameSwitches.value(1)) {
      erasePicture();
      currentFrame = 0;
      wait = 0;
      speedIndex = 0;
      state = 'piston';
      isFinished = false;
      return;
    }

    if (state === 'piston') {
      speedIndex = Number($gameVariables.value(10)) || 0;
      speedIndex = Math.min(Math.max(speedIndex, 0), loopWaits.length - 1);

      wait++;
      if (wait >= loopWaits[speedIndex]) {
        wait = 0;
        showPicture(loopBaseName, currentFrame);
        currentFrame++;
        if (currentFrame >= loopFrameCount) currentFrame = 0;
      }

      if ($gameVariables.value(13) === 1) {
        state = 'ejaculation';
        currentFrame = 0;
        wait = 0;
        $gameScreen.startFlash([255, 255, 255, 255], 30);
      }

    } else if (state === 'ejaculation') {
      wait++;
      if (wait >= 7) {
        wait = 0;
        if (currentFrame < ejaculationFrameCount) {
          showPicture(ejaculationBaseName, currentFrame);
          currentFrame++;
        } else {
          currentFrame = ejaculationFrameCount - 1;
          showPicture(ejaculationBaseName, currentFrame);
          state = 'ejaculationEnd';
        }
      }

    } else if (state === 'ejaculationEnd') {
      // 待機中（タップ入力を無視）
    }
  };

  function onClickEnd() {
    if (state === 'ejaculationEnd') {
      erasePicture();
      isFinished = true;
      $gameSwitches.setValue(1, false);
      SceneManager.goto(Scene_Map);
    }
  }

  const _Sprite_Picture_loadBitmap = Sprite_Picture.prototype.loadBitmap;
  Sprite_Picture.prototype.loadBitmap = function () {
    _Sprite_Picture_loadBitmap.call(this);
    if (this._pictureName) {
      this.bitmap.smooth = false;
    }
  };
})();
