游戏重构
This commit is contained in:
+11
-26
@@ -1,4 +1,6 @@
|
||||
// utils.js — 轻量工具函数
|
||||
// utils.js — 工具函数
|
||||
|
||||
const DEV = new URLSearchParams(location.search).has('dev');
|
||||
|
||||
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
||||
|
||||
@@ -7,47 +9,30 @@ function dbg(msg) {
|
||||
if (el && el.style.display !== 'none') el.textContent = msg;
|
||||
}
|
||||
|
||||
// 语音合成(选项朗读用)
|
||||
// TTS — 仅用于朗读文字消息内容,不用于任何 UI 反馈
|
||||
const TTS = {
|
||||
_voices: [],
|
||||
_ready: false,
|
||||
|
||||
init() {
|
||||
if (!window.speechSynthesis) return;
|
||||
const load = () => {
|
||||
this._voices = speechSynthesis.getVoices();
|
||||
this._ready = true;
|
||||
};
|
||||
const load = () => { this._voices = speechSynthesis.getVoices(); };
|
||||
load();
|
||||
speechSynthesis.onvoiceschanged = load;
|
||||
},
|
||||
|
||||
_pick() {
|
||||
// 优先选中文语音
|
||||
const zh = this._voices.find(v =>
|
||||
v.lang.startsWith('zh') || v.name.includes('Chinese') || v.name.includes('中')
|
||||
);
|
||||
return zh || this._voices[0] || null;
|
||||
return this._voices.find(v => v.lang.startsWith('zh') || v.name.includes('Chinese'))
|
||||
|| this._voices[0] || null;
|
||||
},
|
||||
|
||||
speak(text, { rate = 0.85, pitch = 1, volume = 0.85 } = {}) {
|
||||
speak(text, { rate = 0.85, volume = 0.8 } = {}) {
|
||||
return new Promise(resolve => {
|
||||
if (!window.speechSynthesis) { resolve(); return; }
|
||||
speechSynthesis.cancel();
|
||||
const u = new SpeechSynthesisUtterance(text);
|
||||
u.lang = 'zh-CN';
|
||||
u.rate = rate;
|
||||
u.pitch = pitch;
|
||||
u.volume = volume;
|
||||
u.lang = 'zh-CN'; u.rate = rate; u.volume = volume;
|
||||
const v = this._pick();
|
||||
if (v) u.voice = v;
|
||||
u.onend = resolve;
|
||||
u.onerror = resolve;
|
||||
u.onend = resolve; u.onerror = resolve;
|
||||
speechSynthesis.speak(u);
|
||||
});
|
||||
},
|
||||
|
||||
stop() {
|
||||
if (window.speechSynthesis) speechSynthesis.cancel();
|
||||
},
|
||||
stop() { if (window.speechSynthesis) speechSynthesis.cancel(); },
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user