// main.js - Bootstrap: menu, input mapping, game lifecycle. import { Game } from './game.js'; import { Audio } from './audio.js'; import RONIN from './char_ronin.js'; import VEX from './char_vex.js'; const ROSTER = [ { def: RONIN, label: 'RONIN', desc: '平衡型 / 中距離突進' }, { def: VEX, label: 'VEX', desc: '速度型 / 擊倒與升龍' }, ]; const cfg = { p1: 0, diff: 'normal', mode: 'cpu', rounds: 2, }; let game = null; const audio = new Audio(); const $ = s => document.querySelector(s); /* ---------------- menu ---------------- */ function buildMenu() { const selP1 = $('#selP1'); selP1.innerHTML = ''; ROSTER.forEach((r, i) => { const b = document.createElement('button'); b.textContent = r.label; b.title = r.desc; if (i === cfg.p1) b.classList.add('on'); b.onclick = () => { cfg.p1 = i; buildMenu(); }; selP1.appendChild(b); }); const diffs = [['easy', '簡單'], ['normal', '普通'], ['hard', '困難'], ['brutal', '兇殘']]; const sd = $('#selDiff'); sd.innerHTML = ''; for (const [v, label] of diffs) { const b = document.createElement('button'); b.textContent = label; if (cfg.diff === v) b.classList.add('on'); b.onclick = () => { cfg.diff = v; buildMenu(); }; sd.appendChild(b); } segWire('#selMode', cfg.mode, v => { cfg.mode = v; buildMenu(); }); segWire('#selRounds', String(cfg.rounds), v => { cfg.rounds = +v; buildMenu(); }); } function segWire(sel, current, onPick) { const el = $(sel); el.querySelectorAll('button').forEach(b => { b.classList.toggle('on', b.dataset.v === current); b.onclick = () => onPick(b.dataset.v); }); } /* ---------------- game lifecycle ---------------- */ function startGame() { if (game) game.stop(); $('#menu').classList.add('hidden'); const p1Def = ROSTER[cfg.p1].def; const p2Def = ROSTER[(cfg.p1 + 1) % ROSTER.length].def; const canvas = $('#game'); game = new Game(canvas, p1Def, p2Def, { difficulty: cfg.diff, roundsToWin: cfg.rounds, p1Human: true, audio, }); game.p2Human = cfg.mode === 'p2'; window.game = game; game.start(); audio.init(); audio.resume(); audio.startMusic(); toast(cfg.mode === 'p2' ? 'P1: WASD + J/K/L · P2: TFGH + N/M/,' : '移動:WASD / 方向鍵 · J 輕擊 K 重擊 L 特殊 U 快招 I 蹲重擊 O 升龍'); } function backToMenu() { if (game) { game.stop(); audio.stopMusic(); } $('#menu').classList.remove('hidden'); $('#pause').classList.remove('on'); } function toast(msg, ms = 3200) { const t = $('#toast'); t.textContent = msg; t.style.opacity = '1'; clearTimeout(t._h); t._h = setTimeout(() => { t.style.opacity = '0'; }, ms); } /* ---------------- keyboard ---------------- */ // P1: WASD + J/K/L/U/O/I (arrows also move) const P1_MAP = { KeyW: 'up', KeyS: 'down', KeyA: 'left', KeyD: 'right', ArrowUp: 'up', ArrowDown: 'down', ArrowLeft: 'left', ArrowRight: 'right', KeyJ: 'a', KeyK: 'b', KeyL: 'y', KeyU: 'z', KeyO: 'x', KeyI: 'b', }; const P2_MAP = { Numpad8: 'up', Numpad5: 'down', Numpad4: 'left', Numpad6: 'right', KeyT: 'up', KeyG: 'down', KeyF: 'left', KeyH: 'right', KeyN: 'a', KeyM: 'b', Comma: 'c', Period: 'y', Slash: 'z', Semicolon: 'x', }; const p1set = new Set(), p2set = new Set(); addEventListener('keydown', e => { if (e.repeat) return; audio.init(); audio.resume(); if (e.code === 'Escape') { togglePause(); e.preventDefault(); return; } if (e.code === 'Backquote') { if (game) { game.debug = !game.debug; toast('碰撞框: ' + (game.debug ? 'ON' : 'OFF')); } e.preventDefault(); return; } const k1 = P1_MAP[e.code]; if (k1) { p1set.add(k1); if (game) game.setP1(new Set(p1set)); e.preventDefault(); } const k2 = P2_MAP[e.code]; if (k2) { p2set.add(k2); if (game) game.setP2Keys(new Set(p2set)); e.preventDefault(); } if (e.code === 'Enter' && !$('#menu').classList.contains('hidden')) startGame(); }); addEventListener('keyup', e => { const k1 = P1_MAP[e.code]; if (k1) { p1set.delete(k1); if (game) game.setP1(new Set(p1set)); } const k2 = P2_MAP[e.code]; if (k2) { p2set.delete(k2); if (game) game.setP2Keys(new Set(p2set)); } }); function togglePause() { if (!game) return; game.paused = !game.paused; $('#pause').classList.toggle('on', game.paused); if (game.paused) audio.stopMusic(); else { audio.resume(); audio.startMusic(); } } $('#btnStart').onclick = startGame; $('#btnPause').onclick = togglePause; $('#btnResume').onclick = togglePause; $('#btnQuit').onclick = backToMenu; $('#btnMenu').onclick = backToMenu; $('#btnDebug').onclick = () => { if (!game) return; game.debug = !game.debug; $('#btnDebug').classList.toggle('on', game.debug); toast('碰撞框: ' + (game.debug ? 'ON' : 'OFF')); }; /* ---------------- touch controls ---------------- */ if (matchMedia('(pointer: coarse)').matches) document.body.classList.add('touch'); function bindTouch(elId, key) { const el = $(elId); if (!el) return; const on = e => { e.preventDefault(); el.classList.add('act'); p1set.add(key); if (game) game.setP1(new Set(p1set)); }; const off = e => { e.preventDefault(); el.classList.remove('act'); p1set.delete(key); if (game) game.setP1(new Set(p1set)); }; el.addEventListener('touchstart', on, { passive: false }); el.addEventListener('touchend', off, { passive: false }); el.addEventListener('touchcancel', off, { passive: false }); el.addEventListener('mousedown', on); el.addEventListener('mouseup', off); el.addEventListener('mouseleave', off); } bindTouch('#tu', 'up'); bindTouch('#td', 'down'); bindTouch('#tl', 'left'); bindTouch('#tr', 'right'); bindTouch('#ta', 'a'); bindTouch('#tb', 'b'); bindTouch('#tc', 'y'); /* ---------------- fps meter + resize ---------------- */ addEventListener('resize', () => { if (game) game.renderer.resize(); }); let fpsAcc = 0, fpsFrames = 0, lastFps = 0; setInterval(() => { const el = $('#fps'); if (game && !game.paused) { const now = performance.now(); if (lastFps) { fpsAcc += 1000 / 60; fpsFrames++; if (fpsAcc > 500) { el.textContent = `${Math.round(fpsFrames / (fpsAcc / 1000))} fps · frame ${game.sys.frame}`; fpsAcc = 0; fpsFrames = 0; } } lastFps = now; } else { el.textContent = ''; } }, 500); buildMenu();