154 lines
3.8 KiB
JavaScript
154 lines
3.8 KiB
JavaScript
/**
|
|
* 游戏虚拟老师集成脚本
|
|
* 为游戏页面提供虚拟老师功能的通用集成
|
|
*/
|
|
class GameVirtualTeacher {
|
|
constructor() {
|
|
this.isInitialized = false;
|
|
}
|
|
|
|
/**
|
|
* 初始化虚拟老师集成
|
|
*/
|
|
async init(gameInfo = {}) {
|
|
if (this.isInitialized) return;
|
|
|
|
try {
|
|
// 等待虚拟老师模块加载
|
|
if (!window.virtualTeacher) {
|
|
console.warn('虚拟老师模块未加载,跳过初始化');
|
|
return;
|
|
}
|
|
|
|
// 初始化虚拟老师
|
|
await window.virtualTeacher.init();
|
|
console.log('🦉 游戏虚拟老师集成初始化成功');
|
|
|
|
// 不立即播放游戏介绍,由游戏页面控制播放时机
|
|
this.gameIntro = gameInfo.intro;
|
|
|
|
this.isInitialized = true;
|
|
} catch (error) {
|
|
console.error('游戏虚拟老师集成初始化失败:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 游戏开始
|
|
*/
|
|
gameStart(gameName, gameData = {}) {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
// 重置统计
|
|
window.virtualTeacher.resetStats();
|
|
|
|
// 游戏开始提示
|
|
window.virtualTeacher.gameStart(gameName);
|
|
|
|
// 播放游戏介绍(如果有的话)
|
|
if (this.gameIntro) {
|
|
setTimeout(() => {
|
|
window.virtualTeacher.speak(this.gameIntro);
|
|
}, 500);
|
|
}
|
|
|
|
console.log('🦉 虚拟老师:游戏开始 -', gameName);
|
|
}
|
|
|
|
/**
|
|
* 显示题目
|
|
*/
|
|
showQuestion(questionText, questionNumber = 1, totalQuestions = 1) {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
// 停止当前语音
|
|
window.virtualTeacher.stopAllSpeech();
|
|
|
|
// 延迟播放题目语音
|
|
setTimeout(() => {
|
|
const fullQuestionText = `第${questionNumber}题:${questionText}`;
|
|
window.virtualTeacher.readQuestion(fullQuestionText);
|
|
}, 200);
|
|
}
|
|
|
|
/**
|
|
* 记录正确答案
|
|
*/
|
|
recordCorrect() {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
// 停止当前语音
|
|
window.virtualTeacher.stopAllSpeech();
|
|
|
|
// 延迟播放反馈语音
|
|
setTimeout(() => {
|
|
window.virtualTeacher.recordCorrect();
|
|
}, 100);
|
|
}
|
|
|
|
/**
|
|
* 记录错误答案
|
|
*/
|
|
recordIncorrect() {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
// 停止当前语音
|
|
window.virtualTeacher.stopAllSpeech();
|
|
|
|
// 延迟播放反馈语音
|
|
setTimeout(() => {
|
|
window.virtualTeacher.recordIncorrect();
|
|
}, 100);
|
|
}
|
|
|
|
/**
|
|
* 游戏结束
|
|
*/
|
|
gameEnd() {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
// 立即停止所有语音
|
|
window.virtualTeacher.stopAllSpeech();
|
|
|
|
// 延迟一点再播放游戏结束语音
|
|
setTimeout(() => {
|
|
window.virtualTeacher.gameEnd();
|
|
}, 500);
|
|
}
|
|
|
|
/**
|
|
* 停止所有语音
|
|
*/
|
|
stopAllSpeech() {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
window.virtualTeacher.stopAllSpeech();
|
|
}
|
|
|
|
/**
|
|
* 自定义语音
|
|
*/
|
|
speak(text, options = {}) {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
window.virtualTeacher.speak(text, options);
|
|
}
|
|
|
|
/**
|
|
* 改变表情
|
|
*/
|
|
changeExpression(expression) {
|
|
if (!window.virtualTeacher || !this.isInitialized) return;
|
|
|
|
window.virtualTeacher.changeExpression(expression);
|
|
}
|
|
}
|
|
|
|
// 创建全局实例
|
|
window.gameVirtualTeacher = new GameVirtualTeacher();
|
|
|
|
// 导出类
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
module.exports = GameVirtualTeacher;
|
|
}
|