MineFun.io 模組選單

模組選單

// ==UserScript==
// @name        MineFun.io 模組選單
// @match       *://minefun.io/*
// @grant       none
// @version     1.0a
// @author      HUANG WEI-LUN HUANG WEI-LUN
// @description 模組選單
// @namespace http://tampermonkey.net/
// ==/UserScript==
// --- 模組選單 HTML ---
const menuHTML = `
  <div id="moduleMenu" style="
    position: fixed; top: 20px; right: 20px; width: 200px; background: rgba(0,0,0,0.75);
    color: white; font-family: Arial, sans-serif; font-size: 14px; padding: 10px; border-radius: 8px; z-index: 9999;
  ">
    <h3 style="margin-top:0; margin-bottom:10px; font-size:16px;">模組選單</h3>
    <label><input type="checkbox" id="autoLockToggle" checked> 自動鎖頭</label><br>
    <label><input type="checkbox" id="showEnemyToggle" checked> 顯示敵人點位</label><br>
    <label><input type="checkbox" id="invisibleToggle"> 隱身</label><br>
  </div>
`;

// 插入選單到頁面(避免重複)
if (!document.getElementById('moduleMenu')) {
  const div = document.createElement('div');
  div.innerHTML = menuHTML;
  document.body.appendChild(div.firstElementChild);
}

// --- 狀態變數 ---
let autoLockEnabled = true;
let showEnemyEnabled = true;
let invisibleEnabled = false;

// 綁定選單事件
document.getElementById('autoLockToggle').addEventListener('change', e => {
  autoLockEnabled = e.target.checked;
});
document.getElementById('showEnemyToggle').addEventListener('change', e => {
  showEnemyEnabled = e.target.checked;
});
document.getElementById('invisibleToggle').addEventListener('change', e => {
  invisibleEnabled = e.target.checked;
  toggleInvisibility(invisibleEnabled);
});

// 按 M 鍵切換模組選單顯示/隱藏
document.addEventListener('keydown', e => {
  if (e.key === 'm' || e.key === 'M') {
    const menu = document.getElementById('moduleMenu');
    menu.style.display = (menu.style.display === 'none' || !menu.style.display) ? 'block' : 'none';
  }
});

// 假設取得玩家、敵人函式,請根據 minefun.io 實際 API 改寫
function getPlayer() {
  return window.player;
}
function getEnemies() {
  return window.enemies || [];
}

// 自動鎖頭示範
function autoLockHead() {
  if (!autoLockEnabled) return;
  const player = getPlayer();
  const enemies = getEnemies();
  if (!player || enemies.length === 0) return;
  // 簡單找最近敵人
  enemies.sort((a, b) => {
    return (Math.hypot(a.x - player.x, a.y - player.y)) - (Math.hypot(b.x - player.x, b.y - player.y));
  });
  const target = enemies[0];
  if (target) {
    // 假設 player 有 aimAt(x,y) 函數
    window.player.aimAt(target.x, target.y - (target.headHeight || 10));
  }
}

// 顯示敵人點位示範(簡單在頁面畫圓)
function showEnemyPositions() {
  if (!showEnemyEnabled) {
    if (window.enemyOverlayCanvas) {
      window.enemyOverlayCanvas.style.display = 'none';
    }
    return;
  }
  let canvas = window.enemyOverlayCanvas;
  if (!canvas) {
    canvas = document.createElement('canvas');
    canvas.id = 'enemyOverlayCanvas';
    canvas.style.position = 'fixed';
    canvas.style.top = '0';
    canvas.style.left = '0';
    canvas.style.width = '100%';
    canvas.style.height = '100%';
    canvas.style.pointerEvents = 'none';
    canvas.style.zIndex = 9998;
    document.body.appendChild(canvas);
    window.enemyOverlayCanvas = canvas;
  }
  canvas.style.display = 'block';
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  const ctx = canvas.getContext('2d');
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  const player = getPlayer();
  const enemies = getEnemies();
  if (!player || enemies.length === 0) return;

  // 假設世界座標轉螢幕座標函數(需依遊戲調整)
  function worldToScreen(x, y) {
    const cam = window.camera || { x: 0, y: 0, zoom: 1 };
    return {
      x: (x - cam.x) * cam.zoom + window.innerWidth / 2,
      y: (y - cam.y) * cam.zoom + window.innerHeight / 2,
    };
  }

  enemies.forEach(enemy => {
    const pos = worldToScreen(enemy.x, enemy.y);
    ctx.fillStyle = 'red';
    ctx.beginPath();
    ctx.arc(pos.x, pos.y, 8, 0, Math.PI * 2);
    ctx.fill();
  });
}

// 隱身功能示範(需根據遊戲實際控制玩家隱身的方法調整)
function toggleInvisibility(enable) {
  const player = getPlayer();
  if (!player) return;

  if (enable) {
    // 假設 player 有 visible 屬性可控制
    player.visible = false;

    // 如果是 DOM 元素可用:
    // document.getElementById('playerSprite').style.display = 'none';
  } else {
    player.visible = true;

    // document.getElementById('playerSprite').style.display = 'block';
  }
}

// 主迴圈
function mainLoop() {
  autoLockHead();
  showEnemyPositions();
  // 隱身是即時切換,不需要每幀操作

  requestAnimationFrame(mainLoop);
}

mainLoop();