eithney commited on
Commit
c87be38
·
verified ·
1 Parent(s): da36fe2

Upload 27 files

Browse files
.dockerignore ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Node.js 相关
2
+ node_modules/
3
+ npm-debug.log*
4
+ yarn-debug.log*
5
+ yarn-error.log*
6
+
7
+ # 包管理文件(Docker镜像中不需要)
8
+ package.json
9
+ package-lock.json
10
+
11
+ # 开发工具和文档
12
+ README.md
13
+ .git/
14
+ .gitignore
15
+
16
+ # 系统文件
17
+ .DS_Store
18
+ Thumbs.db
19
+
20
+ # IDE 文件
21
+ .vscode/
22
+ .idea/
23
+ *.swp
24
+ *.swo
25
+
26
+ # 日志文件
27
+ *.log
28
+
29
+ # Docker相关
30
+ Dockerfile
31
+ .dockerignore
32
+
33
+ # 临时文件
34
+ *.tmp
35
+ *.temp
.gitattributes CHANGED
@@ -33,3 +33,10 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ 01HXBW1PHMD1MR2K7NHZT0AQVP.mp3 filter=lfs diff=lfs merge=lfs -text
37
+ assets/images/ChatGPT[[:space:]]Image[[:space:]]2025年9月22日[[:space:]]17_48_21.png filter=lfs diff=lfs merge=lfs -text
38
+ assets/images/ChatGPT[[:space:]]Image[[:space:]]2025年9月22日[[:space:]]17_51_40.png filter=lfs diff=lfs merge=lfs -text
39
+ assets/images/ChatGPT[[:space:]]Image[[:space:]]2025年9月22日[[:space:]]17_54_23.png filter=lfs diff=lfs merge=lfs -text
40
+ assets/images/magic_potx.png filter=lfs diff=lfs merge=lfs -text
41
+ assets/sounds/animal_appear.mp3 filter=lfs diff=lfs merge=lfs -text
42
+ assets/sounds/cookingmp3.mp3 filter=lfs diff=lfs merge=lfs -text
01HXBW1PHMD1MR2K7NHZT0AQVP.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0e103049a8e25f57624af9dafd50abaa7e9537c6a3c46801ac50b1cb192002e
3
+ size 960788
Dockerfile ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 使用官方nginx镜像作为基础镜像
2
+ FROM nginx:alpine
3
+
4
+ # 设置维护者信息
5
+ LABEL maintainer="MagicPot Game Developer"
6
+ LABEL description="MagicPot - 魔法锅游戏 Docker镜像"
7
+
8
+ # 删除默认的nginx配置
9
+ RUN rm /etc/nginx/conf.d/default.conf
10
+
11
+ # 复制自定义nginx配置
12
+ COPY nginx.conf /etc/nginx/conf.d/
13
+
14
+ # 复制项目文件到nginx的html目录
15
+ COPY index.html /usr/share/nginx/html/
16
+ COPY css/ /usr/share/nginx/html/css/
17
+ COPY js/ /usr/share/nginx/html/js/
18
+ COPY assets/ /usr/share/nginx/html/assets/
19
+ COPY *.mp3 /usr/share/nginx/html/
20
+
21
+ # 设置正确的文件权限
22
+ RUN chmod -R 755 /usr/share/nginx/html
23
+
24
+ # 暴露80端口
25
+ EXPOSE 80
26
+
27
+ # 启动nginx
28
+ CMD ["nginx", "-g", "daemon off;"]
README.md CHANGED
@@ -1,10 +1,264 @@
1
- ---
2
- title: MagicPot
3
- emoji: 🌖
4
- colorFrom: red
5
- colorTo: blue
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # MagicPot - 魔法锅游戏
2
+
3
+ 一个卡通手绘风格的语音交互游戏,通过声音控制魔法锅烹饪各种食物!
4
+
5
+ ## 游戏特色
6
+
7
+ - 🎨 **卡通手绘风格**:温馨可爱的视觉效果
8
+ - 🎤 **语音交互**:通过语音命令控制游戏
9
+ - 🍳 **魔法烹饪**:AI生成食物和小动物素材
10
+ - ✨ **粒子效果**:食物从锅中涌出的动态效果
11
+ - 🐾 **动物互动**:小动物会来吃对应的食物
12
+ - 🎁 **隐藏彩蛋**:草莓和西瓜的特殊效果
13
+
14
+ ## 游戏玩法
15
+
16
+ 1. **激活魔法锅**:说出 "cook cook cook pot" 来激活魔法锅
17
+ 2. **选择食物**:说出任何食物的英文单词开始烹饪
18
+ 3. **AI生成素材**:系统自动生成食物和相关小动物的图片
19
+ 4. **食物涌出**:锅盖被顶开,食物如粒子般涌出
20
+ 5. **停止烹饪**:说出 "stop stop stop pot" 停止食物产生
21
+ 6. **动物出现**:当食物达到一定数量,小动物会从屏幕两侧跑来
22
+ 7. **特殊彩蛋**:草莓或西瓜达到一定数量时,小女孩会从右侧出现
23
+
24
+ ## 技术栈
25
+
26
+ - **前端框架**:HTML5 Canvas + JavaScript
27
+ - **语音识别**:Web Speech API
28
+ - **文生图API**:集成AI图像生成服务
29
+ - **动画引擎**:自定义粒子系统
30
+ - **音频处理**:Web Audio API
31
+
32
+ ## 项目结构
33
+
34
+ ```
35
+ MagicPot/
36
+ ├── index.html # 主页面
37
+ ├── css/
38
+ │ └── style.css # 样式文件
39
+ ├── js/
40
+ │ ├── main.js # 主游戏逻辑
41
+ │ ├── voice.js # 语音识别模块
42
+ │ ├── pot.js # 魔法锅类
43
+ │ ├── particles.js # 粒子系统
44
+ │ ├── animals.js # 动物系统
45
+ │ └── textToImage.js # 文生图API接口
46
+ ├── assets/
47
+ │ ├── images/ # 图片资源
48
+ │ ├── sounds/ # 音效文件
49
+ │ └── fonts/ # 字体文件
50
+ └── README.md
51
+ ```
52
+
53
+ ## 安装和运行
54
+
55
+ 1. 克隆项目到本地
56
+ 2. 使用本地服务器运行(推荐使用 Live Server 或 Python HTTP Server)
57
+ 3. 在浏览器中打开,允许麦克风权限
58
+ 4. 开始游戏!
59
+
60
+ ## 开发计划
61
+
62
+ - [x] 项目结构搭建
63
+ - [x] 语音识别功能
64
+ - [x] 魔法锅UI设计
65
+ - [x] 文生图API集成
66
+ - [x] 粒子系统实现
67
+ - [x] 动物生成系统
68
+ - [x] 彩蛋功能
69
+ - [x] 游戏状态管理
70
+
71
+ ## 快速开始
72
+
73
+ ### 方法一:使用 Python HTTP 服务器
74
+ ```bash
75
+ # 在项目根目录运行
76
+ python -m http.server 8000
77
+ # 然后在浏览器中访问 http://localhost:8000
78
+ ```
79
+
80
+ ### 方法二:使用 Node.js
81
+ ```bash
82
+ # 安装依赖
83
+ npm install
84
+
85
+ # 启动开发服务器
86
+ npm run dev
87
+ ```
88
+
89
+ ### 方法三:直接打开文件
90
+ 直接用浏览器打开 `index.html` 文件(某些功能可能受限)
91
+
92
+ ## 游戏控制
93
+
94
+ ### 语音命令
95
+ - **"cook cook cook pot"** - 激活魔法锅
96
+ - **说出食物名称** - 开始烹饪(如:apple, banana, pizza等)
97
+ - **"stop stop stop pot"** - 停止食物涌出
98
+
99
+ ### 键盘快捷键
100
+ - **F12** - 切换调试信息显示
101
+ - **F11** - 测试所有音效
102
+ - **F10** - 切换音效开关
103
+ - **Ctrl+Shift+I** - 配置文生图API密钥
104
+ - **ESC** - 关闭帮助窗口
105
+
106
+ ### 界面功能
107
+ - **🔊 音效按钮** - 点击切换音效开关
108
+ - **❓ 帮助按钮** - 点击查看详细的语音命令帮助
109
+ - **🎤 语音状态指示器** - 显示当前语音识别状态
110
+ - **💬 实时语音显示** - 实时显示用户的语音输入内容
111
+ - **📊 性能监控** - 实时显示粒子数、动物数和FPS(烹饪时自动显示)
112
+
113
+ ## 支持的食物
114
+
115
+ ### 水果类
116
+ - apple (苹果) 🍎
117
+ - banana (香蕉) 🍌
118
+ - orange (橙子) 🍊
119
+ - strawberry (草莓) 🍓 *彩蛋*
120
+ - watermelon (西瓜) 🍉 *彩蛋*
121
+ - grape (葡萄) 🍇
122
+
123
+ ### 主食类
124
+ - pizza (披萨) 🍕
125
+ - burger (汉堡) 🍔
126
+ - bread (面包) 🍞
127
+ - rice (米饭) 🍚
128
+ - noodles (面条) 🍜
129
+
130
+ ### 甜点类
131
+ - cake (蛋糕) 🍰 *特殊效果*
132
+ - cookie (饼干) 🍪
133
+
134
+ ### 其他
135
+ - cheese (奶酪) 🧀
136
+ - fish (鱼) 🐟
137
+ - chicken (鸡肉) 🍗
138
+
139
+ ## 特殊功能
140
+
141
+ ### 彩蛋功能
142
+ - **草莓/西瓜彩蛋**:当草莓或西瓜数量达到30个时,小女孩会从右侧出现
143
+ - **生日蛋糕特效**:制作蛋糕时会有特殊的生日庆祝效果
144
+ - **百食庆祝**:制作100个食物时会有烟花庆祝效果
145
+
146
+ ### 动物系统
147
+ 不同的食物会吸引不同的小动物:
148
+ - 🍎 苹果 → 🐰 兔子
149
+ - 🍌 香蕉 → 🐵 猴子
150
+ - 🍊 橙子 → 🐦 小鸟
151
+ - 🍓 草莓 → 🐻 熊
152
+ - 🍉 西瓜 → 🐘 大象
153
+ - 🍕 披萨 → 🐱 猫咪
154
+ - 🍔 汉堡 → 🐶 小狗
155
+
156
+ ## 技术特性
157
+
158
+ ### 语音识别
159
+ - 使用 Web Speech API
160
+ - 支持英文语音识别
161
+ - ���动重启和错误处理
162
+ - 实时状态显示
163
+
164
+ ### 粒子系统
165
+ - 食物涌出效果
166
+ - 魔法粒子特效
167
+ - 蒸汽动画
168
+ - 庆祝烟花效果
169
+
170
+ ### AI图片生成
171
+ - 支持文生图API集成
172
+ - 本地缓存机制
173
+ - 备用表情符号显示
174
+ - 可配置API密钥
175
+
176
+ ### 动画效果
177
+ - 卡通手绘风格
178
+ - 流畅的Canvas动画
179
+ - 粒子物理系统
180
+ - 动物AI行为
181
+
182
+ ### 音效系统
183
+ - **程序化音效生成**:使用Web Audio API实时生成音效
184
+ - **丰富的音效类型**:
185
+ - 🪄 魔法激活音效(上升音调带颤音)
186
+ - 🫧 烹饪冒泡音效(随机频率变化)
187
+ - 💥 食物弹出音效(短促爆破声)
188
+ - 🐦 动物出现音效(鸟叫声)
189
+ - 🔔 小女孩出现音效(闪亮铃声)
190
+ - 🎺 庆祝音效(三和弦号角声)
191
+ - 💨 停止音效(风声效果)
192
+ - **智能音效控制**:可开关、音量控制、随机音调变化
193
+ - **性能优化**:预生成音效缓存,避免实时计算
194
+
195
+ ### 性能优化
196
+ - **内存管理**:智能控制粒子和动物数量上限
197
+ - **自动清理**:定期清理过期和无效对象
198
+ - **性能监控**:实时显示FPS、粒子数、动物数
199
+ - **资源限制**:
200
+ - 最大粒子数:150个
201
+ - 最大动物数:8只
202
+ - 最大食物数:100个
203
+ - 地面粒子上限:80个
204
+
205
+ ## 浏览器兼容性
206
+
207
+ ### 推荐浏览器
208
+ - Chrome 80+
209
+ - Edge 80+
210
+ - Firefox 75+
211
+ - Safari 14+
212
+
213
+ ### 必需功能
214
+ - Web Speech API(语音识别)
215
+ - Canvas 2D API(游戏渲染)
216
+ - Web Audio API(音效支持)
217
+ - ES6+ 支持
218
+
219
+ ## 故障排除
220
+
221
+ ### 语音识别不工作
222
+ 1. 检查浏览器是否支持 Web Speech API
223
+ 2. 确保已允许麦克风权限
224
+ 3. 检查网络连接(某些浏览器需要HTTPS)
225
+
226
+ ### 文生图功能不工作
227
+ 1. 按 Ctrl+Shift+I 配置API密钥
228
+ 2. 检查API密钥是否有效
229
+ 3. 即使没有API,游戏也会使用备用图片正常运行
230
+
231
+ ### 性能问题
232
+ 1. 按F12查看调试信息
233
+ 2. 检查粒子数量是否过多
234
+ 3. 尝试刷新页面重置游戏
235
+
236
+ ## 开发说明
237
+
238
+ ### 项目结构
239
+ ```
240
+ MagicPot/
241
+ ├── index.html # 主页面
242
+ ├── css/style.css # 样式文件
243
+ ├── js/ # JavaScript模块
244
+ │ ├── main.js # 主游戏逻辑
245
+ │ ├── voice.js # 语音识别
246
+ │ ├── pot.js # 魔法锅类
247
+ │ ├── particles.js # 粒子系统
248
+ │ ├── animals.js # 动物系统
249
+ │ └── textToImage.js # 文生图API
250
+ ├── assets/ # 资源文件
251
+ │ ├── images/ # 图片资源
252
+ │ └── sounds/ # 音效文件
253
+ └── package.json # 项目配置
254
+ ```
255
+
256
+ ### 扩展开发
257
+ - 添加新食物:在各个模块的映射表中添加新条目
258
+ - 添加新动物:扩展 `animals.js` 中的动物类型
259
+ - 添加新特效:在 `particles.js` 中创建新的粒子类型
260
+ - 添加音效:在相应的事件中调用音频播放函数
261
+
262
+ ## 许可证
263
+
264
+ MIT License - 详见 LICENSE 文件
assets/images/ChatGPT Image 2025年9月22日 17_48_21.png ADDED

Git LFS Details

  • SHA256: efb54d7ae630a5085f8f2207bae834a2a3731f3ea9d518529d7f9b06901ce2ab
  • Pointer size: 132 Bytes
  • Size of remote file: 3.79 MB
assets/images/ChatGPT Image 2025年9月22日 17_51_40.png ADDED

Git LFS Details

  • SHA256: 04759f1f82564c921e5de2dcf2da161b3668d3b2a8bdf53e6d78331a866e69ea
  • Pointer size: 132 Bytes
  • Size of remote file: 3.6 MB
assets/images/ChatGPT Image 2025年9月22日 17_54_23.png ADDED

Git LFS Details

  • SHA256: b4bd2e73793d9da2f6f0aacd9edc1cec5c145b63d7340ab6d388df23cb852248
  • Pointer size: 132 Bytes
  • Size of remote file: 3.03 MB
assets/images/README.md ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 图片资源
2
+
3
+ 这个文件夹用于存放游戏图片资源。
4
+
5
+ ## 图片分类:
6
+
7
+ ### 食物图片 (food/)
8
+ - 各种食物的卡通手绘图片
9
+ - 建议尺寸:64x64 或 128x128 像素
10
+ - 格式:PNG(支持透明背景)
11
+
12
+ ### 动物图片 (animals/)
13
+ - 各种小动物的卡通手绘图片
14
+ - 建议尺寸:64x64 或 128x128 像素
15
+ - 格式:PNG(支持透明背景)
16
+
17
+ ### UI图片 (ui/)
18
+ - 游戏界面元素图片
19
+ - 按钮、图标等
20
+ - 格式:PNG
21
+
22
+ ### 背景图片 (backgrounds/)
23
+ - 游戏背景图片
24
+ - 建议尺寸:800x600 或更高
25
+ - 格式:JPG 或 PNG
26
+
27
+ ## 图片命名规范:
28
+
29
+ - 使用英文小写字母
30
+ - 单词之间用下划线连接
31
+ - 例如:`magic_pot.png`, `cute_rabbit.png`
assets/images/magic_potx.png ADDED

Git LFS Details

  • SHA256: 3f9b42ac9cc6436b45f1e0b19354639d10e3e8c0c1145c4c09d350592dcaf62d
  • Pointer size: 132 Bytes
  • Size of remote file: 2.4 MB
assets/sounds/README.md ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 音效文件
2
+
3
+ 这个文件夹用于存放游戏音效文件。
4
+
5
+ ## 建议的音效文件:
6
+
7
+ - `pot_activate.mp3` - 魔法锅激活音效
8
+ - `cooking.mp3` - 烹饪过程音效
9
+ - `food_pop.mp3` - 食物涌出音效
10
+ - `animal_appear.mp3` - 动物出现音效
11
+ - `girl_appear.mp3` - 小女孩出现音效
12
+ - `celebration.mp3` - 庆祝音效
13
+
14
+ ## 音效格式要求:
15
+
16
+ - 格式:MP3 或 WAV
17
+ - 大小:建议小于 1MB
18
+ - 时长:建议 1-3 秒
assets/sounds/animal_appear.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e774477f7327909be32f919dee0933725d6b7846d91c85417dbbaf8d869d03ef
3
+ size 412425
assets/sounds/cookingmp3.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0e103049a8e25f57624af9dafd50abaa7e9537c6a3c46801ac50b1cb192002e
3
+ size 960788
assets/sounds/pot_activate.mp3 ADDED
Binary file (81 kB). View file
 
css/style.css ADDED
@@ -0,0 +1,1125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* 全局样式 */
2
+ * {
3
+ margin: 0;
4
+ padding: 0;
5
+ box-sizing: border-box;
6
+ }
7
+
8
+ body {
9
+ font-family: 'Comic Neue', cursive;
10
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
11
+ min-height: 100vh;
12
+ overflow: hidden;
13
+ }
14
+
15
+ /* 游戏容器 */
16
+ .game-container {
17
+ width: 100vw;
18
+ height: 100vh;
19
+ display: flex;
20
+ flex-direction: column;
21
+ position: relative;
22
+ }
23
+
24
+ /* 游戏标题区域 */
25
+ .game-header {
26
+ background: rgba(255, 255, 255, 0.95);
27
+ padding: 15px 20px;
28
+ display: flex;
29
+ justify-content: space-between;
30
+ align-items: center;
31
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
32
+ border-bottom: 3px solid #ff6b6b;
33
+ }
34
+
35
+ .header-controls {
36
+ display: flex;
37
+ align-items: center;
38
+ gap: 15px;
39
+ }
40
+
41
+ .audio-button, .help-button {
42
+ background: #ff6b6b;
43
+ color: white;
44
+ border: none;
45
+ border-radius: 50%;
46
+ width: 40px;
47
+ height: 40px;
48
+ font-size: 18px;
49
+ cursor: pointer;
50
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
51
+ transition: all 0.3s ease;
52
+ font-family: 'Comic Neue', cursive;
53
+ font-weight: bold;
54
+ }
55
+
56
+ .audio-button:hover, .help-button:hover {
57
+ background: #e55555;
58
+ transform: scale(1.1);
59
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
60
+ }
61
+
62
+ .audio-button:active, .help-button:active {
63
+ transform: scale(0.95);
64
+ }
65
+
66
+ .audio-button.muted {
67
+ background: #95a5a6;
68
+ color: #bdc3c7;
69
+ }
70
+
71
+ .audio-button.muted:hover {
72
+ background: #7f8c8d;
73
+ }
74
+
75
+ .game-title {
76
+ font-family: 'Fredoka One', cursive;
77
+ font-size: 2.2em;
78
+ color: #ff6b6b;
79
+ text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
80
+ margin: 0;
81
+ }
82
+
83
+ .voice-status {
84
+ display: flex;
85
+ align-items: center;
86
+ gap: 10px;
87
+ background: #4ecdc4;
88
+ padding: 8px 15px;
89
+ border-radius: 25px;
90
+ color: white;
91
+ font-weight: bold;
92
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
93
+ }
94
+
95
+ .mic-icon {
96
+ font-size: 1.2em;
97
+ animation: pulse 2s infinite;
98
+ }
99
+
100
+ @keyframes pulse {
101
+ 0%, 100% { transform: scale(1); }
102
+ 50% { transform: scale(1.1); }
103
+ }
104
+
105
+ /* 主游戏区域 */
106
+ .game-main {
107
+ flex: 1;
108
+ position: relative;
109
+ display: flex;
110
+ justify-content: center;
111
+ align-items: center;
112
+ }
113
+
114
+ #gameCanvas {
115
+ border: 4px solid #fff;
116
+ border-radius: 20px;
117
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
118
+ background: linear-gradient(45deg, #ffeaa7, #fab1a0);
119
+ }
120
+
121
+ /* 游戏UI覆盖层 */
122
+ .game-ui {
123
+ position: absolute;
124
+ top: 0;
125
+ left: 0;
126
+ width: 100%;
127
+ height: 100%;
128
+ pointer-events: none;
129
+ z-index: 10;
130
+ }
131
+
132
+ /* 键盘面板在game-ui内,需要特殊处理 */
133
+ .game-ui .keyboard-input-panel {
134
+ pointer-events: auto;
135
+ }
136
+
137
+ .game-ui .keyboard-input-panel.inactive {
138
+ pointer-events: none;
139
+ }
140
+
141
+ .game-ui .keyboard-input-panel.active {
142
+ pointer-events: auto !important;
143
+ }
144
+
145
+ /* 确保激活状态下所有子元素都可以交互 */
146
+ .keyboard-input-panel.active,
147
+ .keyboard-input-panel.active *,
148
+ .keyboard-input-panel.active .input-content,
149
+ .keyboard-input-panel.active .command-input,
150
+ .keyboard-input-panel.active .send-btn,
151
+ .keyboard-input-panel.active .quick-cmd,
152
+ .keyboard-input-panel.active .toggle-panel-btn,
153
+ .keyboard-input-panel.active .history-item {
154
+ pointer-events: auto !important;
155
+ }
156
+
157
+ /* 魔法锅状态 */
158
+ .pot-status {
159
+ position: absolute;
160
+ top: 20px;
161
+ left: 50%;
162
+ transform: translateX(-50%);
163
+ pointer-events: auto;
164
+ }
165
+
166
+ .status-indicator {
167
+ padding: 10px 20px;
168
+ border-radius: 25px;
169
+ font-weight: bold;
170
+ text-align: center;
171
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
172
+ transition: all 0.3s ease;
173
+ }
174
+
175
+ .status-indicator.inactive {
176
+ background: #95a5a6;
177
+ color: white;
178
+ }
179
+
180
+ .status-indicator.active {
181
+ background: #e74c3c;
182
+ color: white;
183
+ animation: glow 2s infinite alternate;
184
+ }
185
+
186
+ .status-indicator.cooking {
187
+ background: #f39c12;
188
+ color: white;
189
+ animation: cooking-pulse 1s infinite;
190
+ }
191
+
192
+ .status-indicator.overflowing {
193
+ background: #27ae60;
194
+ color: white;
195
+ animation: overflow-shake 0.5s infinite;
196
+ }
197
+
198
+ @keyframes glow {
199
+ from { box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3); }
200
+ to { box-shadow: 0 4px 25px rgba(231, 76, 60, 0.8); }
201
+ }
202
+
203
+ @keyframes cooking-pulse {
204
+ 0%, 100% { transform: scale(1); }
205
+ 50% { transform: scale(1.05); }
206
+ }
207
+
208
+ @keyframes overflow-shake {
209
+ 0%, 100% { transform: translateX(0); }
210
+ 25% { transform: translateX(-2px); }
211
+ 75% { transform: translateX(2px); }
212
+ }
213
+
214
+ /* 烹饪信息 */
215
+ .cooking-info {
216
+ position: absolute;
217
+ top: 80px;
218
+ left: 50%;
219
+ transform: translateX(-50%);
220
+ background: rgba(255, 255, 255, 0.95);
221
+ padding: 15px 25px;
222
+ border-radius: 15px;
223
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
224
+ text-align: center;
225
+ min-width: 200px;
226
+ }
227
+
228
+ .cooking-food {
229
+ font-size: 1.2em;
230
+ font-weight: bold;
231
+ color: #2c3e50;
232
+ margin-bottom: 10px;
233
+ }
234
+
235
+ .progress-bar {
236
+ width: 100%;
237
+ height: 8px;
238
+ background: #ecf0f1;
239
+ border-radius: 4px;
240
+ overflow: hidden;
241
+ }
242
+
243
+ .progress-fill {
244
+ height: 100%;
245
+ background: linear-gradient(90deg, #ff6b6b, #4ecdc4);
246
+ border-radius: 4px;
247
+ transition: width 0.3s ease;
248
+ width: 0%;
249
+ }
250
+
251
+ /* 食物计数器 */
252
+ .food-counter {
253
+ position: absolute;
254
+ top: 20px;
255
+ right: 20px;
256
+ background: rgba(255, 255, 255, 0.95);
257
+ padding: 15px 20px;
258
+ border-radius: 15px;
259
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
260
+ display: flex;
261
+ align-items: center;
262
+ gap: 10px;
263
+ }
264
+
265
+ /* 性能监控 */
266
+ .performance-monitor {
267
+ position: absolute;
268
+ top: 20px;
269
+ left: 20px;
270
+ background: rgba(0, 0, 0, 0.8);
271
+ color: white;
272
+ padding: 10px 15px;
273
+ border-radius: 10px;
274
+ font-family: 'Courier New', monospace;
275
+ font-size: 0.9em;
276
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
277
+ min-width: 120px;
278
+ }
279
+
280
+ .perf-item {
281
+ display: flex;
282
+ justify-content: space-between;
283
+ margin-bottom: 5px;
284
+ align-items: center;
285
+ }
286
+
287
+ .perf-item:last-child {
288
+ margin-bottom: 0;
289
+ }
290
+
291
+ .perf-label {
292
+ color: #4ecdc4;
293
+ font-weight: bold;
294
+ }
295
+
296
+ .perf-value {
297
+ color: #ffffff;
298
+ font-weight: bold;
299
+ text-align: right;
300
+ min-width: 30px;
301
+ }
302
+
303
+ .food-emoji {
304
+ font-size: 2em;
305
+ }
306
+
307
+ .count {
308
+ font-size: 1.5em;
309
+ font-weight: bold;
310
+ color: #2c3e50;
311
+ min-width: 30px;
312
+ text-align: center;
313
+ }
314
+
315
+ /* 语音提示 */
316
+ .voice-hints {
317
+ position: absolute;
318
+ bottom: 20px;
319
+ left: 50%;
320
+ transform: translateX(-50%);
321
+ display: flex;
322
+ flex-direction: column;
323
+ gap: 10px;
324
+ align-items: center;
325
+ }
326
+
327
+ .hint-item {
328
+ background: rgba(0, 0, 0, 0.8);
329
+ color: white;
330
+ padding: 8px 15px;
331
+ border-radius: 20px;
332
+ display: flex;
333
+ align-items: center;
334
+ gap: 10px;
335
+ font-size: 0.9em;
336
+ animation: hint-fade-in 0.5s ease;
337
+ }
338
+
339
+ .hint-command {
340
+ background: #ff6b6b;
341
+ padding: 4px 8px;
342
+ border-radius: 10px;
343
+ font-weight: bold;
344
+ font-size: 0.8em;
345
+ }
346
+
347
+ .hint-desc {
348
+ opacity: 0.9;
349
+ }
350
+
351
+ @keyframes hint-fade-in {
352
+ from { opacity: 0; transform: translateY(10px); }
353
+ to { opacity: 1; transform: translateY(0); }
354
+ }
355
+
356
+ /* 调试信息 */
357
+ .debug-info {
358
+ position: absolute;
359
+ top: 10px;
360
+ left: 10px;
361
+ background: rgba(0, 0, 0, 0.8);
362
+ color: white;
363
+ padding: 10px;
364
+ border-radius: 5px;
365
+ font-family: monospace;
366
+ font-size: 0.8em;
367
+ max-width: 300px;
368
+ max-height: 200px;
369
+ overflow-y: auto;
370
+ z-index: 1000;
371
+ }
372
+
373
+ /* 响应式设计 */
374
+ @media (max-width: 768px) {
375
+ .game-title {
376
+ font-size: 1.5em;
377
+ }
378
+
379
+ #gameCanvas {
380
+ width: 95vw;
381
+ height: 70vh;
382
+ }
383
+
384
+ .voice-hints {
385
+ bottom: 10px;
386
+ }
387
+
388
+ .hint-item {
389
+ font-size: 0.8em;
390
+ padding: 6px 12px;
391
+ }
392
+ }
393
+
394
+ /* 动画效果 */
395
+ .bounce-in {
396
+ animation: bounceIn 0.6s ease;
397
+ }
398
+
399
+ @keyframes bounceIn {
400
+ 0% { transform: scale(0.3); opacity: 0; }
401
+ 50% { transform: scale(1.05); }
402
+ 70% { transform: scale(0.9); }
403
+ 100% { transform: scale(1); opacity: 1; }
404
+ }
405
+
406
+ .fade-out {
407
+ animation: fadeOut 0.5s ease forwards;
408
+ }
409
+
410
+ @keyframes fadeOut {
411
+ from { opacity: 1; }
412
+ to { opacity: 0; }
413
+ }
414
+
415
+ /* 特殊效果 */
416
+ .sparkle {
417
+ position: absolute;
418
+ width: 4px;
419
+ height: 4px;
420
+ background: #ffd700;
421
+ border-radius: 50%;
422
+ animation: sparkle 1s ease-in-out infinite;
423
+ }
424
+
425
+ @keyframes sparkle {
426
+ 0%, 100% { opacity: 0; transform: scale(0); }
427
+ 50% { opacity: 1; transform: scale(1); }
428
+ }
429
+
430
+ /* 音频激活提示 */
431
+ .audio-activation-hint {
432
+ position: absolute;
433
+ top: 30%;
434
+ left: 50%;
435
+ transform: translate(-50%, -50%);
436
+ z-index: 200;
437
+ pointer-events: none;
438
+ animation: audioHintPulse 2s infinite;
439
+ }
440
+
441
+ .hint-bubble {
442
+ background: rgba(255, 107, 107, 0.95);
443
+ border: 3px solid #e74c3c;
444
+ border-radius: 20px;
445
+ padding: 15px 25px;
446
+ display: flex;
447
+ align-items: center;
448
+ gap: 10px;
449
+ box-shadow: 0 8px 32px rgba(231, 76, 60, 0.3);
450
+ color: white;
451
+ font-family: 'Comic Neue', cursive;
452
+ font-weight: bold;
453
+ }
454
+
455
+ .hint-icon {
456
+ font-size: 1.5em;
457
+ animation: audioIconBounce 1s infinite;
458
+ }
459
+
460
+ .hint-text {
461
+ font-size: 1.1em;
462
+ }
463
+
464
+ @keyframes audioHintPulse {
465
+ 0%, 100% {
466
+ transform: translate(-50%, -50%) scale(1);
467
+ opacity: 0.9;
468
+ }
469
+ 50% {
470
+ transform: translate(-50%, -50%) scale(1.05);
471
+ opacity: 1;
472
+ }
473
+ }
474
+
475
+ @keyframes audioIconBounce {
476
+ 0%, 100% {
477
+ transform: translateY(0);
478
+ }
479
+ 50% {
480
+ transform: translateY(-3px);
481
+ }
482
+ }
483
+
484
+ /* 语音输入显示 */
485
+ .voice-input-display {
486
+ position: absolute;
487
+ top: 50%;
488
+ left: 50%;
489
+ transform: translate(-50%, -50%);
490
+ z-index: 100;
491
+ pointer-events: none;
492
+ }
493
+
494
+ .speech-bubble {
495
+ background: rgba(255, 255, 255, 0.95);
496
+ border: 3px solid #ff6b6b;
497
+ border-radius: 25px;
498
+ padding: 20px 30px;
499
+ position: relative;
500
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
501
+ animation: speechBubble 0.3s ease-out;
502
+ max-width: 400px;
503
+ text-align: center;
504
+ }
505
+
506
+ .speech-bubble::before {
507
+ content: '';
508
+ position: absolute;
509
+ bottom: -15px;
510
+ left: 50%;
511
+ transform: translateX(-50%);
512
+ width: 0;
513
+ height: 0;
514
+ border-left: 15px solid transparent;
515
+ border-right: 15px solid transparent;
516
+ border-top: 15px solid #ff6b6b;
517
+ }
518
+
519
+ .speech-bubble::after {
520
+ content: '';
521
+ position: absolute;
522
+ bottom: -12px;
523
+ left: 50%;
524
+ transform: translateX(-50%);
525
+ width: 0;
526
+ height: 0;
527
+ border-left: 12px solid transparent;
528
+ border-right: 12px solid transparent;
529
+ border-top: 12px solid rgba(255, 255, 255, 0.95);
530
+ }
531
+
532
+ .speech-text {
533
+ font-size: 1.4em;
534
+ font-weight: bold;
535
+ color: #2c3e50;
536
+ margin-bottom: 8px;
537
+ font-family: 'Comic Neue', cursive;
538
+ line-height: 1.3;
539
+ }
540
+
541
+ .speech-confidence {
542
+ font-size: 0.9em;
543
+ color: #7f8c8d;
544
+ font-style: italic;
545
+ }
546
+
547
+ @keyframes speechBubble {
548
+ 0% {
549
+ opacity: 0;
550
+ transform: scale(0.3) translateY(20px);
551
+ }
552
+ 50% {
553
+ transform: scale(1.1) translateY(-5px);
554
+ }
555
+ 100% {
556
+ opacity: 1;
557
+ transform: scale(1) translateY(0);
558
+ }
559
+ }
560
+
561
+ /* 语音识别状态动画 */
562
+ .speech-bubble.listening {
563
+ border-color: #e74c3c;
564
+ animation: listening-pulse 1.5s infinite;
565
+ }
566
+
567
+ .speech-bubble.processing {
568
+ border-color: #f39c12;
569
+ animation: processing-spin 2s infinite linear;
570
+ }
571
+
572
+ .speech-bubble.recognized {
573
+ border-color: #27ae60;
574
+ animation: recognized-bounce 0.6s ease-out;
575
+ }
576
+
577
+ @keyframes listening-pulse {
578
+ 0%, 100% {
579
+ border-color: #e74c3c;
580
+ box-shadow: 0 8px 32px rgba(231, 76, 60, 0.2);
581
+ }
582
+ 50% {
583
+ border-color: #c0392b;
584
+ box-shadow: 0 8px 32px rgba(231, 76, 60, 0.4);
585
+ }
586
+ }
587
+
588
+ @keyframes processing-spin {
589
+ 0% {
590
+ border-color: #f39c12;
591
+ transform: rotate(0deg);
592
+ }
593
+ 100% {
594
+ border-color: #e67e22;
595
+ transform: rotate(360deg);
596
+ }
597
+ }
598
+
599
+ @keyframes recognized-bounce {
600
+ 0% { transform: scale(1); }
601
+ 30% { transform: scale(1.1); }
602
+ 60% { transform: scale(0.95); }
603
+ 100% { transform: scale(1); }
604
+ }
605
+
606
+ /* 帮助模态框 */
607
+ .help-modal {
608
+ position: fixed;
609
+ top: 0;
610
+ left: 0;
611
+ width: 100%;
612
+ height: 100%;
613
+ z-index: 10000;
614
+ animation: modalFadeIn 0.3s ease-out;
615
+ }
616
+
617
+ .modal-overlay {
618
+ position: absolute;
619
+ top: 0;
620
+ left: 0;
621
+ width: 100%;
622
+ height: 100%;
623
+ background: rgba(0, 0, 0, 0.5);
624
+ backdrop-filter: blur(5px);
625
+ }
626
+
627
+ .modal-content {
628
+ position: absolute;
629
+ top: 50%;
630
+ left: 50%;
631
+ transform: translate(-50%, -50%);
632
+ background: white;
633
+ border-radius: 20px;
634
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
635
+ max-width: 600px;
636
+ max-height: 80vh;
637
+ overflow: hidden;
638
+ font-family: 'Comic Neue', cursive;
639
+ }
640
+
641
+ .modal-header {
642
+ background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
643
+ color: white;
644
+ padding: 20px 30px;
645
+ display: flex;
646
+ justify-content: space-between;
647
+ align-items: center;
648
+ }
649
+
650
+ .modal-header h2 {
651
+ margin: 0;
652
+ font-family: 'Fredoka One', cursive;
653
+ font-size: 1.5em;
654
+ }
655
+
656
+ .modal-close {
657
+ background: none;
658
+ border: none;
659
+ color: white;
660
+ font-size: 24px;
661
+ cursor: pointer;
662
+ padding: 5px;
663
+ border-radius: 50%;
664
+ width: 35px;
665
+ height: 35px;
666
+ display: flex;
667
+ align-items: center;
668
+ justify-content: center;
669
+ transition: background 0.3s ease;
670
+ }
671
+
672
+ .modal-close:hover {
673
+ background: rgba(255, 255, 255, 0.2);
674
+ }
675
+
676
+ .modal-body {
677
+ padding: 30px;
678
+ overflow-y: auto;
679
+ max-height: calc(80vh - 100px);
680
+ }
681
+
682
+ .help-section {
683
+ margin-bottom: 25px;
684
+ padding-bottom: 20px;
685
+ border-bottom: 2px solid #f0f0f0;
686
+ }
687
+
688
+ .help-section:last-child {
689
+ border-bottom: none;
690
+ margin-bottom: 0;
691
+ }
692
+
693
+ .help-section h4 {
694
+ color: #ff6b6b;
695
+ margin-bottom: 10px;
696
+ font-size: 1.2em;
697
+ }
698
+
699
+ .help-section p {
700
+ margin-bottom: 10px;
701
+ line-height: 1.5;
702
+ color: #2c3e50;
703
+ }
704
+
705
+ .help-section ul {
706
+ margin: 10px 0;
707
+ padding-left: 20px;
708
+ }
709
+
710
+ .help-section li {
711
+ margin-bottom: 5px;
712
+ color: #34495e;
713
+ line-height: 1.4;
714
+ }
715
+
716
+ .help-section strong {
717
+ color: #e74c3c;
718
+ }
719
+
720
+ @keyframes modalFadeIn {
721
+ 0% {
722
+ opacity: 0;
723
+ transform: scale(0.9);
724
+ }
725
+ 100% {
726
+ opacity: 1;
727
+ transform: scale(1);
728
+ }
729
+ }
730
+
731
+ @keyframes modalFadeOut {
732
+ 0% {
733
+ opacity: 1;
734
+ transform: scale(1);
735
+ }
736
+ 100% {
737
+ opacity: 0;
738
+ transform: scale(0.9);
739
+ }
740
+ }
741
+
742
+ /* 特殊消息动画 */
743
+ @keyframes specialMessage {
744
+ 0% {
745
+ opacity: 0;
746
+ transform: translate(-50%, -50%) scale(0.3);
747
+ }
748
+ 20% {
749
+ opacity: 1;
750
+ transform: translate(-50%, -50%) scale(1.1);
751
+ }
752
+ 30% {
753
+ transform: translate(-50%, -50%) scale(1);
754
+ }
755
+ 70% {
756
+ opacity: 1;
757
+ transform: translate(-50%, -50%) scale(1);
758
+ }
759
+ 100% {
760
+ opacity: 0;
761
+ transform: translate(-50%, -50%) scale(0.8);
762
+ }
763
+ }
764
+
765
+ /* 键盘输入调试面板 */
766
+ .keyboard-input-panel {
767
+ position: fixed;
768
+ bottom: 20px;
769
+ right: 20px;
770
+ background: rgba(44, 62, 80, 0.95);
771
+ border: 2px solid #4ecdc4;
772
+ border-radius: 15px;
773
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
774
+ backdrop-filter: blur(10px);
775
+ color: white;
776
+ font-family: 'Comic Neue', cursive;
777
+ min-width: 300px;
778
+ max-width: 400px;
779
+ transition: all 0.4s ease;
780
+ z-index: 50;
781
+ pointer-events: auto;
782
+ cursor: move;
783
+ user-select: none;
784
+ }
785
+
786
+ /* 面板激活状态 */
787
+ .keyboard-input-panel.active {
788
+ z-index: 10000 !important;
789
+ transform: scale(1.02);
790
+ box-shadow: 0 12px 40px rgba(78, 205, 196, 0.4);
791
+ border-color: #ff6b6b;
792
+ background: rgba(44, 62, 80, 0.98);
793
+ pointer-events: auto !important;
794
+ }
795
+
796
+ /* 拖拽状态 */
797
+ .keyboard-input-panel.dragging {
798
+ transition: none !important;
799
+ transform: scale(1.05) rotate(2deg);
800
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
801
+ z-index: 15000 !important;
802
+ }
803
+
804
+ .keyboard-input-panel.dragging .input-content {
805
+ pointer-events: none;
806
+ }
807
+
808
+ .keyboard-input-panel.dragging .input-header {
809
+ background: linear-gradient(45deg, #ff6b6b, #e74c3c);
810
+ }
811
+
812
+ /* 面板非激活状态(灰色透明) */
813
+ .keyboard-input-panel.inactive {
814
+ opacity: 0.4;
815
+ background: rgba(108, 117, 125, 0.7);
816
+ border-color: #6c757d;
817
+ transform: scale(0.95);
818
+ pointer-events: none;
819
+ }
820
+
821
+ .keyboard-input-panel.inactive .input-header {
822
+ background: linear-gradient(45deg, #6c757d, #495057);
823
+ }
824
+
825
+ .keyboard-input-panel.inactive .command-input {
826
+ border-color: #6c757d;
827
+ background: rgba(108, 117, 125, 0.3);
828
+ color: rgba(255, 255, 255, 0.6);
829
+ }
830
+
831
+ .keyboard-input-panel.inactive .send-btn {
832
+ background: linear-gradient(45deg, #6c757d, #495057);
833
+ opacity: 0.6;
834
+ }
835
+
836
+ .keyboard-input-panel.inactive .quick-cmd {
837
+ border-color: #6c757d;
838
+ background: rgba(108, 117, 125, 0.2);
839
+ color: rgba(255, 255, 255, 0.6);
840
+ }
841
+
842
+ .keyboard-input-panel:hover:not(.inactive) {
843
+ transform: translateY(-5px);
844
+ box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
845
+ }
846
+
847
+ .keyboard-input-panel.active:hover {
848
+ transform: scale(1.02) translateY(-5px);
849
+ box-shadow: 0 16px 50px rgba(78, 205, 196, 0.5);
850
+ }
851
+
852
+ .input-header {
853
+ background: linear-gradient(45deg, #4ecdc4, #44a08d);
854
+ padding: 15px 20px;
855
+ border-radius: 13px 13px 0 0;
856
+ display: flex;
857
+ justify-content: space-between;
858
+ align-items: center;
859
+ border-bottom: 2px solid rgba(255, 255, 255, 0.1);
860
+ cursor: move;
861
+ user-select: none;
862
+ }
863
+
864
+ .input-header h4 {
865
+ margin: 0;
866
+ font-size: 1.1em;
867
+ font-weight: bold;
868
+ color: white;
869
+ text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
870
+ }
871
+
872
+ .toggle-panel-btn {
873
+ background: rgba(255, 255, 255, 0.2);
874
+ border: none;
875
+ border-radius: 50%;
876
+ width: 35px;
877
+ height: 35px;
878
+ color: white;
879
+ font-size: 16px;
880
+ cursor: pointer;
881
+ transition: all 0.3s ease;
882
+ display: flex;
883
+ align-items: center;
884
+ justify-content: center;
885
+ }
886
+
887
+ .toggle-panel-btn:hover {
888
+ background: rgba(255, 255, 255, 0.3);
889
+ transform: scale(1.1);
890
+ }
891
+
892
+ .toggle-panel-btn:active {
893
+ transform: scale(0.95);
894
+ }
895
+
896
+ .input-content {
897
+ padding: 20px;
898
+ transition: all 0.3s ease;
899
+ }
900
+
901
+ .input-content.collapsed {
902
+ display: none;
903
+ }
904
+
905
+ .input-group {
906
+ display: flex;
907
+ gap: 10px;
908
+ margin-bottom: 20px;
909
+ }
910
+
911
+ .command-input {
912
+ flex: 1;
913
+ padding: 12px 15px;
914
+ border: 2px solid #4ecdc4;
915
+ border-radius: 25px;
916
+ background: rgba(255, 255, 255, 0.1);
917
+ color: white;
918
+ font-family: 'Comic Neue', cursive;
919
+ font-size: 14px;
920
+ outline: none;
921
+ transition: all 0.3s ease;
922
+ }
923
+
924
+ .command-input::placeholder {
925
+ color: rgba(255, 255, 255, 0.7);
926
+ }
927
+
928
+ .command-input:focus {
929
+ border-color: #ff6b6b;
930
+ background: rgba(255, 255, 255, 0.2);
931
+ box-shadow: 0 0 15px rgba(255, 107, 107, 0.3);
932
+ }
933
+
934
+ .send-btn {
935
+ background: linear-gradient(45deg, #ff6b6b, #e55555);
936
+ border: none;
937
+ border-radius: 25px;
938
+ padding: 12px 20px;
939
+ color: white;
940
+ font-family: 'Comic Neue', cursive;
941
+ font-weight: bold;
942
+ font-size: 14px;
943
+ cursor: pointer;
944
+ transition: all 0.3s ease;
945
+ box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
946
+ }
947
+
948
+ .send-btn:hover {
949
+ background: linear-gradient(45deg, #e55555, #d63447);
950
+ transform: translateY(-2px);
951
+ box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
952
+ }
953
+
954
+ .send-btn:active {
955
+ transform: translateY(0);
956
+ }
957
+
958
+ .quick-commands {
959
+ margin-bottom: 20px;
960
+ }
961
+
962
+ .quick-commands h5 {
963
+ margin: 0 0 10px 0;
964
+ color: #4ecdc4;
965
+ font-size: 0.9em;
966
+ font-weight: bold;
967
+ text-transform: uppercase;
968
+ letter-spacing: 1px;
969
+ }
970
+
971
+ .command-buttons {
972
+ display: grid;
973
+ grid-template-columns: 1fr 1fr;
974
+ gap: 8px;
975
+ }
976
+
977
+ .quick-cmd {
978
+ background: rgba(78, 205, 196, 0.2);
979
+ border: 1px solid #4ecdc4;
980
+ border-radius: 20px;
981
+ padding: 8px 12px;
982
+ color: white;
983
+ font-family: 'Comic Neue', cursive;
984
+ font-size: 0.8em;
985
+ cursor: pointer;
986
+ transition: all 0.3s ease;
987
+ text-align: center;
988
+ }
989
+
990
+ .quick-cmd:hover {
991
+ background: rgba(78, 205, 196, 0.4);
992
+ border-color: #44a08d;
993
+ transform: scale(1.05);
994
+ }
995
+
996
+ .quick-cmd:active {
997
+ transform: scale(0.95);
998
+ }
999
+
1000
+ .command-history {
1001
+ border-top: 1px solid rgba(255, 255, 255, 0.1);
1002
+ padding-top: 15px;
1003
+ }
1004
+
1005
+ .command-history h5 {
1006
+ margin: 0 0 10px 0;
1007
+ color: #4ecdc4;
1008
+ font-size: 0.9em;
1009
+ font-weight: bold;
1010
+ text-transform: uppercase;
1011
+ letter-spacing: 1px;
1012
+ }
1013
+
1014
+ .history-list {
1015
+ max-height: 120px;
1016
+ overflow-y: auto;
1017
+ border-radius: 8px;
1018
+ background: rgba(0, 0, 0, 0.2);
1019
+ padding: 8px;
1020
+ }
1021
+
1022
+ .history-item {
1023
+ background: rgba(255, 255, 255, 0.1);
1024
+ border-radius: 15px;
1025
+ padding: 6px 12px;
1026
+ margin-bottom: 5px;
1027
+ font-size: 0.8em;
1028
+ color: rgba(255, 255, 255, 0.9);
1029
+ display: flex;
1030
+ justify-content: space-between;
1031
+ align-items: center;
1032
+ cursor: pointer;
1033
+ transition: all 0.3s ease;
1034
+ }
1035
+
1036
+ .history-item:hover {
1037
+ background: rgba(255, 255, 255, 0.2);
1038
+ transform: translateX(5px);
1039
+ }
1040
+
1041
+ .history-item:last-child {
1042
+ margin-bottom: 0;
1043
+ }
1044
+
1045
+ .history-command {
1046
+ flex: 1;
1047
+ white-space: nowrap;
1048
+ overflow: hidden;
1049
+ text-overflow: ellipsis;
1050
+ }
1051
+
1052
+ .history-time {
1053
+ color: rgba(255, 255, 255, 0.6);
1054
+ font-size: 0.7em;
1055
+ margin-left: 10px;
1056
+ }
1057
+
1058
+ /* 滚动条样式 */
1059
+ .history-list::-webkit-scrollbar {
1060
+ width: 4px;
1061
+ }
1062
+
1063
+ .history-list::-webkit-scrollbar-track {
1064
+ background: rgba(255, 255, 255, 0.1);
1065
+ border-radius: 2px;
1066
+ }
1067
+
1068
+ .history-list::-webkit-scrollbar-thumb {
1069
+ background: #4ecdc4;
1070
+ border-radius: 2px;
1071
+ }
1072
+
1073
+ .history-list::-webkit-scrollbar-thumb:hover {
1074
+ background: #44a08d;
1075
+ }
1076
+
1077
+ /* 面板折叠状态 */
1078
+ .keyboard-input-panel.collapsed {
1079
+ min-width: auto;
1080
+ width: auto;
1081
+ }
1082
+
1083
+ .keyboard-input-panel.collapsed .input-content {
1084
+ display: none;
1085
+ }
1086
+
1087
+ /* 面板动画 */
1088
+ @keyframes panelSlideIn {
1089
+ from {
1090
+ transform: translateX(100%);
1091
+ opacity: 0;
1092
+ }
1093
+ to {
1094
+ transform: translateX(0);
1095
+ opacity: 1;
1096
+ }
1097
+ }
1098
+
1099
+ .keyboard-input-panel {
1100
+ animation: panelSlideIn 0.5s ease-out;
1101
+ }
1102
+
1103
+ /* 响应式设计 */
1104
+ @media (max-width: 768px) {
1105
+ .keyboard-input-panel {
1106
+ bottom: 10px;
1107
+ right: 10px;
1108
+ left: 10px;
1109
+ min-width: auto;
1110
+ max-width: none;
1111
+ }
1112
+
1113
+ .command-buttons {
1114
+ grid-template-columns: 1fr;
1115
+ }
1116
+
1117
+ .input-group {
1118
+ flex-direction: column;
1119
+ }
1120
+
1121
+ .send-btn {
1122
+ align-self: flex-end;
1123
+ min-width: 100px;
1124
+ }
1125
+ }
docker-README.md ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # MagicPot Docker 部署指南
2
+
3
+ ## 项目简介
4
+ MagicPot是一个卡通手绘风格的语音交互魔法锅游戏,已经Docker化便于部署。
5
+
6
+ ## 快速开始
7
+
8
+ ### 1. 构建Docker镜像
9
+ ```bash
10
+ docker build -t magicpot-game .
11
+ ```
12
+
13
+ ### 2. 运行容器
14
+ ```bash
15
+ docker run -d -p 8080:80 --name magicpot magicpot-game
16
+ ```
17
+
18
+ ### 3. 访问游戏
19
+ 打开浏览器访问:http://localhost:8080
20
+
21
+ ## Docker命令详解
22
+
23
+ ### 构建镜像
24
+ ```bash
25
+ # 基础构建
26
+ docker build -t magicpot-game .
27
+
28
+ # 指定标签版本
29
+ docker build -t magicpot-game:v1.0 .
30
+
31
+ # 查看镜像
32
+ docker images | grep magicpot
33
+ ```
34
+
35
+ ### 运行容器
36
+ ```bash
37
+ # 后台运行(推荐)
38
+ docker run -d -p 8080:80 --name magicpot magicpot-game
39
+
40
+ # 前台运行(调试用)
41
+ docker run -p 8080:80 --name magicpot magicpot-game
42
+
43
+ # 自定义端口
44
+ docker run -d -p 3000:80 --name magicpot magicpot-game
45
+
46
+ # 运行后访问:http://localhost:3000
47
+ ```
48
+
49
+ ### 容器管理
50
+ ```bash
51
+ # 查看运行中的容器
52
+ docker ps
53
+
54
+ # 查看所有容器
55
+ docker ps -a
56
+
57
+ # 停止容器
58
+ docker stop magicpot
59
+
60
+ # 启动容器
61
+ docker start magicpot
62
+
63
+ # 重启容器
64
+ docker restart magicpot
65
+
66
+ # 删除容器
67
+ docker rm magicpot
68
+
69
+ # 查看容器日志
70
+ docker logs magicpot
71
+
72
+ # 进入容器(调试用)
73
+ docker exec -it magicpot sh
74
+ ```
75
+
76
+ ## Docker Compose(可选)
77
+
78
+ 创建 `docker-compose.yml` 文件:
79
+
80
+ ```yaml
81
+ version: '3.8'
82
+
83
+ services:
84
+ magicpot:
85
+ build: .
86
+ ports:
87
+ - "8080:80"
88
+ restart: unless-stopped
89
+ container_name: magicpot-game
90
+ ```
91
+
92
+ 使用Docker Compose运行:
93
+ ```bash
94
+ # 构建并启动
95
+ docker-compose up -d
96
+
97
+ # 停止
98
+ docker-compose down
99
+
100
+ # 查看日志
101
+ docker-compose logs
102
+ ```
103
+
104
+ ## 技术栈
105
+ - **Base Image**: nginx:alpine
106
+ - **Web Server**: Nginx
107
+ - **Frontend**: HTML5 + CSS3 + JavaScript
108
+ - **Features**: 语音识别、Canvas动画、粒子系统
109
+
110
+ ## 配置说明
111
+
112
+ ### Nginx配置
113
+ - 静态文件缓存:1年
114
+ - HTML文件缓存:1小时
115
+ - 启用Gzip压缩
116
+ - 安全头设置
117
+ - 支持音频文件(.mp3)
118
+
119
+ ### 端口配置
120
+ - 容器内部端口:80
121
+ - 建议外部端口:8080(可自定义)
122
+
123
+ ## 故障排除
124
+
125
+ ### 常见问题
126
+
127
+ 1. **端口被占用**
128
+ ```bash
129
+ # 检查端口使用情况
130
+ netstat -an | grep :8080
131
+ # 或使用其他端口
132
+ docker run -d -p 3000:80 --name magicpot magicpot-game
133
+ ```
134
+
135
+ 2. **容器启动失败**
136
+ ```bash
137
+ # 查看详细错误信息
138
+ docker logs magicpot
139
+
140
+ # 检查容器状态
141
+ docker ps -a
142
+ ```
143
+
144
+ 3. **游戏无法正常运行**
145
+ - 确保浏览器支持Web Speech API
146
+ - 检查麦克风权限设置
147
+ - 尝试使用HTTPS(语音识别需要)
148
+
149
+ ### 性能优化
150
+
151
+ 1. **镜像大小优化**
152
+ - 使用alpine版本nginx(已实现)
153
+ - 清理不必要的文件(.dockerignore已配置)
154
+
155
+ 2. **运行时优化**
156
+ ```bash
157
+ # 限制内存使用
158
+ docker run -d -p 8080:80 -m 512m --name magicpot magicpot-game
159
+
160
+ # 设置CPU限制
161
+ docker run -d -p 8080:80 --cpus="1.0" --name magicpot magicpot-game
162
+ ```
163
+
164
+ ## 开发环境
165
+
166
+ 如果需要在开发环境中运行:
167
+ ```bash
168
+ # 本地开发(不使用Docker)
169
+ npm install
170
+ npm run dev
171
+
172
+ # 或使用Python
173
+ python -m http.server 8000
174
+ ```
175
+
176
+ ## 版本信息
177
+ - Docker Image: magicpot-game:latest
178
+ - Nginx Version: alpine
179
+ - Node.js Version: >=14.0.0(仅开发环境需要)
index.html ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>MagicPot - 魔法锅游戏</title>
7
+ <link rel="stylesheet" href="css/style.css">
8
+ <link href="https://fonts.googleapis.com/css2?family=Fredoka+One:wght@400&family=Comic+Neue:wght@400;700&display=swap" rel="stylesheet">
9
+ </head>
10
+ <body>
11
+ <div class="game-container">
12
+ <!-- 游戏标题 -->
13
+ <header class="game-header">
14
+ <h1 class="game-title">🪄 MagicPot 魔法锅 ✨</h1>
15
+ <div class="header-controls">
16
+ <button class="audio-button" id="audioButton" title="音效开关">🔊</button>
17
+ <button class="help-button" id="helpButton" title="语音命令帮助">❓</button>
18
+ <div class="voice-status" id="voiceStatus">
19
+ <span class="status-text">准备中...</span>
20
+ <div class="mic-icon">🎤</div>
21
+ </div>
22
+ </div>
23
+ </header>
24
+
25
+ <!-- 主游戏区域 -->
26
+ <main class="game-main">
27
+ <canvas id="gameCanvas" width="800" height="600"></canvas>
28
+
29
+ <!-- 游戏UI覆盖层 -->
30
+ <div class="game-ui">
31
+ <!-- 魔法锅状态显示 -->
32
+ <div class="pot-status" id="potStatus">
33
+ <div class="status-indicator inactive">魔法锅休眠中</div>
34
+ </div>
35
+
36
+ <!-- 当前烹饪的食物 -->
37
+ <div class="cooking-info" id="cookingInfo" style="display: none;">
38
+ <div class="cooking-food">
39
+ <span>正在烹饪:</span>
40
+ <span id="currentFood">-</span>
41
+ </div>
42
+ <div class="cooking-progress">
43
+ <div class="progress-bar">
44
+ <div class="progress-fill" id="progressFill"></div>
45
+ </div>
46
+ </div>
47
+ </div>
48
+
49
+ <!-- 食物计数器 -->
50
+ <div class="food-counter" id="foodCounter" style="display: none;">
51
+ <div class="counter-item">
52
+ <span class="food-emoji" id="foodEmoji">🍎</span>
53
+ <span class="count" id="foodCount">0</span>
54
+ </div>
55
+ </div>
56
+
57
+ <!-- 性能监控 -->
58
+ <div class="performance-monitor" id="performanceMonitor" style="display: none;">
59
+ <div class="perf-item">
60
+ <span class="perf-label">粒子:</span>
61
+ <span class="perf-value" id="particleCount">0</span>
62
+ </div>
63
+ <div class="perf-item">
64
+ <span class="perf-label">动物:</span>
65
+ <span class="perf-value" id="animalCount">0</span>
66
+ </div>
67
+ <div class="perf-item">
68
+ <span class="perf-label">FPS:</span>
69
+ <span class="perf-value" id="fpsCount">60</span>
70
+ </div>
71
+ </div>
72
+
73
+ <!-- 音频激活提示 -->
74
+ <div class="audio-activation-hint" id="audioActivationHint" style="display: none;">
75
+ <div class="hint-bubble">
76
+ <div class="hint-icon">🔊</div>
77
+ <div class="hint-text">点击任意位置启动音效</div>
78
+ </div>
79
+ </div>
80
+
81
+ <!-- 语音输入显示 -->
82
+ <div class="voice-input-display" id="voiceInputDisplay" style="display: none;">
83
+ <div class="speech-bubble">
84
+ <div class="speech-text" id="speechText">正在听取...</div>
85
+ <div class="speech-confidence" id="speechConfidence"></div>
86
+ </div>
87
+ </div>
88
+
89
+ <!-- 语音提示 -->
90
+ <div class="voice-hints" id="voiceHints">
91
+ <div class="hint-item">
92
+ <span class="hint-command">"cook cook cook pot"</span>
93
+ <span class="hint-desc">激活魔法锅</span>
94
+ </div>
95
+ <div class="hint-item" style="display: none;" id="foodHint">
96
+ <span class="hint-command">说出食物名称</span>
97
+ <span class="hint-desc">开始烹饪</span>
98
+ </div>
99
+ <div class="hint-item" style="display: none;" id="stopHint">
100
+ <span class="hint-command">"stop stop stop pot"</span>
101
+ <span class="hint-desc">停止烹饪</span>
102
+ </div>
103
+ </div>
104
+
105
+ <!-- 键盘输入调试面板 -->
106
+ <div class="keyboard-input-panel" id="keyboardInputPanel">
107
+ <div class="input-header">
108
+ <h4>🎮 键盘调试输入</h4>
109
+ <button class="toggle-panel-btn" id="toggleKeyboardPanel" title="切换键盘输入面板">⌨️</button>
110
+ </div>
111
+ <div class="input-content" id="keyboardInputContent">
112
+ <div class="input-group">
113
+ <input type="text"
114
+ id="commandInput"
115
+ class="command-input"
116
+ placeholder="输入指令,如:cook cook cook pot"
117
+ autocomplete="off">
118
+ <button class="send-btn" id="sendCommandBtn">发送</button>
119
+ </div>
120
+
121
+ <div class="quick-commands">
122
+ <h5>快捷指令:</h5>
123
+ <div class="command-buttons">
124
+ <button class="quick-cmd" data-command="cook cook cook pot">激活魔法锅</button>
125
+ <button class="quick-cmd" data-command="stop stop stop pot">停止烹饪</button>
126
+ <button class="quick-cmd" data-command="apple">苹果</button>
127
+ <button class="quick-cmd" data-command="banana">香蕉</button>
128
+ <button class="quick-cmd" data-command="pizza">披萨</button>
129
+ <button class="quick-cmd" data-command="cake">蛋糕</button>
130
+ </div>
131
+ </div>
132
+
133
+ <div class="command-history">
134
+ <h5>命令历史:</h5>
135
+ <div class="history-list" id="commandHistory"></div>
136
+ </div>
137
+
138
+ <!-- 快捷键提示 -->
139
+ <div class="keyboard-shortcuts-hint">
140
+ 快捷键: F9(激活显示) | F8(显示/隐藏) | 1-6(快速命令) | 可拖拽标题栏
141
+ </div>
142
+ </div>
143
+ </div>
144
+ </div>
145
+ </main>
146
+
147
+ <!-- 调试信息 -->
148
+ <div class="debug-info" id="debugInfo" style="display: none;">
149
+ <h3>调试信息</h3>
150
+ <div id="debugLog"></div>
151
+ </div>
152
+ </div>
153
+
154
+ <!-- 加载脚本 -->
155
+ <script src="js/audio.js"></script>
156
+ <script src="js/voice.js"></script>
157
+ <script src="js/keyboard.js"></script>
158
+ <script src="js/particles.js"></script>
159
+ <script src="js/animals.js"></script>
160
+ <script src="js/pot.js"></script>
161
+ <script src="js/textToImage.js"></script>
162
+ <script src="js/main.js"></script>
163
+ </body>
164
+ </html>
js/animals.js ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 动物系统类
2
+ class AnimalSystem {
3
+ constructor(canvas) {
4
+ this.animals = [];
5
+ this.maxAnimals = 8; // 降低最大动物数量
6
+ this.canvas = canvas || document.getElementById('gameCanvas');
7
+ }
8
+
9
+ update(deltaTime) {
10
+ const dt = deltaTime * 0.001;
11
+
12
+ for (let i = this.animals.length - 1; i >= 0; i--) {
13
+ const animal = this.animals[i];
14
+ this.updateAnimal(animal, dt);
15
+
16
+ // 移除离开屏幕或生命周期结束的动物
17
+ if (animal.shouldRemove) {
18
+ this.animals.splice(i, 1);
19
+ }
20
+ }
21
+ }
22
+
23
+ updateAnimal(animal, dt) {
24
+ switch (animal.state) {
25
+ case 'entering':
26
+ this.updateEntering(animal, dt);
27
+ break;
28
+ case 'seeking':
29
+ this.updateSeeking(animal, dt);
30
+ break;
31
+ case 'eating':
32
+ this.updateEating(animal, dt);
33
+ break;
34
+ case 'satisfied':
35
+ this.updateSatisfied(animal, dt);
36
+ break;
37
+ case 'leaving':
38
+ this.updateLeaving(animal, dt);
39
+ break;
40
+ }
41
+
42
+ // 更新动画
43
+ animal.animationTime += dt;
44
+
45
+ // 更新位置
46
+ animal.x += animal.vx * dt;
47
+ animal.y += animal.vy * dt;
48
+
49
+ // 更新生命周期
50
+ animal.lifeTime += dt;
51
+ if (animal.maxLifeTime && animal.lifeTime > animal.maxLifeTime) {
52
+ animal.shouldRemove = true;
53
+ }
54
+ }
55
+
56
+ updateEntering(animal, dt) {
57
+ // 动物从屏幕边缘进入 - 使用显示尺寸而不是内部像素尺寸
58
+ const rect = this.canvas ? this.canvas.getBoundingClientRect() : { width: 800, height: 600 };
59
+ const displayWidth = rect.width;
60
+ const displayHeight = rect.height;
61
+
62
+ const targetX = animal.side === 'left' ? 100 : displayWidth - 100;
63
+ const targetY = displayHeight - 100; // 距离底部100像素
64
+
65
+ console.log(`Canvas显示尺寸: ${displayWidth}x${displayHeight}, 目标位置: (${targetX}, ${targetY})`);
66
+
67
+ const dx = targetX - animal.x;
68
+ const dy = targetY - animal.y;
69
+ const distance = Math.sqrt(dx * dx + dy * dy);
70
+
71
+ console.log(`动物${animal.type}进入中: 当前位置(${animal.x.toFixed(1)}, ${animal.y.toFixed(1)}), 目标位置(${targetX}, ${targetY}), 距离: ${distance.toFixed(1)}`);
72
+
73
+ if (distance < 20) {
74
+ animal.state = 'seeking';
75
+ animal.vx = 0;
76
+ animal.vy = 0;
77
+ console.log(`动物${animal.type}已进入屏幕,状态改为seeking`);
78
+ } else {
79
+ animal.vx = (dx / distance) * animal.speed;
80
+ animal.vy = (dy / distance) * animal.speed;
81
+ }
82
+ }
83
+
84
+ updateSeeking(animal, dt) {
85
+ // 寻找食物
86
+ const food = this.findNearestFood(animal);
87
+
88
+ if (food) {
89
+ console.log(`${animal.type}找到食物: ${food.foodType}, 距离: ${Math.sqrt((food.x - animal.x)**2 + (food.y - animal.y)**2).toFixed(1)}`);
90
+ const dx = food.x - animal.x;
91
+ const dy = food.y - animal.y;
92
+ const distance = Math.sqrt(dx * dx + dy * dy);
93
+
94
+ if (distance < 30) {
95
+ // 开始吃食物
96
+ animal.state = 'eating';
97
+ animal.targetFood = food;
98
+ animal.eatingTime = 0;
99
+ animal.vx = 0;
100
+ animal.vy = 0;
101
+ console.log(`${animal.type}开始吃食物!`);
102
+ } else {
103
+ // 移动向食物
104
+ animal.vx = (dx / distance) * animal.speed * 0.8;
105
+ animal.vy = (dy / distance) * animal.speed * 0.8;
106
+ }
107
+ } else {
108
+ // 没有找到食物,随机移动
109
+ if (Math.random() < 0.1) { // 增加调试输出频率
110
+ console.log(`${animal.type}没有找到食物,喜欢: ${animal.preferredFood}, 随机移动`);
111
+ animal.vx = (Math.random() - 0.5) * animal.speed * 0.5;
112
+ animal.vy = (Math.random() - 0.5) * animal.speed * 0.5;
113
+ }
114
+ }
115
+
116
+ // 如果寻找时间太长,离开
117
+ if (animal.lifeTime > 10) {
118
+ animal.state = 'leaving';
119
+ }
120
+ }
121
+
122
+ updateEating(animal, dt) {
123
+ animal.eatingTime += dt;
124
+
125
+ // 吃食物动画
126
+ if (animal.eatingTime > 0.5 && animal.targetFood) {
127
+ // 移除食物粒子
128
+ this.removeFoodParticle(animal.targetFood);
129
+ animal.targetFood = null;
130
+ animal.foodEaten++;
131
+ }
132
+
133
+ if (animal.eatingTime > 1) {
134
+ if (animal.foodEaten >= animal.maxFood) {
135
+ animal.state = 'satisfied';
136
+ } else {
137
+ animal.state = 'seeking';
138
+ }
139
+ }
140
+ }
141
+
142
+ updateSatisfied(animal, dt) {
143
+ // 满足后的短暂停留
144
+ animal.satisfiedTime = (animal.satisfiedTime || 0) + dt;
145
+
146
+ if (animal.satisfiedTime > 2) {
147
+ animal.state = 'leaving';
148
+ }
149
+ }
150
+
151
+ updateLeaving(animal, dt) {
152
+ // 离开屏幕
153
+ const exitX = animal.side === 'left' ? -50 : this.canvas.width + 50;
154
+ const dx = exitX - animal.x;
155
+ const distance = Math.abs(dx);
156
+
157
+ if (distance < 10) {
158
+ animal.shouldRemove = true;
159
+ } else {
160
+ animal.vx = (dx / distance) * animal.speed * 1.2;
161
+ animal.vy = 0;
162
+ }
163
+ }
164
+
165
+ findNearestFood(animal) {
166
+ // 从游戏主类获取粒子系统
167
+ if (!window.game || !window.game.particleSystem) return null;
168
+
169
+ const particles = window.game.particleSystem.particles;
170
+ let nearestFood = null;
171
+ let nearestDistance = Infinity;
172
+ let totalFood = 0;
173
+ let groundedFood = 0;
174
+ let matchingFood = 0;
175
+
176
+ particles.forEach(particle => {
177
+ if (particle.type === 'food') {
178
+ totalFood++;
179
+ if (particle.isGrounded) {
180
+ groundedFood++;
181
+ if (particle.foodType === animal.preferredFood) {
182
+ matchingFood++;
183
+ const dx = particle.x - animal.x;
184
+ const dy = particle.y - animal.y;
185
+ const distance = Math.sqrt(dx * dx + dy * dy);
186
+
187
+ if (distance < nearestDistance) {
188
+ nearestDistance = distance;
189
+ nearestFood = particle;
190
+ }
191
+ }
192
+ }
193
+ }
194
+ });
195
+
196
+ // 每隔一段时间输出调试信息
197
+ if (Math.random() < 0.01) {
198
+ console.log(`${animal.type}寻找食物: 总食物=${totalFood}, 落地食物=${groundedFood}, 匹配食物=${matchingFood}, 喜欢=${animal.preferredFood}`);
199
+ if (totalFood > 0) {
200
+ console.log(`示例食物类型: ${particles.find(p => p.type === 'food')?.foodType}`);
201
+ }
202
+ }
203
+
204
+ return nearestFood;
205
+ }
206
+
207
+ removeFoodParticle(foodParticle) {
208
+ if (!window.game || !window.game.particleSystem) return;
209
+
210
+ const particles = window.game.particleSystem.particles;
211
+ const index = particles.indexOf(foodParticle);
212
+ if (index > -1) {
213
+ particles.splice(index, 1);
214
+
215
+ // 创建吃食物的特效
216
+ window.game.particleSystem.createSparkleParticles(
217
+ foodParticle.x,
218
+ foodParticle.y,
219
+ 5
220
+ );
221
+ }
222
+ }
223
+
224
+ createAnimal(foodType, side = 'left') {
225
+ if (this.animals.length >= this.maxAnimals) {
226
+ console.log(`已达到最大动物数量: ${this.maxAnimals}`);
227
+ return null;
228
+ }
229
+
230
+ const animalType = this.getAnimalForFood(foodType);
231
+ // 使用显示尺寸而不是内部像素尺寸
232
+ const rect = this.canvas ? this.canvas.getBoundingClientRect() : { width: 800, height: 600 };
233
+ const displayWidth = rect.width;
234
+ const displayHeight = rect.height;
235
+ const startX = side === 'left' ? -50 : displayWidth + 50;
236
+ const startY = displayHeight - 60;
237
+
238
+ console.log(`Canvas显示尺寸: ${displayWidth}x${displayHeight}, 动物起始位置: (${startX}, ${startY})`);
239
+
240
+ const animal = {
241
+ x: startX,
242
+ y: startY,
243
+ vx: 0,
244
+ vy: 0,
245
+ type: animalType,
246
+ side: side,
247
+ state: 'entering',
248
+ speed: Math.random() * 30 + 40,
249
+ size: Math.random() * 20 + 30,
250
+ preferredFood: foodType,
251
+ foodEaten: 0,
252
+ maxFood: Math.floor(Math.random() * 3) + 2,
253
+ animationTime: 0,
254
+ lifeTime: 0,
255
+ maxLifeTime: 30, // 30秒后自动离开
256
+ shouldRemove: false,
257
+ color: this.getAnimalColor(animalType),
258
+ emoji: this.getAnimalEmoji(animalType)
259
+ };
260
+
261
+ this.animals.push(animal);
262
+ console.log(`动物已创建: ${animalType}, 位置: (${animal.x}, ${animal.y}), 当前动物总数: ${this.animals.length}`);
263
+ console.log(`动物详细信息:`, animal);
264
+ return animal;
265
+ }
266
+
267
+ createLittleGirl(side = 'right') {
268
+ // 使用显示尺寸而不是内部像素尺寸
269
+ const rect = this.canvas ? this.canvas.getBoundingClientRect() : { width: 800, height: 600 };
270
+ const displayWidth = rect.width;
271
+ const displayHeight = rect.height;
272
+ const startX = side === 'left' ? -50 : displayWidth + 50;
273
+ const startY = displayHeight - 80;
274
+
275
+ console.log(`小女孩Canvas显示尺寸: ${displayWidth}x${displayHeight}, 起始位置: (${startX}, ${startY})`);
276
+
277
+ const girl = {
278
+ x: startX,
279
+ y: startY,
280
+ vx: 0,
281
+ vy: 0,
282
+ type: 'girl',
283
+ side: side,
284
+ state: 'entering',
285
+ speed: 60,
286
+ size: 40,
287
+ preferredFood: ['strawberry', 'watermelon'],
288
+ foodEaten: 0,
289
+ maxFood: 5,
290
+ animationTime: 0,
291
+ lifeTime: 0,
292
+ maxLifeTime: 45,
293
+ shouldRemove: false,
294
+ color: '#FFB6C1',
295
+ emoji: '👧',
296
+ isSpecial: true
297
+ };
298
+
299
+ this.animals.push(girl);
300
+ console.log(`小女孩已创建: 位置: (${girl.x}, ${girl.y}), 当前动物总数: ${this.animals.length}`);
301
+ return girl;
302
+ }
303
+
304
+ getAnimalForFood(foodType) {
305
+ const animalMap = {
306
+ 'apple': 'rabbit',
307
+ 'banana': 'monkey',
308
+ 'orange': 'bird',
309
+ 'strawberry': 'bear',
310
+ 'watermelon': 'elephant',
311
+ 'grape': 'fox',
312
+ 'pizza': 'cat',
313
+ 'burger': 'dog',
314
+ 'cake': 'mouse',
315
+ 'cookie': 'squirrel',
316
+ 'bread': 'duck',
317
+ 'cheese': 'rat',
318
+ 'fish': 'cat',
319
+ 'chicken': 'dog',
320
+ 'rice': 'bird',
321
+ 'noodles': 'panda',
322
+ 'carrot': 'rabbit',
323
+ 'corn': 'chicken'
324
+ };
325
+
326
+ return animalMap[foodType] || 'cat';
327
+ }
328
+
329
+ getAnimalColor(animalType) {
330
+ const colorMap = {
331
+ 'rabbit': '#F5DEB3',
332
+ 'monkey': '#DEB887',
333
+ 'bird': '#87CEEB',
334
+ 'bear': '#8B4513',
335
+ 'elephant': '#A9A9A9',
336
+ 'fox': '#FF8C00',
337
+ 'cat': '#FFB6C1',
338
+ 'dog': '#DEB887',
339
+ 'mouse': '#C0C0C0',
340
+ 'squirrel': '#D2691E',
341
+ 'duck': '#FFFF00',
342
+ 'rat': '#808080',
343
+ 'panda': '#000000',
344
+ 'chicken': '#FFFF00'
345
+ };
346
+
347
+ return colorMap[animalType] || '#FFB6C1';
348
+ }
349
+
350
+ getAnimalEmoji(animalType) {
351
+ const emojiMap = {
352
+ 'rabbit': '🐰',
353
+ 'monkey': '🐵',
354
+ 'bird': '🐦',
355
+ 'bear': '🐻',
356
+ 'elephant': '🐘',
357
+ 'fox': '🦊',
358
+ 'cat': '🐱',
359
+ 'dog': '🐶',
360
+ 'mouse': '🐭',
361
+ 'squirrel': '🐿️',
362
+ 'duck': '🦆',
363
+ 'rat': '🐀',
364
+ 'panda': '🐼',
365
+ 'chicken': '🐔'
366
+ };
367
+
368
+ return emojiMap[animalType] || '🐱';
369
+ }
370
+
371
+ render(ctx) {
372
+ if (this.animals.length > 0) {
373
+ console.log(`渲染${this.animals.length}个动物`);
374
+ }
375
+ this.animals.forEach(animal => {
376
+ this.renderAnimal(ctx, animal);
377
+ });
378
+ }
379
+
380
+ renderAnimal(ctx, animal) {
381
+ ctx.save();
382
+
383
+ // 调试信息:在控制台显示渲染信息(仅首次)
384
+ if (!animal.debugRendered) {
385
+ console.log(`正在渲染动物: ${animal.type}, 位置: (${animal.x}, ${animal.y}), emoji: ${animal.emoji}, 尺寸: ${animal.size}`);
386
+ animal.debugRendered = true;
387
+ }
388
+
389
+ // 移动到动物位置
390
+ ctx.translate(animal.x, animal.y);
391
+
392
+ // 翻转方向
393
+ if (animal.vx < 0) {
394
+ ctx.scale(-1, 1);
395
+ }
396
+
397
+ // 绘制阴影
398
+ ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
399
+ ctx.beginPath();
400
+ ctx.ellipse(0, animal.size * 0.4, animal.size * 0.6, animal.size * 0.2, 0, 0, Math.PI * 2);
401
+ ctx.fill();
402
+
403
+ // 绘制动物主体
404
+ if (animal.emoji) {
405
+ ctx.font = `${animal.size}px Arial`;
406
+ ctx.textAlign = 'center';
407
+ ctx.textBaseline = 'middle';
408
+ ctx.fillStyle = '#000000';
409
+ ctx.fillText(animal.emoji, 0, -animal.size * 0.2);
410
+ } else {
411
+ // 备用:绘制简单的动物形状
412
+ this.renderSimpleAnimal(ctx, animal);
413
+ }
414
+
415
+ // 绘制调试信息(显示动物类型)
416
+ ctx.font = '12px Arial';
417
+ ctx.fillStyle = '#FF0000';
418
+ ctx.fillText(animal.type, 0, animal.size * 0.6);
419
+
420
+ // 绘制状态指示器
421
+ this.renderAnimalState(ctx, animal);
422
+
423
+ // 绘制动画效果
424
+ this.renderAnimalEffects(ctx, animal);
425
+
426
+ ctx.restore();
427
+ }
428
+
429
+ renderSimpleAnimal(ctx, animal) {
430
+ // 身体
431
+ ctx.fillStyle = animal.color;
432
+ ctx.beginPath();
433
+ ctx.ellipse(0, 0, animal.size * 0.4, animal.size * 0.3, 0, 0, Math.PI * 2);
434
+ ctx.fill();
435
+
436
+ // 头部
437
+ ctx.beginPath();
438
+ ctx.ellipse(0, -animal.size * 0.3, animal.size * 0.3, animal.size * 0.25, 0, 0, Math.PI * 2);
439
+ ctx.fill();
440
+
441
+ // 眼睛
442
+ ctx.fillStyle = '#000000';
443
+ ctx.beginPath();
444
+ ctx.arc(-animal.size * 0.1, -animal.size * 0.35, 2, 0, Math.PI * 2);
445
+ ctx.arc(animal.size * 0.1, -animal.size * 0.35, 2, 0, Math.PI * 2);
446
+ ctx.fill();
447
+ }
448
+
449
+ renderAnimalState(ctx, animal) {
450
+ // 根据状态显示不同的指示器
451
+ switch (animal.state) {
452
+ case 'eating':
453
+ // 显示爱心
454
+ ctx.fillStyle = '#FF69B4';
455
+ ctx.font = '16px Arial';
456
+ ctx.textAlign = 'center';
457
+ ctx.fillText('💖', 0, -animal.size * 0.8);
458
+ break;
459
+ case 'satisfied':
460
+ // 显示满足表情
461
+ ctx.fillStyle = '#32CD32';
462
+ ctx.font = '16px Arial';
463
+ ctx.textAlign = 'center';
464
+ ctx.fillText('😊', 0, -animal.size * 0.8);
465
+ break;
466
+ }
467
+ }
468
+
469
+ renderAnimalEffects(ctx, animal) {
470
+ // 特殊动物的额外效果
471
+ if (animal.isSpecial) {
472
+ // 小女孩的闪光效果
473
+ const sparkleCount = 3;
474
+ const time = animal.animationTime * 2;
475
+
476
+ ctx.fillStyle = '#FFD700';
477
+ for (let i = 0; i < sparkleCount; i++) {
478
+ const angle = (i / sparkleCount) * Math.PI * 2 + time;
479
+ const radius = animal.size * 0.8;
480
+ const x = Math.cos(angle) * radius;
481
+ const y = Math.sin(angle) * radius;
482
+
483
+ ctx.beginPath();
484
+ ctx.arc(x, y, 3, 0, Math.PI * 2);
485
+ ctx.fill();
486
+ }
487
+ }
488
+
489
+ // 移动时的灰尘效果
490
+ if (Math.abs(animal.vx) > 10 && animal.state !== 'eating') {
491
+ ctx.fillStyle = 'rgba(139, 69, 19, 0.3)';
492
+ for (let i = 0; i < 3; i++) {
493
+ const dustX = -animal.vx * 0.1 * (i + 1) + (Math.random() - 0.5) * 10;
494
+ const dustY = animal.size * 0.3 + Math.random() * 5;
495
+
496
+ ctx.beginPath();
497
+ ctx.arc(dustX, dustY, Math.random() * 3 + 1, 0, Math.PI * 2);
498
+ ctx.fill();
499
+ }
500
+ }
501
+ }
502
+
503
+ // 清理所有动物
504
+ clear() {
505
+ this.animals = [];
506
+ }
507
+
508
+ // 获取动物数量
509
+ getAnimalCount() {
510
+ return this.animals.length;
511
+ }
512
+
513
+ // 获取特定类型的动物数量
514
+ getAnimalCountByType(type) {
515
+ return this.animals.filter(animal => animal.type === type).length;
516
+ }
517
+
518
+ // 移除指定的动物
519
+ removeAnimal(animal) {
520
+ const index = this.animals.indexOf(animal);
521
+ if (index > -1) {
522
+ this.animals.splice(index, 1);
523
+ }
524
+ }
525
+
526
+ // 让所有动物离开
527
+ makeAllAnimalsLeave() {
528
+ this.animals.forEach(animal => {
529
+ if (animal.state !== 'leaving') {
530
+ animal.state = 'leaving';
531
+ }
532
+ });
533
+ }
534
+ }
js/audio.js ADDED
@@ -0,0 +1,474 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 音频管理系统
2
+ class AudioManager {
3
+ constructor() {
4
+ this.audioContext = null;
5
+ this.sounds = new Map();
6
+ this.isEnabled = true;
7
+ this.volume = 0.7;
8
+ this.isInitialized = false;
9
+ this.hasShownInteractionHint = false;
10
+
11
+ // 音效配置
12
+ this.soundConfig = {
13
+ potActivate: { frequency: 440, duration: 0.5, type: 'magic' },
14
+ cooking: { frequency: 220, duration: 0.3, type: 'bubble' },
15
+ foodPop: { frequency: 660, duration: 0.2, type: 'pop' },
16
+ animalAppear: { frequency: 880, duration: 0.4, type: 'chirp' },
17
+ girlAppear: { frequency: 1320, duration: 0.6, type: 'sparkle' },
18
+ celebration: { frequency: 1760, duration: 1.0, type: 'fanfare' },
19
+ stopCooking: { frequency: 330, duration: 0.4, type: 'whoosh' }
20
+ };
21
+
22
+ this.init();
23
+ }
24
+
25
+ async init() {
26
+ try {
27
+ // 创建音频上下文(暂停状态)
28
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
29
+
30
+ // 注册用户交互事件来启动音频上下文
31
+ this.setupUserInteractionHandlers();
32
+
33
+ console.log('音频系统已准备,等待用户交互启动');
34
+ } catch (error) {
35
+ console.warn('音频系统初始化失败:', error);
36
+ this.isEnabled = false;
37
+ }
38
+ }
39
+
40
+ setupUserInteractionHandlers() {
41
+ const startAudio = async () => {
42
+ if (this.audioContext && this.audioContext.state === 'suspended') {
43
+ try {
44
+ await this.audioContext.resume();
45
+
46
+ // 音频上下文启动后,预生成音效
47
+ if (!this.isInitialized) {
48
+ this.preGenerateSounds();
49
+ this.isInitialized = true;
50
+ console.log('音频系统初始化完成');
51
+ }
52
+
53
+ // 隐藏音频激活提示
54
+ this.hideAudioActivationHint();
55
+
56
+ } catch (error) {
57
+ console.warn('启动音频上下文失败:', error);
58
+ }
59
+ }
60
+ };
61
+
62
+ // 监听各种用户交互事件
63
+ const events = ['click', 'touchstart', 'keydown'];
64
+ const handler = () => {
65
+ startAudio();
66
+ // 移除事件监听器,只需要启动一次
67
+ events.forEach(event => {
68
+ document.removeEventListener(event, handler);
69
+ });
70
+ };
71
+
72
+ events.forEach(event => {
73
+ document.addEventListener(event, handler, { once: true });
74
+ });
75
+ }
76
+
77
+ showAudioActivationHint() {
78
+ const hint = document.getElementById('audioActivationHint');
79
+ if (hint) {
80
+ hint.style.display = 'block';
81
+ console.log('显示音频激活提示');
82
+ }
83
+ }
84
+
85
+ hideAudioActivationHint() {
86
+ const hint = document.getElementById('audioActivationHint');
87
+ if (hint) {
88
+ hint.style.display = 'none';
89
+ console.log('隐藏音频激活提示');
90
+ }
91
+ }
92
+
93
+ async ensureAudioContext() {
94
+ if (!this.audioContext) return false;
95
+
96
+ if (this.audioContext.state === 'suspended') {
97
+ try {
98
+ await this.audioContext.resume();
99
+
100
+ // 如果音频上下文刚刚启动,且还未初始化,则初始化音效
101
+ if (!this.isInitialized) {
102
+ this.preGenerateSounds();
103
+ this.isInitialized = true;
104
+ console.log('音频系统延迟初始化完成');
105
+ }
106
+
107
+ // 隐藏音频激活提示
108
+ this.hideAudioActivationHint();
109
+ } catch (error) {
110
+ console.warn('无法恢复音频上下文:', error);
111
+ return false;
112
+ }
113
+ }
114
+
115
+ return this.audioContext.state === 'running';
116
+ }
117
+
118
+ preGenerateSounds() {
119
+ // 预生成所有音效的音频数据
120
+ Object.keys(this.soundConfig).forEach(soundName => {
121
+ this.generateSound(soundName);
122
+ });
123
+ }
124
+
125
+ generateSound(soundName) {
126
+ const config = this.soundConfig[soundName];
127
+ if (!config) return null;
128
+
129
+ const sampleRate = this.audioContext.sampleRate;
130
+ const duration = config.duration;
131
+ const length = sampleRate * duration;
132
+ const buffer = this.audioContext.createBuffer(1, length, sampleRate);
133
+ const data = buffer.getChannelData(0);
134
+
135
+ // 根据音效类型生成不同的波形
136
+ switch (config.type) {
137
+ case 'magic':
138
+ this.generateMagicSound(data, config, sampleRate);
139
+ break;
140
+ case 'bubble':
141
+ this.generateBubbleSound(data, config, sampleRate);
142
+ break;
143
+ case 'pop':
144
+ this.generatePopSound(data, config, sampleRate);
145
+ break;
146
+ case 'chirp':
147
+ this.generateChirpSound(data, config, sampleRate);
148
+ break;
149
+ case 'sparkle':
150
+ this.generateSparkleSound(data, config, sampleRate);
151
+ break;
152
+ case 'fanfare':
153
+ this.generateFanfareSound(data, config, sampleRate);
154
+ break;
155
+ case 'whoosh':
156
+ this.generateWhooshSound(data, config, sampleRate);
157
+ break;
158
+ default:
159
+ this.generateSimpleBeep(data, config, sampleRate);
160
+ }
161
+
162
+ this.sounds.set(soundName, buffer);
163
+ return buffer;
164
+ }
165
+
166
+ generateMagicSound(data, config, sampleRate) {
167
+ // 魔法激活音效 - 上升的音调带有颤音
168
+ const baseFreq = config.frequency;
169
+ const length = data.length;
170
+
171
+ for (let i = 0; i < length; i++) {
172
+ const t = i / sampleRate;
173
+ const progress = i / length;
174
+
175
+ // 上升的频率
176
+ const freq = baseFreq * (1 + progress * 0.5);
177
+
178
+ // 颤音效果
179
+ const vibrato = 1 + 0.1 * Math.sin(2 * Math.PI * 6 * t);
180
+
181
+ // 主音调
182
+ const wave = Math.sin(2 * Math.PI * freq * vibrato * t);
183
+
184
+ // 包络(淡入淡出)
185
+ const envelope = Math.sin(Math.PI * progress) * Math.exp(-progress * 2);
186
+
187
+ data[i] = wave * envelope * 0.3;
188
+ }
189
+ }
190
+
191
+ generateBubbleSound(data, config, sampleRate) {
192
+ // 烹饪冒泡音效
193
+ const baseFreq = config.frequency;
194
+ const length = data.length;
195
+
196
+ for (let i = 0; i < length; i++) {
197
+ const t = i / sampleRate;
198
+ const progress = i / length;
199
+
200
+ // 随机频率变化模拟冒泡
201
+ const randomFactor = 1 + 0.3 * (Math.random() - 0.5);
202
+ const freq = baseFreq * randomFactor;
203
+
204
+ // 多个正弦波叠加
205
+ const wave1 = Math.sin(2 * Math.PI * freq * t);
206
+ const wave2 = Math.sin(2 * Math.PI * freq * 1.5 * t) * 0.5;
207
+ const wave3 = Math.sin(2 * Math.PI * freq * 2 * t) * 0.25;
208
+
209
+ const wave = wave1 + wave2 + wave3;
210
+
211
+ // 快速衰减
212
+ const envelope = Math.exp(-progress * 8);
213
+
214
+ data[i] = wave * envelope * 0.2;
215
+ }
216
+ }
217
+
218
+ generatePopSound(data, config, sampleRate) {
219
+ // 食物弹出音效 - 短促的爆破声
220
+ const baseFreq = config.frequency;
221
+ const length = data.length;
222
+
223
+ for (let i = 0; i < length; i++) {
224
+ const t = i / sampleRate;
225
+ const progress = i / length;
226
+
227
+ // 快速下降的频率
228
+ const freq = baseFreq * (1 - progress * 0.8);
229
+
230
+ // 噪音成分
231
+ const noise = (Math.random() - 0.5) * 0.3;
232
+
233
+ // 主音调
234
+ const wave = Math.sin(2 * Math.PI * freq * t) + noise;
235
+
236
+ // 非常快的衰减
237
+ const envelope = Math.exp(-progress * 15);
238
+
239
+ data[i] = wave * envelope * 0.4;
240
+ }
241
+ }
242
+
243
+ generateChirpSound(data, config, sampleRate) {
244
+ // 动物出现音效 - 鸟叫声
245
+ const baseFreq = config.frequency;
246
+ const length = data.length;
247
+
248
+ for (let i = 0; i < length; i++) {
249
+ const t = i / sampleRate;
250
+ const progress = i / length;
251
+
252
+ // 频率变化模拟鸟叫
253
+ const freqMod = Math.sin(2 * Math.PI * 8 * t) * 0.2;
254
+ const freq = baseFreq * (1 + freqMod);
255
+
256
+ const wave = Math.sin(2 * Math.PI * freq * t);
257
+
258
+ // 自然的包络
259
+ const envelope = Math.sin(Math.PI * progress) * Math.exp(-progress * 3);
260
+
261
+ data[i] = wave * envelope * 0.25;
262
+ }
263
+ }
264
+
265
+ generateSparkleSound(data, config, sampleRate) {
266
+ // 小女孩出现音效 - 闪亮的铃声
267
+ const baseFreq = config.frequency;
268
+ const length = data.length;
269
+
270
+ for (let i = 0; i < length; i++) {
271
+ const t = i / sampleRate;
272
+ const progress = i / length;
273
+
274
+ // 多个谐波创造铃声效果
275
+ const wave1 = Math.sin(2 * Math.PI * baseFreq * t);
276
+ const wave2 = Math.sin(2 * Math.PI * baseFreq * 2 * t) * 0.5;
277
+ const wave3 = Math.sin(2 * Math.PI * baseFreq * 3 * t) * 0.25;
278
+ const wave4 = Math.sin(2 * Math.PI * baseFreq * 4 * t) * 0.125;
279
+
280
+ const wave = wave1 + wave2 + wave3 + wave4;
281
+
282
+ // 缓慢衰减
283
+ const envelope = Math.exp(-progress * 1.5);
284
+
285
+ data[i] = wave * envelope * 0.2;
286
+ }
287
+ }
288
+
289
+ generateFanfareSound(data, config, sampleRate) {
290
+ // 庆祝音效 - 号角声
291
+ const baseFreq = config.frequency;
292
+ const length = data.length;
293
+
294
+ for (let i = 0; i < length; i++) {
295
+ const t = i / sampleRate;
296
+ const progress = i / length;
297
+
298
+ // 三和弦
299
+ const freq1 = baseFreq;
300
+ const freq2 = baseFreq * 1.25; // 大三度
301
+ const freq3 = baseFreq * 1.5; // 完全五度
302
+
303
+ const wave1 = Math.sin(2 * Math.PI * freq1 * t);
304
+ const wave2 = Math.sin(2 * Math.PI * freq2 * t) * 0.8;
305
+ const wave3 = Math.sin(2 * Math.PI * freq3 * t) * 0.6;
306
+
307
+ const wave = wave1 + wave2 + wave3;
308
+
309
+ // 持续的包络
310
+ const envelope = Math.sin(Math.PI * progress) * 0.8;
311
+
312
+ data[i] = wave * envelope * 0.15;
313
+ }
314
+ }
315
+
316
+ generateWhooshSound(data, config, sampleRate) {
317
+ // 停止音效 - 风声
318
+ const length = data.length;
319
+
320
+ for (let i = 0; i < length; i++) {
321
+ const progress = i / length;
322
+
323
+ // 白噪音
324
+ const noise = (Math.random() - 0.5) * 2;
325
+
326
+ // 低通滤波效果(简化)
327
+ const cutoff = 1000 * (1 - progress);
328
+ const filtered = noise * Math.exp(-progress * 3);
329
+
330
+ // 快速衰减
331
+ const envelope = Math.exp(-progress * 5);
332
+
333
+ data[i] = filtered * envelope * 0.3;
334
+ }
335
+ }
336
+
337
+ generateSimpleBeep(data, config, sampleRate) {
338
+ // 简单的蜂鸣声
339
+ const freq = config.frequency;
340
+ const length = data.length;
341
+
342
+ for (let i = 0; i < length; i++) {
343
+ const t = i / sampleRate;
344
+ const progress = i / length;
345
+
346
+ const wave = Math.sin(2 * Math.PI * freq * t);
347
+ const envelope = Math.exp(-progress * 3);
348
+
349
+ data[i] = wave * envelope * 0.3;
350
+ }
351
+ }
352
+
353
+ async playSound(soundName, volume = 1.0) {
354
+ if (!this.isEnabled) return;
355
+
356
+ const canPlay = await this.ensureAudioContext();
357
+ if (!canPlay) {
358
+ // 如果是第一次尝试播放,显示视觉提示
359
+ if (!this.hasShownInteractionHint) {
360
+ this.showAudioActivationHint();
361
+ this.hasShownInteractionHint = true;
362
+ }
363
+ return;
364
+ }
365
+
366
+ if (!this.isInitialized) {
367
+ console.warn('音频系统尚未初始化');
368
+ return;
369
+ }
370
+
371
+ const buffer = this.sounds.get(soundName);
372
+ if (!buffer) {
373
+ console.warn(`音效 ${soundName} 不存在`);
374
+ return;
375
+ }
376
+
377
+ try {
378
+ const source = this.audioContext.createBufferSource();
379
+ const gainNode = this.audioContext.createGain();
380
+
381
+ source.buffer = buffer;
382
+ source.connect(gainNode);
383
+ gainNode.connect(this.audioContext.destination);
384
+
385
+ // 设置音量
386
+ gainNode.gain.value = this.volume * volume;
387
+
388
+ source.start();
389
+
390
+ // 清理资源
391
+ source.onended = () => {
392
+ source.disconnect();
393
+ gainNode.disconnect();
394
+ };
395
+
396
+ } catch (error) {
397
+ console.warn(`播放音效 ${soundName} 失败:`, error);
398
+ }
399
+ }
400
+
401
+ // 播放特定音效的便捷方法
402
+ playPotActivate() {
403
+ this.playSound('potActivate');
404
+ }
405
+
406
+ playCooking() {
407
+ this.playSound('cooking', 0.5);
408
+ }
409
+
410
+ playFoodPop() {
411
+ this.playSound('foodPop', 0.8);
412
+ }
413
+
414
+ playAnimalAppear() {
415
+ this.playSound('animalAppear', 0.6);
416
+ }
417
+
418
+ playGirlAppear() {
419
+ this.playSound('girlAppear', 0.7);
420
+ }
421
+
422
+ playCelebration() {
423
+ this.playSound('celebration', 0.9);
424
+ }
425
+
426
+ playStopCooking() {
427
+ this.playSound('stopCooking', 0.6);
428
+ }
429
+
430
+ // 播放随机音调的食物弹出声
431
+ playRandomFoodPop() {
432
+ const randomPitch = 0.8 + Math.random() * 0.4; // 0.8 到 1.2
433
+ this.playSound('foodPop', randomPitch);
434
+ }
435
+
436
+ // 设置音量
437
+ setVolume(volume) {
438
+ this.volume = Math.max(0, Math.min(1, volume));
439
+ }
440
+
441
+ // 启用/禁用音效
442
+ setEnabled(enabled) {
443
+ this.isEnabled = enabled;
444
+ localStorage.setItem('magicPotAudioEnabled', enabled.toString());
445
+ }
446
+
447
+ // 获取音效状态
448
+ getStatus() {
449
+ return {
450
+ enabled: this.isEnabled,
451
+ initialized: this.isInitialized,
452
+ volume: this.volume,
453
+ soundCount: this.sounds.size,
454
+ contextState: this.audioContext ? this.audioContext.state : 'none'
455
+ };
456
+ }
457
+
458
+ // 测试所有音效
459
+ async testAllSounds() {
460
+ const soundNames = Object.keys(this.soundConfig);
461
+
462
+ for (let i = 0; i < soundNames.length; i++) {
463
+ const soundName = soundNames[i];
464
+ console.log(`测试音效: ${soundName}`);
465
+ await this.playSound(soundName);
466
+
467
+ // 等待一段时间再播放下一个
468
+ await new Promise(resolve => setTimeout(resolve, 800));
469
+ }
470
+ }
471
+ }
472
+
473
+ // 创建全局音频管理器实例
474
+ window.audioManager = new AudioManager();
js/keyboard.js ADDED
@@ -0,0 +1,684 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 键盘输入处理类
2
+ class KeyboardInputHandler {
3
+ constructor() {
4
+ this.onCommand = null; // 命令回调函数
5
+ this.commandHistory = [];
6
+ this.maxHistorySize = 20;
7
+ this.isPanelCollapsed = false;
8
+ this.isActive = false; // 面板激活状态
9
+
10
+ // 拖拽相关属性
11
+ this.isDragging = false;
12
+ this.dragOffset = { x: 0, y: 0 };
13
+ this.currentPosition = { x: null, y: null };
14
+
15
+ this.init();
16
+ }
17
+
18
+ init() {
19
+ this.setupEventListeners();
20
+ this.updatePanel();
21
+ this.setActiveState(false); // 初始为非激活状态
22
+ this.setPanelCollapsed(true); // 默认折叠面板
23
+ console.log('键盘输入处理器已初始化');
24
+ }
25
+
26
+ setupEventListeners() {
27
+ // 输入框事件
28
+ const commandInput = document.getElementById('commandInput');
29
+ const sendBtn = document.getElementById('sendCommandBtn');
30
+ const toggleBtn = document.getElementById('toggleKeyboardPanel');
31
+
32
+ if (commandInput) {
33
+ // 回车发送命令
34
+ commandInput.addEventListener('keydown', (e) => {
35
+ if (e.key === 'Enter') {
36
+ e.preventDefault();
37
+ this.sendCommand();
38
+ }
39
+ });
40
+
41
+ // 历史记录导航(上下键)
42
+ commandInput.addEventListener('keydown', (e) => {
43
+ if (e.key === 'ArrowUp') {
44
+ e.preventDefault();
45
+ this.navigateHistory(-1);
46
+ } else if (e.key === 'ArrowDown') {
47
+ e.preventDefault();
48
+ this.navigateHistory(1);
49
+ }
50
+ });
51
+ }
52
+
53
+ // 发送按钮
54
+ if (sendBtn) {
55
+ sendBtn.addEventListener('click', () => {
56
+ this.sendCommand();
57
+ });
58
+ }
59
+
60
+ // 面板折叠按钮
61
+ if (toggleBtn) {
62
+ toggleBtn.addEventListener('click', () => {
63
+ this.togglePanel();
64
+ });
65
+ }
66
+
67
+ // 快捷指令按钮
68
+ const quickCmdBtns = document.querySelectorAll('.quick-cmd');
69
+ quickCmdBtns.forEach(btn => {
70
+ btn.addEventListener('click', () => {
71
+ const command = btn.getAttribute('data-command');
72
+ if (command) {
73
+ this.executeCommand(command);
74
+ }
75
+ });
76
+ });
77
+
78
+ // 全局键盘快捷键
79
+ document.addEventListener('keydown', (e) => {
80
+ this.handleGlobalShortcuts(e);
81
+ });
82
+
83
+ // 历史记录点击事件
84
+ this.setupHistoryListener();
85
+
86
+ // 拖拽功能
87
+ this.setupDragFunctionality();
88
+ }
89
+
90
+ setupHistoryListener() {
91
+ const historyList = document.getElementById('commandHistory');
92
+ if (historyList) {
93
+ historyList.addEventListener('click', (e) => {
94
+ if (e.target.classList.contains('history-item') ||
95
+ e.target.parentElement.classList.contains('history-item')) {
96
+
97
+ const historyItem = e.target.classList.contains('history-item')
98
+ ? e.target
99
+ : e.target.parentElement;
100
+
101
+ const command = historyItem.querySelector('.history-command').textContent;
102
+ if (command) {
103
+ this.fillInputWithCommand(command);
104
+ }
105
+ }
106
+ });
107
+ }
108
+ }
109
+
110
+ handleGlobalShortcuts(e) {
111
+ // F9 由 main.js 处理,这里不再处理
112
+ // if (e.key === 'F9') {
113
+ // e.preventDefault();
114
+ // this.toggleActiveState();
115
+ // return;
116
+ // }
117
+
118
+ // 只在面板激活且没有焦点在输入框时处理全局快捷键
119
+ if (!this.isActive || document.activeElement.tagName === 'INPUT') {
120
+ return;
121
+ }
122
+
123
+ // Ctrl/Cmd + K 聚焦到输入框
124
+ if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
125
+ e.preventDefault();
126
+ this.focusInput();
127
+ }
128
+
129
+ // Ctrl/Cmd + ` 切换面板
130
+ if ((e.ctrlKey || e.metaKey) && e.key === '`') {
131
+ e.preventDefault();
132
+ this.togglePanel();
133
+ }
134
+
135
+ // ESC 清空输入框、取消激活或折叠面板
136
+ if (e.key === 'Escape') {
137
+ const input = document.getElementById('commandInput');
138
+ if (input && input.value) {
139
+ input.value = '';
140
+ input.blur();
141
+ } else if (this.isActive) {
142
+ this.setActiveState(false);
143
+ } else {
144
+ this.togglePanel();
145
+ }
146
+ }
147
+
148
+ // 数字键快速命令(1-6) - 仅在激活状态下工作
149
+ if (this.isActive && e.key >= '1' && e.key <= '6') {
150
+ const quickCommands = [
151
+ 'cook cook cook pot', // 1
152
+ 'stop stop stop pot', // 2
153
+ 'apple', // 3
154
+ 'banana', // 4
155
+ 'pizza', // 5
156
+ 'cake' // 6
157
+ ];
158
+
159
+ const commandIndex = parseInt(e.key) - 1;
160
+ if (commandIndex < quickCommands.length) {
161
+ e.preventDefault();
162
+ this.executeCommand(quickCommands[commandIndex]);
163
+ }
164
+ }
165
+ }
166
+
167
+ sendCommand() {
168
+ const input = document.getElementById('commandInput');
169
+ if (!input || !this.isActive) return;
170
+
171
+ const command = input.value.trim();
172
+ if (!command) return;
173
+
174
+ this.executeCommand(command);
175
+ input.value = '';
176
+ }
177
+
178
+ executeCommand(command) {
179
+ // 添加到历史记录
180
+ this.addToHistory(command);
181
+
182
+ // 显示调试信息
183
+ console.log(`[键盘输入] 执行命令: "${command}"`);
184
+
185
+ // 触发命令回调
186
+ if (this.onCommand) {
187
+ this.onCommand(command);
188
+ }
189
+
190
+ // 更新界面
191
+ this.updatePanel();
192
+ this.showCommandFeedback(command);
193
+ }
194
+
195
+ addToHistory(command) {
196
+ // 避免重复的连续命令
197
+ if (this.commandHistory.length > 0 &&
198
+ this.commandHistory[this.commandHistory.length - 1].command === command) {
199
+ return;
200
+ }
201
+
202
+ const historyItem = {
203
+ command: command,
204
+ timestamp: new Date().toLocaleTimeString(),
205
+ fullTimestamp: Date.now()
206
+ };
207
+
208
+ this.commandHistory.push(historyItem);
209
+
210
+ // 限制历史记录大小
211
+ if (this.commandHistory.length > this.maxHistorySize) {
212
+ this.commandHistory.shift();
213
+ }
214
+
215
+ this.updateHistoryDisplay();
216
+ }
217
+
218
+ navigateHistory(direction) {
219
+ // 实现历史记录导航功能
220
+ const input = document.getElementById('commandInput');
221
+ if (!input || this.commandHistory.length === 0) return;
222
+
223
+ if (!this.historyIndex) {
224
+ this.historyIndex = this.commandHistory.length;
225
+ }
226
+
227
+ this.historyIndex += direction;
228
+
229
+ if (this.historyIndex < 0) {
230
+ this.historyIndex = 0;
231
+ } else if (this.historyIndex >= this.commandHistory.length) {
232
+ this.historyIndex = this.commandHistory.length;
233
+ input.value = '';
234
+ return;
235
+ }
236
+
237
+ input.value = this.commandHistory[this.historyIndex].command;
238
+ }
239
+
240
+ fillInputWithCommand(command) {
241
+ const input = document.getElementById('commandInput');
242
+ if (input) {
243
+ input.value = command;
244
+ input.focus();
245
+ }
246
+ }
247
+
248
+ updateHistoryDisplay() {
249
+ const historyList = document.getElementById('commandHistory');
250
+ if (!historyList) return;
251
+
252
+ if (this.commandHistory.length === 0) {
253
+ historyList.innerHTML = '<div style="color: rgba(255,255,255,0.5); font-style: italic; text-align: center; padding: 10px;">暂无命令历史</div>';
254
+ return;
255
+ }
256
+
257
+ const historyHTML = this.commandHistory
258
+ .slice(-10) // 只显示最近10条
259
+ .reverse()
260
+ .map(item => `
261
+ <div class="history-item" title="点击重新使用此命令">
262
+ <span class="history-command">${this.escapeHtml(item.command)}</span>
263
+ <span class="history-time">${item.timestamp}</span>
264
+ </div>
265
+ `).join('');
266
+
267
+ historyList.innerHTML = historyHTML;
268
+ }
269
+
270
+ showCommandFeedback(command) {
271
+ // 创建命令反馈动画
272
+ const panel = document.getElementById('keyboardInputPanel');
273
+ if (!panel) return;
274
+
275
+ // 添加闪烁效果
276
+ panel.style.borderColor = '#ff6b6b';
277
+ setTimeout(() => {
278
+ panel.style.borderColor = '#4ecdc4';
279
+ }, 200);
280
+
281
+ // 在面板标题旁显示最后执行的命令
282
+ const header = panel.querySelector('.input-header h4');
283
+ if (header) {
284
+ const originalText = header.textContent;
285
+ header.textContent = `🎮 执行: ${command}`;
286
+ header.style.color = '#ff6b6b';
287
+
288
+ setTimeout(() => {
289
+ header.textContent = originalText;
290
+ header.style.color = 'white';
291
+ }, 1500);
292
+ }
293
+ }
294
+
295
+ togglePanel() {
296
+ this.setPanelCollapsed(!this.isPanelCollapsed);
297
+ }
298
+
299
+ setPanelCollapsed(collapsed) {
300
+ const panel = document.getElementById('keyboardInputPanel');
301
+ const content = document.getElementById('keyboardInputContent');
302
+ const toggleBtn = document.getElementById('toggleKeyboardPanel');
303
+
304
+ if (!panel || !content || !toggleBtn) return;
305
+
306
+ this.isPanelCollapsed = collapsed;
307
+
308
+ if (this.isPanelCollapsed) {
309
+ // 完全隐藏面板
310
+ panel.style.display = 'none';
311
+ panel.classList.add('collapsed');
312
+ } else {
313
+ // 显示面板
314
+ panel.style.display = 'block';
315
+ panel.classList.remove('collapsed');
316
+ content.style.display = 'block';
317
+ toggleBtn.textContent = '⌨️';
318
+ toggleBtn.title = '折叠键盘输入面板';
319
+ }
320
+
321
+ console.log(`键盘输入面板${this.isPanelCollapsed ? '已隐藏' : '已显示'}`);
322
+ }
323
+
324
+ focusInput() {
325
+ const input = document.getElementById('commandInput');
326
+ if (input) {
327
+ // 如果面板未激活,先激活
328
+ if (!this.isActive) {
329
+ this.setActiveState(true);
330
+ }
331
+
332
+ // 如果面板是折叠的,先展开
333
+ if (this.isPanelCollapsed) {
334
+ this.setPanelCollapsed(false);
335
+ }
336
+
337
+ setTimeout(() => {
338
+ input.focus();
339
+ }, 100);
340
+ }
341
+ }
342
+
343
+ updatePanel() {
344
+ this.updateHistoryDisplay();
345
+
346
+ // 更新快捷指令状态
347
+ const quickCmdBtns = document.querySelectorAll('.quick-cmd');
348
+ quickCmdBtns.forEach(btn => {
349
+ btn.addEventListener('mouseenter', () => {
350
+ const command = btn.getAttribute('data-command');
351
+ btn.title = `点击执行: ${command}`;
352
+ });
353
+ });
354
+ }
355
+
356
+ // 工具方法
357
+ escapeHtml(text) {
358
+ const div = document.createElement('div');
359
+ div.textContent = text;
360
+ return div.innerHTML;
361
+ }
362
+
363
+ // 设置面板激活状态
364
+ setActiveState(active) {
365
+ console.log(`setActiveState called with: ${active}`);
366
+ this.isActive = active;
367
+ const panel = document.getElementById('keyboardInputPanel');
368
+ if (!panel) {
369
+ console.log('keyboardInputPanel not found!');
370
+ return;
371
+ }
372
+
373
+ panel.classList.remove('active', 'inactive');
374
+
375
+ if (active) {
376
+ panel.classList.add('active');
377
+ console.log('Panel activated - adding active class');
378
+ this.showActivationIndicator('面板已激活 - 可以输入命令');
379
+ } else {
380
+ panel.classList.add('inactive');
381
+ console.log('Panel deactivated - adding inactive class');
382
+ this.showActivationIndicator('面板未激活 - 按F9激活');
383
+ }
384
+
385
+ console.log(`键盘输入面板${active ? '已激活' : '已取消激活'}`);
386
+ console.log('Panel classes:', panel.className);
387
+ }
388
+
389
+ // 切换面板激活状态
390
+ toggleActiveState() {
391
+ console.log('toggleActiveState called, current state:', this.isActive);
392
+ this.setActiveState(!this.isActive);
393
+ console.log('toggleActiveState completed, new state:', this.isActive);
394
+ }
395
+
396
+ // 显示激活状态指示器
397
+ showActivationIndicator(message) {
398
+ // 移除之前的指示器
399
+ const existingIndicator = document.querySelector('.activation-indicator');
400
+ if (existingIndicator) {
401
+ existingIndicator.remove();
402
+ }
403
+
404
+ // 创建新的指示器
405
+ const indicator = document.createElement('div');
406
+ indicator.className = 'activation-indicator';
407
+ indicator.textContent = message;
408
+ indicator.style.cssText = `
409
+ position: fixed;
410
+ top: 50%;
411
+ left: 50%;
412
+ transform: translate(-50%, -50%);
413
+ background: ${this.isActive ? '#4ecdc4' : '#6c757d'};
414
+ color: white;
415
+ padding: 15px 25px;
416
+ border-radius: 25px;
417
+ font-family: 'Comic Neue', cursive;
418
+ font-weight: bold;
419
+ font-size: 16px;
420
+ z-index: 15000;
421
+ box-shadow: 0 8px 32px rgba(0,0,0,0.3);
422
+ animation: activationPulse 0.5s ease-out;
423
+ `;
424
+
425
+ document.body.appendChild(indicator);
426
+
427
+ // 自动移除
428
+ setTimeout(() => {
429
+ if (indicator.parentNode) {
430
+ indicator.style.animation = 'activationFadeOut 0.3s ease-out forwards';
431
+ setTimeout(() => {
432
+ if (indicator.parentNode) {
433
+ indicator.parentNode.removeChild(indicator);
434
+ }
435
+ }, 300);
436
+ }
437
+ }, 2000);
438
+ }
439
+
440
+ // 获取统计信息
441
+ getStats() {
442
+ return {
443
+ totalCommands: this.commandHistory.length,
444
+ isCollapsed: this.isPanelCollapsed,
445
+ isActive: this.isActive,
446
+ lastCommand: this.commandHistory.length > 0
447
+ ? this.commandHistory[this.commandHistory.length - 1].command
448
+ : null
449
+ };
450
+ }
451
+
452
+ // 清空历史记录
453
+ clearHistory() {
454
+ this.commandHistory = [];
455
+ this.updateHistoryDisplay();
456
+ console.log('命令历史已清空');
457
+ }
458
+
459
+ // 导出历史记录
460
+ exportHistory() {
461
+ const data = {
462
+ exported: new Date().toISOString(),
463
+ commands: this.commandHistory
464
+ };
465
+
466
+ const blob = new Blob([JSON.stringify(data, null, 2)],
467
+ { type: 'application/json' });
468
+ const url = URL.createObjectURL(blob);
469
+
470
+ const a = document.createElement('a');
471
+ a.href = url;
472
+ a.download = `magicpot-commands-${Date.now()}.json`;
473
+ document.body.appendChild(a);
474
+ a.click();
475
+ document.body.removeChild(a);
476
+ URL.revokeObjectURL(url);
477
+
478
+ console.log('命令历史已导出');
479
+ }
480
+
481
+ // 获取命令提示
482
+ getCommandSuggestions(input) {
483
+ const suggestions = [
484
+ 'cook cook cook pot',
485
+ 'stop stop stop pot',
486
+ 'apple', 'banana', 'orange', 'strawberry', 'watermelon', 'grape',
487
+ 'pizza', 'burger', 'bread', 'rice', 'noodles', 'cake', 'cookie',
488
+ 'cheese', 'fish', 'chicken', 'carrot', 'tomato', 'corn', 'broccoli'
489
+ ];
490
+
491
+ if (!input) return suggestions.slice(0, 5);
492
+
493
+ const filtered = suggestions.filter(cmd =>
494
+ cmd.toLowerCase().includes(input.toLowerCase())
495
+ );
496
+
497
+ return filtered.slice(0, 8);
498
+ }
499
+
500
+ // 设置拖拽功能
501
+ setupDragFunctionality() {
502
+ const panel = document.getElementById('keyboardInputPanel');
503
+ const header = panel?.querySelector('.input-header');
504
+
505
+ if (!panel || !header) return;
506
+
507
+ // 鼠标按下开始拖拽
508
+ header.addEventListener('mousedown', (e) => {
509
+ if (!this.isActive) return; // 只有激活状态下才能拖拽
510
+
511
+ this.isDragging = true;
512
+ panel.classList.add('dragging');
513
+
514
+ const rect = panel.getBoundingClientRect();
515
+ this.dragOffset.x = e.clientX - rect.left;
516
+ this.dragOffset.y = e.clientY - rect.top;
517
+
518
+ // 记录当前位置
519
+ this.currentPosition.x = rect.left;
520
+ this.currentPosition.y = rect.top;
521
+
522
+ e.preventDefault();
523
+ });
524
+
525
+ // 全局鼠标移动事件
526
+ document.addEventListener('mousemove', (e) => {
527
+ if (!this.isDragging) return;
528
+
529
+ e.preventDefault();
530
+
531
+ // 计算新位置
532
+ let newX = e.clientX - this.dragOffset.x;
533
+ let newY = e.clientY - this.dragOffset.y;
534
+
535
+ // 边界检查
536
+ const rect = panel.getBoundingClientRect();
537
+ const maxX = window.innerWidth - rect.width;
538
+ const maxY = window.innerHeight - rect.height;
539
+
540
+ newX = Math.max(0, Math.min(newX, maxX));
541
+ newY = Math.max(0, Math.min(newY, maxY));
542
+
543
+ // 应用新位置
544
+ panel.style.left = newX + 'px';
545
+ panel.style.top = newY + 'px';
546
+ panel.style.right = 'auto';
547
+ panel.style.bottom = 'auto';
548
+
549
+ this.currentPosition.x = newX;
550
+ this.currentPosition.y = newY;
551
+ });
552
+
553
+ // 全局鼠标松开事件
554
+ document.addEventListener('mouseup', (e) => {
555
+ if (!this.isDragging) return;
556
+
557
+ this.isDragging = false;
558
+ panel.classList.remove('dragging');
559
+
560
+ // 保存位置到localStorage
561
+ this.savePosition();
562
+ });
563
+
564
+ // 加载保存的位置
565
+ this.loadPosition();
566
+ }
567
+
568
+ // 保存面板位置
569
+ savePosition() {
570
+ if (this.currentPosition.x !== null && this.currentPosition.y !== null) {
571
+ const position = {
572
+ x: this.currentPosition.x,
573
+ y: this.currentPosition.y
574
+ };
575
+ localStorage.setItem('keyboardPanelPosition', JSON.stringify(position));
576
+ }
577
+ }
578
+
579
+ // 加载保存的位置
580
+ loadPosition() {
581
+ const panel = document.getElementById('keyboardInputPanel');
582
+ if (!panel) return;
583
+
584
+ try {
585
+ const savedPosition = localStorage.getItem('keyboardPanelPosition');
586
+ if (savedPosition) {
587
+ const position = JSON.parse(savedPosition);
588
+
589
+ // 检查位置是否在屏幕范围内
590
+ const rect = panel.getBoundingClientRect();
591
+ const maxX = window.innerWidth - rect.width;
592
+ const maxY = window.innerHeight - rect.height;
593
+
594
+ if (position.x >= 0 && position.x <= maxX &&
595
+ position.y >= 0 && position.y <= maxY) {
596
+
597
+ panel.style.left = position.x + 'px';
598
+ panel.style.top = position.y + 'px';
599
+ panel.style.right = 'auto';
600
+ panel.style.bottom = 'auto';
601
+
602
+ this.currentPosition.x = position.x;
603
+ this.currentPosition.y = position.y;
604
+ }
605
+ }
606
+ } catch (error) {
607
+ console.warn('加载面板位置失败:', error);
608
+ }
609
+ }
610
+
611
+ // 重置面板位置到右下角
612
+ resetPosition() {
613
+ const panel = document.getElementById('keyboardInputPanel');
614
+ if (!panel) return;
615
+
616
+ panel.style.left = 'auto';
617
+ panel.style.top = 'auto';
618
+ panel.style.right = '20px';
619
+ panel.style.bottom = '20px';
620
+
621
+ this.currentPosition.x = null;
622
+ this.currentPosition.y = null;
623
+
624
+ // 清除保存的位置
625
+ localStorage.removeItem('keyboardPanelPosition');
626
+
627
+ console.log('面板位置已重置到右下角');
628
+ }
629
+ }
630
+
631
+ // 添加快捷键提示样式
632
+ const keyboardStyle = document.createElement('style');
633
+ keyboardStyle.textContent = `
634
+ .keyboard-shortcuts-hint {
635
+ position: absolute;
636
+ bottom: 5px;
637
+ left: 50%;
638
+ transform: translateX(-50%);
639
+ background: rgba(0, 0, 0, 0.8);
640
+ color: white;
641
+ padding: 5px 10px;
642
+ border-radius: 10px;
643
+ font-size: 0.7em;
644
+ white-space: nowrap;
645
+ opacity: 0;
646
+ transition: opacity 0.3s ease;
647
+ pointer-events: none;
648
+ font-family: 'Comic Neue', cursive;
649
+ }
650
+
651
+ .keyboard-input-panel:hover .keyboard-shortcuts-hint {
652
+ opacity: 1;
653
+ }
654
+
655
+ .command-input:focus + .keyboard-shortcuts-hint {
656
+ opacity: 1;
657
+ }
658
+
659
+ @keyframes activationPulse {
660
+ 0% {
661
+ opacity: 0;
662
+ transform: translate(-50%, -50%) scale(0.8);
663
+ }
664
+ 50% {
665
+ transform: translate(-50%, -50%) scale(1.1);
666
+ }
667
+ 100% {
668
+ opacity: 1;
669
+ transform: translate(-50%, -50%) scale(1);
670
+ }
671
+ }
672
+
673
+ @keyframes activationFadeOut {
674
+ from {
675
+ opacity: 1;
676
+ transform: translate(-50%, -50%) scale(1);
677
+ }
678
+ to {
679
+ opacity: 0;
680
+ transform: translate(-50%, -50%) scale(0.9);
681
+ }
682
+ }
683
+ `;
684
+ document.head.appendChild(keyboardStyle);
js/main.js ADDED
@@ -0,0 +1,1365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 主游戏类
2
+ class MagicPotGame {
3
+ constructor() {
4
+ this.canvas = document.getElementById('gameCanvas');
5
+ this.ctx = this.canvas.getContext('2d');
6
+ this.gameState = 'idle'; // idle, activated, cooking, overflowing, stopped
7
+ this.currentFood = null;
8
+ this.foodCount = 0;
9
+ // this.particles = []; // 不再需要单独的粒子数组,使用particleSystem管理
10
+ this.lastTime = 0;
11
+
12
+ // 游戏配置
13
+ this.config = {
14
+ maxFoodCount: 100, // 最大食物计数
15
+ maxParticles: 150, // 最大粒子数量
16
+ maxAnimals: 8, // 最大动物数量
17
+ animalSpawnThreshold: 5, // 动物出现阈值(降低到5以便更快测试)
18
+ easterEggThreshold: 30, // 彩蛋触发阈值
19
+ particleSpawnRate: 1, // 粒子生成速率(调整为1,降低食物生成速度)
20
+ cookingDuration: 3000, // 3秒烹饪时间
21
+ cleanupInterval: 5000, // 清理间隔(毫秒)
22
+ maxGroundedParticles: 80 // 地面最大粒子数
23
+ };
24
+
25
+ // 初始化组件
26
+ this.voice = new VoiceRecognition();
27
+ this.keyboard = new KeyboardInputHandler();
28
+ this.pot = new MagicPot(this.canvas.width / 2, this.canvas.height / 2 + 50);
29
+ this.particleSystem = new ParticleSystem();
30
+ this.animalSystem = new AnimalSystem(this.canvas); // 传入Canvas引用
31
+ this.textToImage = new TextToImageAPI();
32
+ this.audio = window.audioManager;
33
+
34
+ // 食物生成控制
35
+ this.lastFoodSpawnTime = 0;
36
+ this.foodSpawnInterval = 500; // 每500毫秒生成一次食物(显著降低生成速度)
37
+
38
+ // 性能监控
39
+ this.lastCleanupTime = 0;
40
+ this.performanceStats = {
41
+ particles: 0,
42
+ animals: 0,
43
+ fps: 0,
44
+ lastFpsTime: 0,
45
+ frameCount: 0
46
+ };
47
+
48
+ this.init();
49
+ }
50
+
51
+ init() {
52
+ this.setupEventListeners();
53
+ this.setupCanvas();
54
+ this.gameLoop();
55
+ this.updateUI();
56
+
57
+ // 显示初始提示
58
+ this.showDebug('游戏初始化完成,等待语音命令...');
59
+
60
+ // 启动定期清理
61
+ this.startPerformanceMonitoring();
62
+ }
63
+
64
+ setupEventListeners() {
65
+ // 语音识别事件
66
+ this.voice.onResult = (transcript) => {
67
+ this.handleVoiceCommand(transcript);
68
+ };
69
+
70
+ this.voice.onError = (error) => {
71
+ this.showDebug(`语音识别错误: ${error}`);
72
+ };
73
+
74
+ this.voice.onStatusChange = (status) => {
75
+ this.updateVoiceStatus(status);
76
+ };
77
+
78
+ // 键盘输入事件
79
+ this.keyboard.onCommand = (command) => {
80
+ this.handleKeyboardCommand(command);
81
+ };
82
+
83
+ // 键盘调试快捷键
84
+ document.addEventListener('keydown', (e) => {
85
+ if (e.key === 'F12') {
86
+ this.toggleDebug();
87
+ } else if (e.key === 'F11') {
88
+ this.testAudio();
89
+ } else if (e.key === 'F10') {
90
+ this.toggleAudio();
91
+ } else if (e.key === 'F9') {
92
+ e.preventDefault(); // 确保阻止浏览器默认行为
93
+ this.toggleKeyboardActivation();
94
+ } else if (e.key === 'F8') {
95
+ this.toggleKeyboardPanel();
96
+ } else if (e.key === 'F7') {
97
+ this.clearCommandHistory();
98
+ } else if (e.key === 'F6') {
99
+ this.restartVoice();
100
+ } else if (e.key === 'F5') {
101
+ this.resetKeyboardPanelPosition();
102
+ } else if (e.key === 'F4') {
103
+ this.adjustFoodSpawnSpeed();
104
+ } else if (e.key === 'a' && e.altKey) {
105
+ e.preventDefault();
106
+ this.forceSpawnAnimal();
107
+ }
108
+ });
109
+
110
+ // 音效按钮
111
+ const audioButton = document.getElementById('audioButton');
112
+ if (audioButton) {
113
+ audioButton.addEventListener('click', () => {
114
+ this.toggleAudio();
115
+ });
116
+ this.updateAudioButton();
117
+ }
118
+
119
+ // 帮助按钮
120
+ const helpButton = document.getElementById('helpButton');
121
+ if (helpButton) {
122
+ helpButton.addEventListener('click', () => {
123
+ this.showVoiceHelp();
124
+ });
125
+ }
126
+
127
+ // 窗口大小调整
128
+ window.addEventListener('resize', () => {
129
+ this.resizeCanvas();
130
+ });
131
+ }
132
+
133
+ setupCanvas() {
134
+ // 设置高DPI显示
135
+ const dpr = window.devicePixelRatio || 1;
136
+ const rect = this.canvas.getBoundingClientRect();
137
+
138
+ this.canvas.width = rect.width * dpr;
139
+ this.canvas.height = rect.height * dpr;
140
+ this.ctx.scale(dpr, dpr);
141
+
142
+ this.canvas.style.width = rect.width + 'px';
143
+ this.canvas.style.height = rect.height + 'px';
144
+ }
145
+
146
+ handleVoiceCommand(transcript) {
147
+ const command = transcript.toLowerCase().trim();
148
+ this.showDebug(`语音命令: "${command}"`);
149
+ this.processCommand(command, '语音');
150
+ }
151
+
152
+ handleKeyboardCommand(command) {
153
+ const normalizedCommand = command.toLowerCase().trim();
154
+ this.showDebug(`键盘命令: "${command}"`);
155
+ this.processCommand(normalizedCommand, '键盘');
156
+ }
157
+
158
+ processCommand(command, source) {
159
+ // 统一的命令处理逻辑
160
+
161
+ // 激活魔法锅 - 更灵活的匹配
162
+ if (this.matchActivationCommand(command)) {
163
+ this.activatePot();
164
+ this.showDebug(`通过${source}激活魔法锅`);
165
+ return;
166
+ }
167
+
168
+ // 停止烹饪 - 更灵活的匹配
169
+ if (this.matchStopCommand(command)) {
170
+ this.stopCooking();
171
+ this.showDebug(`通过${source}停止烹饪`);
172
+ return;
173
+ }
174
+
175
+ // 如果锅已激活,处理食物命令
176
+ if (this.gameState === 'activated') {
177
+ const foodName = this.extractFoodName(command);
178
+ if (foodName) {
179
+ this.startCooking(foodName);
180
+ this.showDebug(`通过${source}开始烹饪: ${foodName}`);
181
+ } else {
182
+ this.showDebug(`未识别的食物: "${command}" (来源: ${source})`);
183
+ }
184
+ } else {
185
+ // 如果锅未激活,提示用户先激活
186
+ if (this.isValidFoodCommand(command)) {
187
+ this.showDebug(`请先激活魔法锅再烹饪食物 (来源: ${source})`);
188
+ this.showSpecialMessage('请先说 "cook cook cook pot" 激活魔法锅');
189
+ }
190
+ }
191
+ }
192
+
193
+ isValidFoodCommand(command) {
194
+ // 检查是否是有效的食物命令
195
+ const foods = [
196
+ 'apple', 'banana', 'orange', 'strawberry', 'watermelon', 'grape',
197
+ 'pizza', 'burger', 'bread', 'rice', 'noodles', 'cake', 'cookie',
198
+ 'cheese', 'fish', 'chicken', 'carrot', 'tomato', 'corn', 'broccoli',
199
+ 'potato', 'onion'
200
+ ];
201
+
202
+ return foods.some(food => command.includes(food));
203
+ }
204
+
205
+ matchActivationCommand(command) {
206
+ // 多种激活命令的匹配模式
207
+ const patterns = [
208
+ /cook.*cook.*cook.*pot/,
209
+ /cook.*pot.*cook.*cook/,
210
+ /pot.*cook.*cook.*cook/,
211
+ /cook\s+cook\s+cook\s+pot/,
212
+ /cook.*three.*times.*pot/,
213
+ /magic.*pot.*cook/
214
+ ];
215
+
216
+ return patterns.some(pattern => pattern.test(command));
217
+ }
218
+
219
+ matchStopCommand(command) {
220
+ // 多种停止命令的匹配模式
221
+ const patterns = [
222
+ /stop.*stop.*stop.*pot/,
223
+ /stop.*pot.*stop.*stop/,
224
+ /pot.*stop.*stop.*stop/,
225
+ /stop\s+stop\s+stop\s+pot/,
226
+ /stop.*three.*times.*pot/,
227
+ /magic.*pot.*stop/
228
+ ];
229
+
230
+ return patterns.some(pattern => pattern.test(command));
231
+ }
232
+
233
+ extractFoodName(command) {
234
+ // 支持的食物列表
235
+ const foods = [
236
+ 'apple', 'banana', 'orange', 'strawberry', 'watermelon', 'grape',
237
+ 'pizza', 'burger', 'bread', 'rice', 'noodles', 'cake', 'cookie',
238
+ 'cheese', 'fish', 'chicken', 'carrot', 'tomato', 'corn', 'broccoli',
239
+ 'potato', 'onion'
240
+ ];
241
+
242
+ // 在命令中查找食物名称
243
+ for (const food of foods) {
244
+ if (command.includes(food)) {
245
+ return food;
246
+ }
247
+ }
248
+
249
+ // 如果没有找到确切匹配,尝试模糊匹配
250
+ for (const food of foods) {
251
+ if (this.fuzzyMatch(command, food)) {
252
+ return food;
253
+ }
254
+ }
255
+
256
+ return null;
257
+ }
258
+
259
+ fuzzyMatch(text, target) {
260
+ // 简单的模糊匹配算法
261
+ const threshold = 0.7;
262
+ const similarity = this.calculateSimilarity(text, target);
263
+ return similarity >= threshold;
264
+ }
265
+
266
+ calculateSimilarity(str1, str2) {
267
+ // 计算两个字符串的相似度
268
+ const longer = str1.length > str2.length ? str1 : str2;
269
+ const shorter = str1.length > str2.length ? str2 : str1;
270
+
271
+ if (longer.length === 0) return 1.0;
272
+
273
+ const distance = this.levenshteinDistance(longer, shorter);
274
+ return (longer.length - distance) / longer.length;
275
+ }
276
+
277
+ levenshteinDistance(str1, str2) {
278
+ // 计算编辑距离
279
+ const matrix = [];
280
+
281
+ for (let i = 0; i <= str2.length; i++) {
282
+ matrix[i] = [i];
283
+ }
284
+
285
+ for (let j = 0; j <= str1.length; j++) {
286
+ matrix[0][j] = j;
287
+ }
288
+
289
+ for (let i = 1; i <= str2.length; i++) {
290
+ for (let j = 1; j <= str1.length; j++) {
291
+ if (str2.charAt(i - 1) === str1.charAt(j - 1)) {
292
+ matrix[i][j] = matrix[i - 1][j - 1];
293
+ } else {
294
+ matrix[i][j] = Math.min(
295
+ matrix[i - 1][j - 1] + 1,
296
+ matrix[i][j - 1] + 1,
297
+ matrix[i - 1][j] + 1
298
+ );
299
+ }
300
+ }
301
+ }
302
+
303
+ return matrix[str2.length][str1.length];
304
+ }
305
+
306
+ activatePot() {
307
+ if (this.gameState === 'idle') {
308
+ this.gameState = 'activated';
309
+ this.pot.activate();
310
+ this.updateUI();
311
+ this.showDebug('魔法锅已激活!');
312
+ this.showSpecialMessage('🪄 魔法锅已激活!\n现在说出食物名称开始烹饪');
313
+
314
+ // 播放激活音效
315
+ if (this.audio) {
316
+ this.audio.playPotActivate();
317
+ }
318
+ }
319
+ }
320
+
321
+ async startCooking(foodName) {
322
+ if (this.gameState !== 'activated') return;
323
+
324
+ this.gameState = 'cooking';
325
+ this.currentFood = foodName;
326
+ this.pot.startCooking(foodName);
327
+ this.updateUI();
328
+
329
+ this.showDebug(`开始烹饪: ${foodName}`);
330
+
331
+ // 播放烹饪音效
332
+ if (this.audio) {
333
+ this.audio.playCooking();
334
+ // 烹饪过程中定期播放冒泡声
335
+ this.cookingSoundInterval = setInterval(() => {
336
+ if (this.gameState === 'cooking') {
337
+ this.audio.playCooking();
338
+ }
339
+ }, 1000);
340
+ }
341
+
342
+ try {
343
+ // 生成食物和动物素材
344
+ await this.generateAssets(foodName);
345
+
346
+ // 烹饪完成,开始溢出
347
+ setTimeout(() => {
348
+ this.startOverflowing();
349
+ }, this.config.cookingDuration);
350
+
351
+ } catch (error) {
352
+ this.showDebug(`生成素材失败: ${error.message}`);
353
+ // 即使生成失败也继续游戏
354
+ setTimeout(() => {
355
+ this.startOverflowing();
356
+ }, this.config.cookingDuration);
357
+ }
358
+ }
359
+
360
+ async generateAssets(foodName) {
361
+ // 显示生成进度
362
+ this.updateCookingProgress(0);
363
+
364
+ // 生成食物图片
365
+ this.updateCookingProgress(30);
366
+ const foodImage = await this.textToImage.generateFood(foodName);
367
+
368
+ // 生成动物图片
369
+ this.updateCookingProgress(60);
370
+ const animalImage = await this.textToImage.generateAnimal(foodName);
371
+
372
+ // 保存生成的素材
373
+ this.pot.setFoodAssets(foodImage, animalImage);
374
+ this.updateCookingProgress(100);
375
+
376
+ this.showDebug(`素材生成完成: ${foodName}`);
377
+ }
378
+
379
+ startOverflowing() {
380
+ this.gameState = 'overflowing';
381
+ this.pot.startOverflowing();
382
+ this.foodCount = 0; // 重置计数器开始计数
383
+ this.updateUI();
384
+ this.showDebug('食物开始涌出!');
385
+
386
+ console.log(`开始溢出,食物计数重置为: ${this.foodCount}`);
387
+
388
+ // 停止烹饪音效
389
+ if (this.cookingSoundInterval) {
390
+ clearInterval(this.cookingSoundInterval);
391
+ this.cookingSoundInterval = null;
392
+ }
393
+ }
394
+
395
+ stopCooking() {
396
+ if (this.gameState === 'overflowing') {
397
+ this.gameState = 'stopped';
398
+ this.pot.stopOverflowing();
399
+ this.updateUI();
400
+ this.showDebug('停止烹饪');
401
+
402
+ // 播放停止音效
403
+ if (this.audio) {
404
+ this.audio.playStopCooking();
405
+ }
406
+
407
+ // 停止烹饪音效
408
+ if (this.cookingSoundInterval) {
409
+ clearInterval(this.cookingSoundInterval);
410
+ this.cookingSoundInterval = null;
411
+ }
412
+
413
+ // 显示统计信息
414
+ this.showGameStats();
415
+
416
+ // 5秒后重置游戏状态
417
+ setTimeout(() => {
418
+ this.resetGame();
419
+ }, 5000);
420
+ }
421
+ }
422
+
423
+ resetGame() {
424
+ this.gameState = 'idle';
425
+ this.currentFood = null;
426
+ this.foodCount = 0;
427
+ this.pot.reset();
428
+ this.particleSystem.clear();
429
+ this.animalSystem.clear();
430
+ this.updateUI();
431
+ this.showDebug('游戏已重置,可以重新开始');
432
+ }
433
+
434
+ showGameStats() {
435
+ const stats = {
436
+ food: this.currentFood,
437
+ count: this.foodCount,
438
+ animals: this.animalSystem.getAnimalCount(),
439
+ particles: this.particleSystem.getParticleCount()
440
+ };
441
+
442
+ const message = `本轮统计:\n食物:${stats.food}\n数量:${stats.count}\n动物:${stats.animals}只`;
443
+ this.showSpecialMessage(message);
444
+ this.showDebug(`游戏统计: ${JSON.stringify(stats)}`);
445
+ }
446
+
447
+ gameLoop(currentTime = 0) {
448
+ const deltaTime = currentTime - this.lastTime;
449
+ this.lastTime = currentTime;
450
+
451
+ this.update(deltaTime);
452
+ this.render();
453
+
454
+ // 性能监控和清理
455
+ this.updatePerformanceStats(currentTime);
456
+ this.performCleanupIfNeeded(currentTime);
457
+
458
+ requestAnimationFrame((time) => this.gameLoop(time));
459
+ }
460
+
461
+ update(deltaTime) {
462
+ // 更新魔法锅
463
+ this.pot.update(deltaTime);
464
+
465
+ // 更新粒子系统
466
+ this.particleSystem.update(deltaTime);
467
+
468
+ // 更新动物系统
469
+ this.animalSystem.update(deltaTime);
470
+
471
+ // 如果正在溢出,生成食物粒子(控制生成频率)
472
+ if (this.gameState === 'overflowing') {
473
+ const currentTime = Date.now();
474
+ if (currentTime - this.lastFoodSpawnTime >= this.foodSpawnInterval) {
475
+ this.spawnFoodParticles();
476
+ this.lastFoodSpawnTime = currentTime;
477
+
478
+ // 在生成食物粒子后立即检查动物生成
479
+ this.checkAnimalSpawn();
480
+ this.checkEasterEggs();
481
+ }
482
+ }
483
+ }
484
+
485
+ spawnFoodParticles() {
486
+ // 检查是否达到最大粒子数量
487
+ if (this.particleSystem.getParticleCount() >= this.config.maxParticles) {
488
+ this.showDebug('已达到最大粒子数量,停止生成新粒子');
489
+ return;
490
+ }
491
+
492
+ // 检查是否达到最大食物计数
493
+ if (this.foodCount >= this.config.maxFoodCount) {
494
+ this.showDebug('已达到最大食物数量,自动停止烹饪');
495
+ this.stopCooking();
496
+ return;
497
+ }
498
+
499
+ // 根据配置生成食物粒子
500
+ const spawnCount = Math.min(
501
+ this.config.particleSpawnRate,
502
+ this.config.maxParticles - this.particleSystem.getParticleCount(),
503
+ this.config.maxFoodCount - this.foodCount
504
+ );
505
+
506
+ for (let i = 0; i < spawnCount; i++) {
507
+ const particle = this.particleSystem.createFoodParticle(
508
+ this.pot.x,
509
+ this.pot.y - 50,
510
+ this.currentFood
511
+ );
512
+ // 注意:粒子已经在 createFoodParticle 中添加到 particleSystem.particles 了
513
+ // 这里不需要再添加到 this.particles
514
+ this.foodCount++;
515
+
516
+ console.log(`食物粒子已创建: ${this.currentFood}, 当前计数: ${this.foodCount}`);
517
+
518
+ // 随机播放食物弹出音效
519
+ if (this.audio && Math.random() < 0.3) {
520
+ this.audio.playRandomFoodPop();
521
+ }
522
+ }
523
+
524
+ // 更新UI显示
525
+ this.updateUI();
526
+ }
527
+
528
+ checkAnimalSpawn() {
529
+ // 更宽松的动物出现条件
530
+ if (this.foodCount >= this.config.animalSpawnThreshold) {
531
+ // 每2个食物就有机会出现动物,并且增加随机性
532
+ if (this.foodCount % 2 === 0 && Math.random() < 0.3) {
533
+ this.showDebug(`食物数量达到${this.foodCount},尝试生成动物`);
534
+ this.spawnAnimal();
535
+ }
536
+ } else {
537
+ // 调试信息:显示距离动物出现还需要多少食物
538
+ if (this.foodCount % 2 === 0) {
539
+ const remaining = this.config.animalSpawnThreshold - this.foodCount;
540
+ this.showDebug(`还需要${remaining}个食物才能出现动物(当前:${this.foodCount})`);
541
+ }
542
+ }
543
+ }
544
+
545
+ checkEasterEggs() {
546
+ // 草莓和西瓜的特殊彩蛋
547
+ if ((this.currentFood === 'strawberry' || this.currentFood === 'watermelon') &&
548
+ this.foodCount >= this.config.easterEggThreshold) {
549
+
550
+ // 检查是否已经有小女孩了
551
+ const hasGirl = this.animalSystem.animals.some(animal => animal.type === 'girl');
552
+
553
+ if (!hasGirl && this.foodCount % 15 === 0) {
554
+ this.spawnLittleGirl();
555
+ this.showSpecialMessage('小女孩被美味的水果吸引过来了!');
556
+ }
557
+ }
558
+
559
+ // 其他隐藏彩蛋
560
+ this.checkSecretEasterEggs();
561
+ }
562
+
563
+ checkSecretEasterEggs() {
564
+ // 特殊组合彩蛋
565
+ if (this.foodCount === 100) {
566
+ this.createCelebrationEffect();
567
+ this.showSpecialMessage('恭喜!你制作了100个食物!');
568
+ }
569
+
570
+ // 特定食物的特殊效果
571
+ if (this.currentFood === 'cake' && this.foodCount >= 20) {
572
+ this.createBirthdayEffect();
573
+ }
574
+ }
575
+
576
+ spawnAnimal() {
577
+ // 检查是否达到最大动物数量
578
+ if (this.animalSystem.getAnimalCount() >= this.config.maxAnimals) {
579
+ this.showDebug('已达到最大动物数量,不再生成新动物');
580
+ return;
581
+ }
582
+
583
+ // 检查当前食物是否已设置
584
+ if (!this.currentFood) {
585
+ this.showDebug('当前食物未设置,无法生成动物');
586
+ return;
587
+ }
588
+
589
+ const side = Math.random() < 0.5 ? 'left' : 'right';
590
+ console.log(`尝试生成动物: 食物类型=${this.currentFood}, 边=${side}, 当前动物数=${this.animalSystem.getAnimalCount()}`);
591
+
592
+ const animal = this.animalSystem.createAnimal(this.currentFood, side);
593
+ if (animal) {
594
+ // 动物已经在 createAnimal 方法中添加到 animalSystem.animals 了
595
+ this.showDebug(`🐾 小动物出现了!类型: ${animal.type}, 食物: ${this.currentFood}, 边: ${side}, 总数: ${this.animalSystem.getAnimalCount()}`);
596
+
597
+ // 播放动物出现音效
598
+ if (this.audio) {
599
+ this.audio.playAnimalAppear();
600
+ }
601
+ } else {
602
+ this.showDebug('❌ 动物生成失败');
603
+ }
604
+ }
605
+
606
+ spawnLittleGirl() {
607
+ const girl = this.animalSystem.createLittleGirl('right');
608
+ if (girl) {
609
+ // 小女孩已经在 createLittleGirl 方法中添加到 animalSystem.animals 了
610
+ this.showDebug('小女孩出现了!');
611
+
612
+ // 播放小女孩出现音效
613
+ if (this.audio) {
614
+ this.audio.playGirlAppear();
615
+ }
616
+
617
+ // 创建特殊效果
618
+ this.particleSystem.createSparkleParticles(
619
+ this.canvas.width - 100,
620
+ this.canvas.height - 100,
621
+ 10
622
+ );
623
+ }
624
+ }
625
+
626
+ createCelebrationEffect() {
627
+ // 播放庆祝音效
628
+ if (this.audio) {
629
+ this.audio.playCelebration();
630
+ }
631
+
632
+ // 创建庆祝烟花效果
633
+ for (let i = 0; i < 5; i++) {
634
+ setTimeout(() => {
635
+ const x = Math.random() * this.canvas.width;
636
+ const y = Math.random() * this.canvas.height * 0.5;
637
+ this.particleSystem.createExplosionParticles(x, y, 15, '#FFD700');
638
+ this.particleSystem.createExplosionParticles(x, y, 15, '#FF69B4');
639
+ }, i * 500);
640
+ }
641
+ }
642
+
643
+ createBirthdayEffect() {
644
+ // 生日蛋糕特效
645
+ const centerX = this.canvas.width / 2;
646
+ const centerY = this.canvas.height / 2;
647
+
648
+ this.particleSystem.createSparkleParticles(centerX, centerY, 20);
649
+ this.showSpecialMessage('生日快乐!🎂');
650
+ }
651
+
652
+ showSpecialMessage(message) {
653
+ // 显示特殊消息
654
+ const messageDiv = document.createElement('div');
655
+ messageDiv.textContent = message;
656
+ messageDiv.style.cssText = `
657
+ position: fixed;
658
+ top: 50%;
659
+ left: 50%;
660
+ transform: translate(-50%, -50%);
661
+ background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
662
+ color: white;
663
+ padding: 20px 30px;
664
+ border-radius: 15px;
665
+ font-size: 18px;
666
+ font-weight: bold;
667
+ box-shadow: 0 8px 32px rgba(0,0,0,0.3);
668
+ z-index: 10000;
669
+ animation: specialMessage 3s ease-in-out forwards;
670
+ `;
671
+
672
+ document.body.appendChild(messageDiv);
673
+
674
+ setTimeout(() => {
675
+ if (messageDiv.parentNode) {
676
+ messageDiv.parentNode.removeChild(messageDiv);
677
+ }
678
+ }, 3000);
679
+ }
680
+
681
+ render() {
682
+ // 清空画布
683
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
684
+
685
+ // 绘制背景
686
+ this.renderBackground();
687
+
688
+ // 绘制魔法锅
689
+ this.pot.render(this.ctx);
690
+
691
+ // 绘制粒子
692
+ this.particleSystem.render(this.ctx);
693
+
694
+ // 绘制动物
695
+ this.animalSystem.render(this.ctx);
696
+
697
+ // 绘制特效
698
+ this.renderEffects();
699
+ }
700
+
701
+ renderBackground() {
702
+ // 绘制渐变背景
703
+ const gradient = this.ctx.createLinearGradient(0, 0, 0, this.canvas.height);
704
+ gradient.addColorStop(0, '#87CEEB');
705
+ gradient.addColorStop(1, '#98FB98');
706
+
707
+ this.ctx.fillStyle = gradient;
708
+ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
709
+
710
+ // 绘制装饰性元素
711
+ this.renderDecorations();
712
+ }
713
+
714
+ renderDecorations() {
715
+ // 绘制云朵
716
+ this.ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
717
+ this.drawCloud(100, 80, 60);
718
+ this.drawCloud(300, 60, 40);
719
+ this.drawCloud(500, 90, 50);
720
+ }
721
+
722
+ drawCloud(x, y, size) {
723
+ this.ctx.beginPath();
724
+ this.ctx.arc(x, y, size * 0.5, 0, Math.PI * 2);
725
+ this.ctx.arc(x + size * 0.3, y, size * 0.6, 0, Math.PI * 2);
726
+ this.ctx.arc(x + size * 0.6, y, size * 0.4, 0, Math.PI * 2);
727
+ this.ctx.arc(x - size * 0.3, y, size * 0.4, 0, Math.PI * 2);
728
+ this.ctx.fill();
729
+ }
730
+
731
+ renderEffects() {
732
+ // 绘制魔法粒子效果
733
+ if (this.gameState === 'activated' || this.gameState === 'cooking') {
734
+ this.renderMagicSparkles();
735
+ }
736
+ }
737
+
738
+ renderMagicSparkles() {
739
+ const time = Date.now() * 0.005;
740
+ this.ctx.fillStyle = '#FFD700';
741
+
742
+ for (let i = 0; i < 5; i++) {
743
+ const angle = (i / 5) * Math.PI * 2 + time;
744
+ const radius = 80 + Math.sin(time + i) * 20;
745
+ const x = this.pot.x + Math.cos(angle) * radius;
746
+ const y = this.pot.y + Math.sin(angle) * radius;
747
+
748
+ this.ctx.beginPath();
749
+ this.ctx.arc(x, y, 3, 0, Math.PI * 2);
750
+ this.ctx.fill();
751
+ }
752
+ }
753
+
754
+ updateUI() {
755
+ // 更新锅状态
756
+ const potStatus = document.getElementById('potStatus');
757
+ const statusIndicator = potStatus.querySelector('.status-indicator');
758
+
759
+ statusIndicator.className = 'status-indicator';
760
+
761
+ switch (this.gameState) {
762
+ case 'idle':
763
+ statusIndicator.textContent = '魔法锅休眠中';
764
+ statusIndicator.classList.add('inactive');
765
+ break;
766
+ case 'activated':
767
+ statusIndicator.textContent = '魔法锅已激活';
768
+ statusIndicator.classList.add('active');
769
+ break;
770
+ case 'cooking':
771
+ statusIndicator.textContent = '正在烹饪中...';
772
+ statusIndicator.classList.add('cooking');
773
+ break;
774
+ case 'overflowing':
775
+ statusIndicator.textContent = '食物涌出中!';
776
+ statusIndicator.classList.add('overflowing');
777
+ break;
778
+ case 'stopped':
779
+ statusIndicator.textContent = '烹饪已停止';
780
+ statusIndicator.classList.add('inactive');
781
+ break;
782
+ }
783
+
784
+ // 更新烹饪信息
785
+ const cookingInfo = document.getElementById('cookingInfo');
786
+ const currentFoodSpan = document.getElementById('currentFood');
787
+
788
+ if (this.gameState === 'cooking' && this.currentFood) {
789
+ cookingInfo.style.display = 'block';
790
+ currentFoodSpan.textContent = this.currentFood;
791
+ } else {
792
+ cookingInfo.style.display = 'none';
793
+ }
794
+
795
+ // 更新食物计数器
796
+ const foodCounter = document.getElementById('foodCounter');
797
+ const foodCount = document.getElementById('foodCount');
798
+ const foodEmoji = document.getElementById('foodEmoji');
799
+
800
+ if (this.gameState === 'overflowing' || this.gameState === 'stopped') {
801
+ foodCounter.style.display = 'block';
802
+ foodCount.textContent = this.foodCount;
803
+ foodEmoji.textContent = this.getFoodEmoji(this.currentFood);
804
+ } else {
805
+ foodCounter.style.display = 'none';
806
+ }
807
+
808
+ // 更新语音提示
809
+ this.updateVoiceHints();
810
+
811
+ // 更新性能监控
812
+ this.updatePerformanceDisplay();
813
+ }
814
+
815
+ updateVoiceHints() {
816
+ const foodHint = document.getElementById('foodHint');
817
+ const stopHint = document.getElementById('stopHint');
818
+
819
+ foodHint.style.display = this.gameState === 'activated' ? 'block' : 'none';
820
+ stopHint.style.display = this.gameState === 'overflowing' ? 'block' : 'none';
821
+ }
822
+
823
+ updatePerformanceDisplay() {
824
+ const performanceMonitor = document.getElementById('performanceMonitor');
825
+ const particleCount = document.getElementById('particleCount');
826
+ const animalCount = document.getElementById('animalCount');
827
+ const fpsCount = document.getElementById('fpsCount');
828
+
829
+ if (performanceMonitor && particleCount && animalCount && fpsCount) {
830
+ const perfInfo = this.getPerformanceInfo();
831
+
832
+ particleCount.textContent = perfInfo.particles;
833
+ animalCount.textContent = perfInfo.animals;
834
+ fpsCount.textContent = perfInfo.fps;
835
+
836
+ // 根据游戏状态显示/隐藏性能监控
837
+ const shouldShow = this.gameState === 'overflowing' ||
838
+ this.gameState === 'cooking' ||
839
+ perfInfo.particles > 50;
840
+
841
+ performanceMonitor.style.display = shouldShow ? 'block' : 'none';
842
+
843
+ // 性能警告颜色
844
+ if (perfInfo.particles > this.config.maxParticles * 0.8) {
845
+ particleCount.style.color = '#ff6b6b';
846
+ } else {
847
+ particleCount.style.color = '#ffffff';
848
+ }
849
+
850
+ if (perfInfo.animals > this.config.maxAnimals * 0.8) {
851
+ animalCount.style.color = '#ff6b6b';
852
+ } else {
853
+ animalCount.style.color = '#ffffff';
854
+ }
855
+
856
+ if (perfInfo.fps < 30) {
857
+ fpsCount.style.color = '#ff6b6b';
858
+ } else if (perfInfo.fps < 45) {
859
+ fpsCount.style.color = '#f39c12';
860
+ } else {
861
+ fpsCount.style.color = '#27ae60';
862
+ }
863
+ }
864
+ }
865
+
866
+ updateCookingProgress(progress) {
867
+ const progressFill = document.getElementById('progressFill');
868
+ if (progressFill) {
869
+ progressFill.style.width = progress + '%';
870
+ }
871
+ }
872
+
873
+ updateVoiceStatus(status) {
874
+ const voiceStatus = document.getElementById('voiceStatus');
875
+ const statusText = voiceStatus.querySelector('.status-text');
876
+
877
+ switch (status) {
878
+ case 'listening':
879
+ statusText.textContent = '正在听取...';
880
+ voiceStatus.style.background = '#e74c3c';
881
+ break;
882
+ case 'processing':
883
+ statusText.textContent = '处理中...';
884
+ voiceStatus.style.background = '#f39c12';
885
+ break;
886
+ case 'ready':
887
+ statusText.textContent = '准备就绪';
888
+ voiceStatus.style.background = '#4ecdc4';
889
+ break;
890
+ case 'error':
891
+ statusText.textContent = '语音错误';
892
+ voiceStatus.style.background = '#95a5a6';
893
+ break;
894
+ }
895
+ }
896
+
897
+ getFoodEmoji(foodName) {
898
+ const emojiMap = {
899
+ 'apple': '🍎',
900
+ 'banana': '🍌',
901
+ 'orange': '🍊',
902
+ 'strawberry': '🍓',
903
+ 'watermelon': '🍉',
904
+ 'grape': '🍇',
905
+ 'pizza': '🍕',
906
+ 'burger': '🍔',
907
+ 'cake': '🍰',
908
+ 'cookie': '🍪',
909
+ 'bread': '🍞',
910
+ 'cheese': '🧀'
911
+ };
912
+
913
+ return emojiMap[foodName] || '🍽️';
914
+ }
915
+
916
+ showDebug(message) {
917
+ const debugLog = document.getElementById('debugLog');
918
+ if (debugLog) {
919
+ const time = new Date().toLocaleTimeString();
920
+ const perfInfo = this.getPerformanceInfo();
921
+ const perfText = `FPS:${perfInfo.fps} P:${perfInfo.particles} A:${perfInfo.animals} M:${perfInfo.memoryUsage}KB`;
922
+ debugLog.innerHTML += `<div>[${time}] [${perfText}] ${message}</div>`;
923
+ debugLog.scrollTop = debugLog.scrollHeight;
924
+
925
+ // 限制调试日志行数
926
+ const lines = debugLog.children;
927
+ if (lines.length > 50) {
928
+ for (let i = 0; i < 10; i++) {
929
+ debugLog.removeChild(lines[0]);
930
+ }
931
+ }
932
+ }
933
+ console.log(`[MagicPot] ${message}`);
934
+ }
935
+
936
+ toggleDebug() {
937
+ const debugInfo = document.getElementById('debugInfo');
938
+ debugInfo.style.display = debugInfo.style.display === 'none' ? 'block' : 'none';
939
+ }
940
+
941
+ resizeCanvas() {
942
+ this.setupCanvas();
943
+ }
944
+
945
+ showVoiceHelp() {
946
+ const helpContent = `
947
+ <h3>🎤 语音命令帮助</h3>
948
+ <div class="help-section">
949
+ <h4>🪄 激活魔法锅</h4>
950
+ <p>说出:"<strong>cook cook cook pot</strong>"</p>
951
+ <p>其他可用命令:</p>
952
+ <ul>
953
+ <li>"magic pot cook"</li>
954
+ <li>"cook three times pot"</li>
955
+ </ul>
956
+ </div>
957
+
958
+ <div class="help-section">
959
+ <h4>🍎 开始烹饪</h4>
960
+ <p>魔法锅激活后,说出任何食物名称:</p>
961
+ <ul>
962
+ <li><strong>水果:</strong>apple, banana, orange, strawberry, watermelon, grape</li>
963
+ <li><strong>主食:</strong>pizza, burger, bread, rice, noodles</li>
964
+ <li><strong>甜点:</strong>cake, cookie</li>
965
+ <li><strong>其他:</strong>cheese, fish, chicken</li>
966
+ </ul>
967
+ </div>
968
+
969
+ <div class="help-section">
970
+ <h4>🛑 停止烹饪</h4>
971
+ <p>说出:"<strong>stop stop stop pot</strong>"</p>
972
+ <p>其他可用命令:</p>
973
+ <ul>
974
+ <li>"magic pot stop"</li>
975
+ <li>"stop three times pot"</li>
976
+ </ul>
977
+ </div>
978
+
979
+ <div class="help-section">
980
+ <h4>🎁 特殊彩蛋</h4>
981
+ <ul>
982
+ <li><strong>草莓/西瓜:</strong>数量达到30个时,小女孩会出现</li>
983
+ <li><strong>生日蛋糕:</strong>制作蛋糕时有特殊庆祝效果</li>
984
+ <li><strong>百食庆祝:</strong>制作100个食物时有烟花效果</li>
985
+ </ul>
986
+ </div>
987
+
988
+ <div class="help-section">
989
+ <h4>🔊 音效控制</h4>
990
+ <ul>
991
+ <li><strong>音效按钮:</strong>点击🔊按钮开关音效</li>
992
+ <li><strong>F10:</strong>快捷键切换音效开关</li>
993
+ <li><strong>F11:</strong>测试所有音效</li>
994
+ </ul>
995
+ </div>
996
+
997
+ <div class="help-section">
998
+ <h4>🎵 音效说明</h4>
999
+ <ul>
1000
+ <li><strong>激活音效:</strong>魔法锅激活时的神奇音调</li>
1001
+ <li><strong>烹饪音效:</strong>烹饪过程中的冒泡声</li>
1002
+ <li><strong>食物弹出:</strong>食物涌出时的弹跳声</li>
1003
+ <li><strong>动物出现:</strong>小动物出现时的鸟叫声</li>
1004
+ <li><strong>小女孩:</strong>小女孩出现时的铃声</li>
1005
+ <li><strong>庆祝音效:</strong>达成成就时的号角声</li>
1006
+ </ul>
1007
+ </div>
1008
+
1009
+ <div class="help-section">
1010
+ <h4>💡 使用技巧</h4>
1011
+ <ul>
1012
+ <li>说话清晰,语速适中</li>
1013
+ <li>确保浏览器已允许麦克风权限</li>
1014
+ <li>在安静的环境中使用效果更佳</li>
1015
+ <li>按F12可查看调试信息</li>
1016
+ <li><strong>按F9激活/取消激活键盘面板</strong></li>
1017
+ <li><strong>按F8显示/隐藏键盘面板</strong></li>
1018
+ <li><strong>按F7清空命令历史</strong></li>
1019
+ <li><strong>按F5重置面板位置</strong></li>
1020
+ <li><strong>按F4调整食物生成速度</strong></li>
1021
+ <li><strong>按Alt+A强制生成动物(调试)</strong></li>
1022
+ <li>首次使用时点击任意按钮激活音频</li>
1023
+ </ul>
1024
+ </div>
1025
+
1026
+ <div class="help-section">
1027
+ <h4>⌨️ 键盘输入调试</h4>
1028
+ <ul>
1029
+ <li><strong>F9:</strong> 激活/取消激活键盘面板 ⭐</li>
1030
+ <li><strong>Ctrl+K:</strong> 聚焦到输入框(自动激活)</li>
1031
+ <li><strong>Ctrl+反引号:</strong> 切换输入面板</li>
1032
+ <li><strong>数字键1-6:</strong> 快速执行常用命令</li>
1033
+ <li><strong>↑↓方向键:</strong> 浏览命令历史</li>
1034
+ <li><strong>Enter:</strong> 执行当前输入的命令</li>
1035
+ <li><strong>Esc:</strong> 清空输入或取消激活</li>
1036
+ </ul>
1037
+ <p style="color: #f39c12; font-style: italic;">
1038
+ 💡 面板默认隐藏,按F9激活并显示面板后才能输入命令<br>
1039
+ 🖱️ 激活后可拖拽标题栏移动面板,按F5重置到右下角<br>
1040
+ 👁️ 按F8可手动显示/隐藏面板,取消激活时自动隐藏
1041
+ </p>
1042
+ </div>
1043
+ `;
1044
+
1045
+ this.showModal('语音命令帮助', helpContent);
1046
+ }
1047
+
1048
+ showModal(title, content) {
1049
+ // 创建模态框
1050
+ const modal = document.createElement('div');
1051
+ modal.className = 'help-modal';
1052
+ modal.innerHTML = `
1053
+ <div class="modal-overlay"></div>
1054
+ <div class="modal-content">
1055
+ <div class="modal-header">
1056
+ <h2>${title}</h2>
1057
+ <button class="modal-close">✕</button>
1058
+ </div>
1059
+ <div class="modal-body">
1060
+ ${content}
1061
+ </div>
1062
+ </div>
1063
+ `;
1064
+
1065
+ document.body.appendChild(modal);
1066
+
1067
+ // 关闭按钮事件
1068
+ const closeBtn = modal.querySelector('.modal-close');
1069
+ const overlay = modal.querySelector('.modal-overlay');
1070
+
1071
+ const closeModal = () => {
1072
+ modal.style.animation = 'modalFadeOut 0.3s ease-out forwards';
1073
+ setTimeout(() => {
1074
+ if (modal.parentNode) {
1075
+ modal.parentNode.removeChild(modal);
1076
+ }
1077
+ }, 300);
1078
+ };
1079
+
1080
+ closeBtn.addEventListener('click', closeModal);
1081
+ overlay.addEventListener('click', closeModal);
1082
+
1083
+ // ESC键关闭
1084
+ const handleKeydown = (e) => {
1085
+ if (e.key === 'Escape') {
1086
+ closeModal();
1087
+ document.removeEventListener('keydown', handleKeydown);
1088
+ }
1089
+ };
1090
+ document.addEventListener('keydown', handleKeydown);
1091
+ }
1092
+
1093
+ // 性能监控和优化方法
1094
+ startPerformanceMonitoring() {
1095
+ this.showDebug('性能监控已启动');
1096
+ }
1097
+
1098
+ updatePerformanceStats(currentTime) {
1099
+ // 更新FPS
1100
+ this.performanceStats.frameCount++;
1101
+ if (currentTime - this.performanceStats.lastFpsTime >= 1000) {
1102
+ this.performanceStats.fps = this.performanceStats.frameCount;
1103
+ this.performanceStats.frameCount = 0;
1104
+ this.performanceStats.lastFpsTime = currentTime;
1105
+ }
1106
+
1107
+ // 更新粒子和动物统计
1108
+ this.performanceStats.particles = this.particleSystem.getParticleCount();
1109
+ this.performanceStats.animals = this.animalSystem.getAnimalCount();
1110
+ }
1111
+
1112
+ performCleanupIfNeeded(currentTime) {
1113
+ // 定期清理
1114
+ if (currentTime - this.lastCleanupTime >= this.config.cleanupInterval) {
1115
+ this.performMemoryCleanup();
1116
+ this.lastCleanupTime = currentTime;
1117
+ }
1118
+ }
1119
+
1120
+ performMemoryCleanup() {
1121
+ const beforeParticles = this.particleSystem.getParticleCount();
1122
+ const beforeAnimals = this.animalSystem.getAnimalCount();
1123
+
1124
+ // 清理地面上过多的粒子
1125
+ this.cleanupGroundedParticles();
1126
+
1127
+ // 清理长时间存在的动物
1128
+ this.cleanupOldAnimals();
1129
+
1130
+ // 清理无效的粒子
1131
+ this.particleSystem.cleanupInvalidParticles();
1132
+
1133
+ const afterParticles = this.particleSystem.getParticleCount();
1134
+ const afterAnimals = this.animalSystem.getAnimalCount();
1135
+
1136
+ if (beforeParticles !== afterParticles || beforeAnimals !== afterAnimals) {
1137
+ this.showDebug(`内存清理完成 - 粒子: ${beforeParticles}→${afterParticles}, 动物: ${beforeAnimals}→${afterAnimals}`);
1138
+ }
1139
+ }
1140
+
1141
+ cleanupGroundedParticles() {
1142
+ const groundedParticles = this.particleSystem.particles.filter(p =>
1143
+ p.type === 'food' && p.isGrounded
1144
+ );
1145
+
1146
+ if (groundedParticles.length > this.config.maxGroundedParticles) {
1147
+ // 移除最老的地面粒子
1148
+ const toRemove = groundedParticles.length - this.config.maxGroundedParticles;
1149
+ const oldestParticles = groundedParticles
1150
+ .sort((a, b) => a.lifeTime - b.lifeTime)
1151
+ .slice(0, toRemove);
1152
+
1153
+ oldestParticles.forEach(particle => {
1154
+ const index = this.particleSystem.particles.indexOf(particle);
1155
+ if (index > -1) {
1156
+ this.particleSystem.particles.splice(index, 1);
1157
+ }
1158
+ });
1159
+ }
1160
+ }
1161
+
1162
+ cleanupOldAnimals() {
1163
+ // 移除长时间未活动的动物
1164
+ const oldAnimals = this.animalSystem.animals.filter(animal =>
1165
+ animal.lifeTime > 45 && animal.state !== 'eating'
1166
+ );
1167
+
1168
+ oldAnimals.forEach(animal => {
1169
+ animal.shouldRemove = true;
1170
+ });
1171
+ }
1172
+
1173
+ // 获取性能统计信息
1174
+ getPerformanceInfo() {
1175
+ return {
1176
+ fps: this.performanceStats.fps,
1177
+ particles: this.performanceStats.particles,
1178
+ animals: this.performanceStats.animals,
1179
+ foodCount: this.foodCount,
1180
+ gameState: this.gameState,
1181
+ memoryUsage: this.estimateMemoryUsage()
1182
+ };
1183
+ }
1184
+
1185
+ estimateMemoryUsage() {
1186
+ // 粗略估算内存使用量(KB)
1187
+ const particleMemory = this.performanceStats.particles * 0.5; // 每个粒子约0.5KB
1188
+ const animalMemory = this.performanceStats.animals * 1; // 每个动物约1KB
1189
+ const baseMemory = 100; // 基础内存约100KB
1190
+
1191
+ return Math.round(baseMemory + particleMemory + animalMemory);
1192
+ }
1193
+
1194
+ // 强制生成动物(调试用)
1195
+ forceSpawnAnimal() {
1196
+ console.log('=== 强制生成动物调试 ===');
1197
+ console.log(`当前食物: ${this.currentFood}`);
1198
+ console.log(`食物数量: ${this.foodCount}`);
1199
+ console.log(`当前动物数: ${this.animalSystem.getAnimalCount()}`);
1200
+ console.log(`最大动物数: ${this.config.maxAnimals}`);
1201
+ console.log(`游戏状态: ${this.gameState}`);
1202
+ console.log(`Canvas尺寸: ${this.canvas.width}x${this.canvas.height}`);
1203
+
1204
+ if (!this.currentFood) {
1205
+ this.showDebug('❌ 当前没有食物类型,无法生成动物');
1206
+ return;
1207
+ }
1208
+
1209
+ if (this.animalSystem.getAnimalCount() >= this.config.maxAnimals) {
1210
+ this.showDebug(`❌ 已达到最大动物数量 ${this.config.maxAnimals},清理一些动物后再试`);
1211
+ // 清理一些老动物
1212
+ this.animalSystem.makeAllAnimalsLeave();
1213
+ return;
1214
+ }
1215
+
1216
+ this.showDebug('🔧 强制尝试生成动物...');
1217
+ this.spawnAnimal();
1218
+
1219
+ // 显示当前所有动物的状态
1220
+ setTimeout(() => {
1221
+ console.log('=== 当前动物状态 ===');
1222
+ this.animalSystem.animals.forEach((animal, index) => {
1223
+ console.log(`动物${index + 1}: ${animal.type}, 位置(${animal.x}, ${animal.y}), 状态: ${animal.state}, emoji: ${animal.emoji}`);
1224
+ });
1225
+ }, 100);
1226
+ }
1227
+
1228
+ // 音效控制方法
1229
+ toggleAudio() {
1230
+ if (this.audio) {
1231
+ const newState = !this.audio.isEnabled;
1232
+ this.audio.setEnabled(newState);
1233
+ this.updateAudioButton();
1234
+
1235
+ if (newState) {
1236
+ this.showDebug('音效已开启');
1237
+ // 播放测试音效
1238
+ setTimeout(() => {
1239
+ this.audio.playPotActivate();
1240
+ }, 100);
1241
+ } else {
1242
+ this.showDebug('音效已关闭');
1243
+ }
1244
+ }
1245
+ }
1246
+
1247
+ updateAudioButton() {
1248
+ const audioButton = document.getElementById('audioButton');
1249
+ if (audioButton && this.audio) {
1250
+ if (this.audio.isEnabled) {
1251
+ audioButton.textContent = '🔊';
1252
+ audioButton.classList.remove('muted');
1253
+ audioButton.title = '点击关闭音效';
1254
+ } else {
1255
+ audioButton.textContent = '🔇';
1256
+ audioButton.classList.add('muted');
1257
+ audioButton.title = '点击开启音效';
1258
+ }
1259
+ }
1260
+ }
1261
+
1262
+ // 音效测试功能
1263
+ testAudio() {
1264
+ if (this.audio) {
1265
+ this.showDebug('开始音效测试...');
1266
+ this.audio.testAllSounds();
1267
+ }
1268
+ }
1269
+
1270
+ // 重启语音识别
1271
+ restartVoice() {
1272
+ if (this.voice) {
1273
+ this.showDebug('手动重启语音识别系统');
1274
+ this.voice.restart();
1275
+ }
1276
+ }
1277
+
1278
+ // 切换键盘输入面板
1279
+ toggleKeyboardPanel() {
1280
+ if (this.keyboard) {
1281
+ this.keyboard.togglePanel();
1282
+ const stats = this.keyboard.getStats();
1283
+ this.showDebug(`键盘面板${stats.isCollapsed ? '已折叠' : '已展开'}`);
1284
+ }
1285
+ }
1286
+
1287
+ // 清空命令历史
1288
+ clearCommandHistory() {
1289
+ if (this.keyboard) {
1290
+ this.keyboard.clearHistory();
1291
+ this.showDebug('键盘命令历史已清空');
1292
+ }
1293
+ }
1294
+
1295
+ // 切换键盘面板激活状态
1296
+ toggleKeyboardActivation() {
1297
+ if (this.keyboard) {
1298
+ console.log('F9 pressed - toggling keyboard activation');
1299
+ this.keyboard.toggleActiveState();
1300
+ const stats = this.keyboard.getStats();
1301
+
1302
+ // 激活时自动展开面板,取消激活时隐藏面板
1303
+ if (stats.isActive) {
1304
+ this.keyboard.setPanelCollapsed(false);
1305
+ this.showDebug('键盘面板已激活并显示');
1306
+ } else {
1307
+ this.keyboard.setPanelCollapsed(true);
1308
+ this.showDebug('键盘面板已取消激活并隐藏');
1309
+ }
1310
+
1311
+ console.log('Keyboard activation state:', stats.isActive);
1312
+ } else {
1313
+ console.log('Keyboard handler not initialized');
1314
+ }
1315
+ }
1316
+
1317
+ // 重置键盘面板位置
1318
+ resetKeyboardPanelPosition() {
1319
+ if (this.keyboard) {
1320
+ this.keyboard.resetPosition();
1321
+ this.showDebug('键盘面板位置已重置');
1322
+ }
1323
+ }
1324
+
1325
+ // 调整食物生成速度
1326
+ adjustFoodSpawnSpeed() {
1327
+ // 循环切换生成速度:慢速(800ms) -> 中速(400ms) -> 快速(200ms) -> 超快(100ms)
1328
+ const speeds = [
1329
+ { interval: 800, rate: 1, name: '慢速' },
1330
+ { interval: 400, rate: 1, name: '中速' },
1331
+ { interval: 200, rate: 2, name: '快速' },
1332
+ { interval: 100, rate: 3, name: '超快' }
1333
+ ];
1334
+
1335
+ // 找到当前速度的索引
1336
+ let currentIndex = speeds.findIndex(speed => speed.interval === this.foodSpawnInterval);
1337
+ if (currentIndex === -1) currentIndex = 0; // 默认慢速
1338
+
1339
+ // 切换到下一个速度
1340
+ const nextIndex = (currentIndex + 1) % speeds.length;
1341
+ const newSpeed = speeds[nextIndex];
1342
+
1343
+ this.foodSpawnInterval = newSpeed.interval;
1344
+ this.config.particleSpawnRate = newSpeed.rate;
1345
+
1346
+ this.showDebug(`食物生成速度已调整为: ${newSpeed.name} (${newSpeed.interval}ms间隔)`);
1347
+ this.showSpecialMessage(`🍽️ 食物生成速度: ${newSpeed.name}`);
1348
+ }
1349
+
1350
+ // 获取完整的调试信息
1351
+ getDebugInfo() {
1352
+ const info = {
1353
+ game: this.getPerformanceInfo(),
1354
+ voice: this.voice ? this.voice.getStatus() : null,
1355
+ keyboard: this.keyboard ? this.keyboard.getStats() : null
1356
+ };
1357
+
1358
+ return info;
1359
+ }
1360
+ }
1361
+
1362
+ // 游戏初始化
1363
+ document.addEventListener('DOMContentLoaded', () => {
1364
+ window.game = new MagicPotGame();
1365
+ });
js/particles.js ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 粒子系统类
2
+ class ParticleSystem {
3
+ constructor() {
4
+ this.particles = [];
5
+ this.maxParticles = 150; // 降低最大粒子数
6
+ this.gravity = 0.5;
7
+ this.bounce = 0.6;
8
+ this.friction = 0.98;
9
+ this.lastCleanupTime = 0;
10
+ }
11
+
12
+ update(deltaTime) {
13
+ const dt = deltaTime * 0.001; // 转换为秒
14
+
15
+ for (let i = this.particles.length - 1; i >= 0; i--) {
16
+ const particle = this.particles[i];
17
+ this.updateParticle(particle, dt);
18
+
19
+ // 移除生命周期结束的粒子
20
+ if (particle.life <= 0) {
21
+ this.particles.splice(i, 1);
22
+ }
23
+ }
24
+ }
25
+
26
+ updateParticle(particle, dt) {
27
+ // 更新位置
28
+ particle.x += particle.vx * dt;
29
+ particle.y += particle.vy * dt;
30
+
31
+ // 应用重力
32
+ particle.vy += this.gravity * dt * 60; // 60fps基准
33
+
34
+ // 应用摩擦力
35
+ particle.vx *= this.friction;
36
+
37
+ // 边界碰撞检测
38
+ this.handleBoundaryCollision(particle);
39
+
40
+ // 更新生命周期
41
+ particle.life -= dt / particle.maxLife;
42
+ particle.opacity = Math.max(0, particle.life);
43
+
44
+ // 更新大小(某些粒子会变化)
45
+ if (particle.sizeChange) {
46
+ particle.size += particle.sizeChange * dt;
47
+ particle.size = Math.max(1, particle.size);
48
+ }
49
+
50
+ // 更新旋转
51
+ if (particle.rotation !== undefined) {
52
+ particle.rotation += particle.rotationSpeed * dt;
53
+ }
54
+ }
55
+
56
+ handleBoundaryCollision(particle) {
57
+ const canvas = document.getElementById('gameCanvas');
58
+ const canvasWidth = canvas.width;
59
+ const canvasHeight = canvas.height;
60
+
61
+ // 底部碰撞
62
+ if (particle.y + particle.size > canvasHeight) {
63
+ particle.y = canvasHeight - particle.size;
64
+ particle.vy *= -this.bounce;
65
+
66
+ // 减少水平速度
67
+ particle.vx *= 0.8;
68
+
69
+ // 如果速度太小,停止弹跳
70
+ if (Math.abs(particle.vy) < 10) {
71
+ particle.vy = 0;
72
+ particle.isGrounded = true;
73
+ }
74
+ }
75
+
76
+ // 左右边界碰撞
77
+ if (particle.x - particle.size < 0) {
78
+ particle.x = particle.size;
79
+ particle.vx *= -this.bounce;
80
+ } else if (particle.x + particle.size > canvasWidth) {
81
+ particle.x = canvasWidth - particle.size;
82
+ particle.vx *= -this.bounce;
83
+ }
84
+
85
+ // 顶部边界(防止粒子飞出屏幕)
86
+ if (particle.y - particle.size < 0) {
87
+ particle.y = particle.size;
88
+ particle.vy *= -this.bounce;
89
+ }
90
+ }
91
+
92
+ createFoodParticle(x, y, foodType) {
93
+ if (this.particles.length >= this.maxParticles) {
94
+ // 移除最老的粒子
95
+ this.particles.shift();
96
+ }
97
+
98
+ const particle = {
99
+ x: x + (Math.random() - 0.5) * 20,
100
+ y: y + (Math.random() - 0.5) * 10,
101
+ vx: (Math.random() - 0.5) * 200,
102
+ vy: -Math.random() * 150 - 50,
103
+ size: Math.random() * 15 + 8,
104
+ color: this.getFoodColor(foodType),
105
+ emoji: this.getFoodEmoji(foodType),
106
+ life: 1,
107
+ maxLife: Math.random() * 3 + 2,
108
+ opacity: 1,
109
+ rotation: Math.random() * Math.PI * 2,
110
+ rotationSpeed: (Math.random() - 0.5) * 5,
111
+ type: 'food',
112
+ foodType: foodType,
113
+ isGrounded: false,
114
+ bounceCount: 0,
115
+ maxBounces: Math.floor(Math.random() * 3) + 2
116
+ };
117
+
118
+ this.particles.push(particle);
119
+ return particle;
120
+ }
121
+
122
+ createExplosionParticles(x, y, count = 10, color = '#FFD700') {
123
+ for (let i = 0; i < count; i++) {
124
+ const angle = (i / count) * Math.PI * 2;
125
+ const speed = Math.random() * 100 + 50;
126
+
127
+ const particle = {
128
+ x: x,
129
+ y: y,
130
+ vx: Math.cos(angle) * speed,
131
+ vy: Math.sin(angle) * speed,
132
+ size: Math.random() * 8 + 4,
133
+ color: color,
134
+ life: 1,
135
+ maxLife: Math.random() * 1 + 0.5,
136
+ opacity: 1,
137
+ type: 'explosion',
138
+ sizeChange: -5 // 粒子逐渐变小
139
+ };
140
+
141
+ this.particles.push(particle);
142
+ }
143
+ }
144
+
145
+ createSparkleParticles(x, y, count = 5) {
146
+ for (let i = 0; i < count; i++) {
147
+ const particle = {
148
+ x: x + (Math.random() - 0.5) * 50,
149
+ y: y + (Math.random() - 0.5) * 50,
150
+ vx: (Math.random() - 0.5) * 30,
151
+ vy: -Math.random() * 30 - 10,
152
+ size: Math.random() * 4 + 2,
153
+ color: Math.random() < 0.5 ? '#FFD700' : '#FF69B4',
154
+ life: 1,
155
+ maxLife: Math.random() * 1.5 + 0.5,
156
+ opacity: 1,
157
+ type: 'sparkle',
158
+ twinkle: true
159
+ };
160
+
161
+ this.particles.push(particle);
162
+ }
163
+ }
164
+
165
+ render(ctx) {
166
+ this.particles.forEach(particle => {
167
+ this.renderParticle(ctx, particle);
168
+ });
169
+ }
170
+
171
+ renderParticle(ctx, particle) {
172
+ ctx.save();
173
+ ctx.globalAlpha = particle.opacity;
174
+
175
+ // 移动到粒子位置
176
+ ctx.translate(particle.x, particle.y);
177
+
178
+ // 应用旋转
179
+ if (particle.rotation !== undefined) {
180
+ ctx.rotate(particle.rotation);
181
+ }
182
+
183
+ switch (particle.type) {
184
+ case 'food':
185
+ this.renderFoodParticle(ctx, particle);
186
+ break;
187
+ case 'explosion':
188
+ this.renderExplosionParticle(ctx, particle);
189
+ break;
190
+ case 'sparkle':
191
+ this.renderSparkleParticle(ctx, particle);
192
+ break;
193
+ default:
194
+ this.renderDefaultParticle(ctx, particle);
195
+ break;
196
+ }
197
+
198
+ ctx.restore();
199
+ }
200
+
201
+ renderFoodParticle(ctx, particle) {
202
+ // 绘制食物表情符号
203
+ if (particle.emoji) {
204
+ ctx.font = `${particle.size}px Arial`;
205
+ ctx.textAlign = 'center';
206
+ ctx.textBaseline = 'middle';
207
+ ctx.fillText(particle.emoji, 0, 0);
208
+ } else {
209
+ // 备用:绘制彩色圆形
210
+ ctx.fillStyle = particle.color;
211
+ ctx.beginPath();
212
+ ctx.arc(0, 0, particle.size / 2, 0, Math.PI * 2);
213
+ ctx.fill();
214
+
215
+ // 添加高光
216
+ ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
217
+ ctx.beginPath();
218
+ ctx.arc(-particle.size / 6, -particle.size / 6, particle.size / 4, 0, Math.PI * 2);
219
+ ctx.fill();
220
+ }
221
+ }
222
+
223
+ renderExplosionParticle(ctx, particle) {
224
+ ctx.fillStyle = particle.color;
225
+ ctx.shadowBlur = 10;
226
+ ctx.shadowColor = particle.color;
227
+
228
+ ctx.beginPath();
229
+ ctx.arc(0, 0, particle.size, 0, Math.PI * 2);
230
+ ctx.fill();
231
+ }
232
+
233
+ renderSparkleParticle(ctx, particle) {
234
+ ctx.fillStyle = particle.color;
235
+ ctx.shadowBlur = 8;
236
+ ctx.shadowColor = particle.color;
237
+
238
+ // 绘制星形
239
+ const spikes = 4;
240
+ const outerRadius = particle.size;
241
+ const innerRadius = particle.size * 0.4;
242
+
243
+ ctx.beginPath();
244
+ for (let i = 0; i < spikes * 2; i++) {
245
+ const angle = (i / (spikes * 2)) * Math.PI * 2;
246
+ const radius = i % 2 === 0 ? outerRadius : innerRadius;
247
+ const x = Math.cos(angle) * radius;
248
+ const y = Math.sin(angle) * radius;
249
+
250
+ if (i === 0) {
251
+ ctx.moveTo(x, y);
252
+ } else {
253
+ ctx.lineTo(x, y);
254
+ }
255
+ }
256
+ ctx.closePath();
257
+ ctx.fill();
258
+
259
+ // 闪烁效果
260
+ if (particle.twinkle) {
261
+ const twinkleAlpha = Math.sin(Date.now() * 0.01) * 0.5 + 0.5;
262
+ ctx.globalAlpha *= twinkleAlpha;
263
+ }
264
+ }
265
+
266
+ renderDefaultParticle(ctx, particle) {
267
+ ctx.fillStyle = particle.color || '#FFD700';
268
+ ctx.beginPath();
269
+ ctx.arc(0, 0, particle.size, 0, Math.PI * 2);
270
+ ctx.fill();
271
+ }
272
+
273
+ getFoodColor(foodType) {
274
+ const colorMap = {
275
+ 'apple': '#FF6B6B',
276
+ 'banana': '#FFE66D',
277
+ 'orange': '#FF8E53',
278
+ 'strawberry': '#FF6B9D',
279
+ 'watermelon': '#C44569',
280
+ 'grape': '#9B59B6',
281
+ 'pizza': '#F39C12',
282
+ 'burger': '#D35400',
283
+ 'cake': '#F8C471',
284
+ 'cookie': '#D2691E',
285
+ 'bread': '#DEB887',
286
+ 'cheese': '#F1C40F',
287
+ 'fish': '#3498DB',
288
+ 'chicken': '#E67E22',
289
+ 'rice': '#ECF0F1',
290
+ 'noodles': '#F4D03F'
291
+ };
292
+
293
+ return colorMap[foodType] || '#FFD700';
294
+ }
295
+
296
+ getFoodEmoji(foodType) {
297
+ const emojiMap = {
298
+ 'apple': '🍎',
299
+ 'banana': '🍌',
300
+ 'orange': '🍊',
301
+ 'strawberry': '🍓',
302
+ 'watermelon': '🍉',
303
+ 'grape': '🍇',
304
+ 'pizza': '🍕',
305
+ 'burger': '🍔',
306
+ 'cake': '🍰',
307
+ 'cookie': '🍪',
308
+ 'bread': '🍞',
309
+ 'cheese': '🧀',
310
+ 'fish': '🐟',
311
+ 'chicken': '🍗',
312
+ 'rice': '🍚',
313
+ 'noodles': '🍜',
314
+ 'carrot': '🥕',
315
+ 'tomato': '🍅',
316
+ 'corn': '🌽',
317
+ 'broccoli': '🥦',
318
+ 'potato': '🥔',
319
+ 'onion': '🧅'
320
+ };
321
+
322
+ return emojiMap[foodType] || '🍽️';
323
+ }
324
+
325
+ // 清理所有粒子
326
+ clear() {
327
+ this.particles = [];
328
+ }
329
+
330
+ // 获取指定类型的粒子数量
331
+ getParticleCount(type = null) {
332
+ if (type) {
333
+ return this.particles.filter(p => p.type === type).length;
334
+ }
335
+ return this.particles.length;
336
+ }
337
+
338
+ // 移除指定类型的粒子
339
+ removeParticlesByType(type) {
340
+ this.particles = this.particles.filter(p => p.type !== type);
341
+ }
342
+
343
+ // 获取地面上的食物粒子数量
344
+ getGroundedFoodCount() {
345
+ return this.particles.filter(p => p.type === 'food' && p.isGrounded).length;
346
+ }
347
+
348
+ // 在指定区域内创建粒子爆炸效果
349
+ createAreaExplosion(x, y, radius, particleCount = 20) {
350
+ for (let i = 0; i < particleCount; i++) {
351
+ const angle = Math.random() * Math.PI * 2;
352
+ const distance = Math.random() * radius;
353
+ const particleX = x + Math.cos(angle) * distance;
354
+ const particleY = y + Math.sin(angle) * distance;
355
+
356
+ this.createExplosionParticles(particleX, particleY, 3);
357
+ }
358
+ }
359
+
360
+ // 清理无效粒子
361
+ cleanupInvalidParticles() {
362
+ const beforeCount = this.particles.length;
363
+
364
+ // 移除生命周期结束的粒子
365
+ this.particles = this.particles.filter(particle => particle.life > 0);
366
+
367
+ // 移除超出边界太远的粒子
368
+ const canvas = document.getElementById('gameCanvas');
369
+ if (canvas) {
370
+ const margin = 100; // 边界外100像素
371
+ this.particles = this.particles.filter(particle => {
372
+ return particle.x > -margin &&
373
+ particle.x < canvas.width + margin &&
374
+ particle.y > -margin &&
375
+ particle.y < canvas.height + margin;
376
+ });
377
+ }
378
+
379
+ const afterCount = this.particles.length;
380
+ return beforeCount - afterCount; // 返回清理的粒子数量
381
+ }
382
+
383
+ // 优化粒子更新
384
+ updateParticle(particle, dt) {
385
+ // 更新生命周期
386
+ particle.life -= dt / particle.maxLife;
387
+ if (particle.life <= 0) {
388
+ return; // 粒子死亡,将在下次清理时移除
389
+ }
390
+
391
+ // 更新位置
392
+ particle.x += particle.vx * dt;
393
+ particle.y += particle.vy * dt;
394
+
395
+ // 应用重力
396
+ particle.vy += this.gravity * dt * 60; // 60fps基准
397
+
398
+ // 应用摩擦力
399
+ particle.vx *= this.friction;
400
+
401
+ // 边界碰撞检测
402
+ this.handleBoundaryCollision(particle);
403
+
404
+ // 更新透明度
405
+ particle.opacity = Math.max(0, particle.life);
406
+
407
+ // 更新大小(某些粒子会变化)
408
+ if (particle.sizeChange) {
409
+ particle.size += particle.sizeChange * dt;
410
+ particle.size = Math.max(1, particle.size);
411
+ }
412
+
413
+ // 更新旋转
414
+ if (particle.rotation !== undefined) {
415
+ particle.rotation += particle.rotationSpeed * dt;
416
+ }
417
+
418
+ // 更新生命时间(用于清理)
419
+ particle.lifeTime = (particle.lifeTime || 0) + dt;
420
+ }
421
+
422
+ // 获取特定状态的粒子统计
423
+ getParticleStats() {
424
+ const stats = {
425
+ total: this.particles.length,
426
+ food: 0,
427
+ explosion: 0,
428
+ sparkle: 0,
429
+ grounded: 0,
430
+ moving: 0
431
+ };
432
+
433
+ this.particles.forEach(particle => {
434
+ if (particle.type === 'food') {
435
+ stats.food++;
436
+ if (particle.isGrounded) stats.grounded++;
437
+ else stats.moving++;
438
+ } else if (particle.type === 'explosion') {
439
+ stats.explosion++;
440
+ } else if (particle.type === 'sparkle') {
441
+ stats.sparkle++;
442
+ }
443
+ });
444
+
445
+ return stats;
446
+ }
447
+
448
+ // 强制清理最老的粒子
449
+ forceCleanupOldest(count) {
450
+ if (count <= 0) return 0;
451
+
452
+ // 按生命时间排序,移除最老的
453
+ const sortedParticles = this.particles
454
+ .map((particle, index) => ({ particle, index, lifeTime: particle.lifeTime || 0 }))
455
+ .sort((a, b) => b.lifeTime - a.lifeTime);
456
+
457
+ const toRemove = Math.min(count, sortedParticles.length);
458
+ const indicesToRemove = sortedParticles
459
+ .slice(0, toRemove)
460
+ .map(item => item.index)
461
+ .sort((a, b) => b - a); // 从后往前删除
462
+
463
+ indicesToRemove.forEach(index => {
464
+ this.particles.splice(index, 1);
465
+ });
466
+
467
+ return toRemove;
468
+ }
469
+ }
470
+
471
+ // 食物粒子特殊效果类
472
+ class FoodParticleEffects {
473
+ static createBurstEffect(particleSystem, x, y, foodType, count = 15) {
474
+ for (let i = 0; i < count; i++) {
475
+ const angle = (i / count) * Math.PI * 2;
476
+ const speed = Math.random() * 80 + 40;
477
+
478
+ const particle = {
479
+ x: x,
480
+ y: y,
481
+ vx: Math.cos(angle) * speed,
482
+ vy: Math.sin(angle) * speed - 30,
483
+ size: Math.random() * 12 + 6,
484
+ color: particleSystem.getFoodColor(foodType),
485
+ emoji: particleSystem.getFoodEmoji(foodType),
486
+ life: 1,
487
+ maxLife: Math.random() * 2 + 1,
488
+ opacity: 1,
489
+ rotation: Math.random() * Math.PI * 2,
490
+ rotationSpeed: (Math.random() - 0.5) * 8,
491
+ type: 'food',
492
+ foodType: foodType,
493
+ isGrounded: false
494
+ };
495
+
496
+ particleSystem.particles.push(particle);
497
+ }
498
+ }
499
+
500
+ static createFountainEffect(particleSystem, x, y, foodType) {
501
+ const particleCount = 8;
502
+
503
+ for (let i = 0; i < particleCount; i++) {
504
+ const angle = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI / 3;
505
+ const speed = Math.random() * 120 + 80;
506
+
507
+ const particle = {
508
+ x: x + (Math.random() - 0.5) * 30,
509
+ y: y,
510
+ vx: Math.cos(angle) * speed,
511
+ vy: Math.sin(angle) * speed,
512
+ size: Math.random() * 18 + 10,
513
+ color: particleSystem.getFoodColor(foodType),
514
+ emoji: particleSystem.getFoodEmoji(foodType),
515
+ life: 1,
516
+ maxLife: Math.random() * 4 + 2,
517
+ opacity: 1,
518
+ rotation: Math.random() * Math.PI * 2,
519
+ rotationSpeed: (Math.random() - 0.5) * 6,
520
+ type: 'food',
521
+ foodType: foodType,
522
+ isGrounded: false
523
+ };
524
+
525
+ particleSystem.particles.push(particle);
526
+ }
527
+ }
528
+ }
js/pot.js ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 魔法锅类
2
+ class MagicPot {
3
+ constructor(x, y) {
4
+ this.x = x;
5
+ this.y = y;
6
+ this.width = 120;
7
+ this.height = 100;
8
+ this.state = 'idle'; // idle, activated, cooking, overflowing
9
+
10
+ // 动画属性
11
+ this.animationTime = 0;
12
+ this.glowIntensity = 0;
13
+ this.shakeAmount = 0;
14
+ this.lidOffset = 0;
15
+ this.steamParticles = [];
16
+
17
+ // 颜色配置
18
+ this.colors = {
19
+ pot: '#8B4513',
20
+ potHighlight: '#CD853F',
21
+ potShadow: '#654321',
22
+ lid: '#A0522D',
23
+ lidHighlight: '#DEB887',
24
+ glow: '#FFD700',
25
+ steam: '#FFFFFF',
26
+ magic: '#FF69B4'
27
+ };
28
+
29
+ // 食物和动物素材
30
+ this.foodImage = null;
31
+ this.animalImage = null;
32
+ this.currentFood = null;
33
+
34
+ // 魔法锅图片
35
+ this.potImage = null;
36
+ this.imageLoaded = false;
37
+ this.loadPotImage();
38
+ }
39
+
40
+ update(deltaTime) {
41
+ this.animationTime += deltaTime * 0.001;
42
+
43
+ // 根据状态更新动画
44
+ switch (this.state) {
45
+ case 'idle':
46
+ this.updateIdleAnimation();
47
+ break;
48
+ case 'activated':
49
+ this.updateActivatedAnimation();
50
+ break;
51
+ case 'cooking':
52
+ this.updateCookingAnimation();
53
+ break;
54
+ case 'overflowing':
55
+ this.updateOverflowingAnimation();
56
+ break;
57
+ }
58
+
59
+ // 更新蒸汽粒子
60
+ this.updateSteamParticles(deltaTime);
61
+ }
62
+
63
+ updateIdleAnimation() {
64
+ // 轻微的呼吸效果
65
+ this.glowIntensity = Math.sin(this.animationTime * 0.5) * 0.1 + 0.1;
66
+ this.shakeAmount = 0;
67
+ this.lidOffset = 0;
68
+ }
69
+
70
+ updateActivatedAnimation() {
71
+ // 发光效果
72
+ this.glowIntensity = Math.sin(this.animationTime * 2) * 0.3 + 0.5;
73
+ this.shakeAmount = 0;
74
+ this.lidOffset = 0;
75
+
76
+ // 生成魔法粒子
77
+ if (Math.random() < 0.1) {
78
+ this.createMagicParticle();
79
+ }
80
+ }
81
+
82
+ updateCookingAnimation() {
83
+ // 强烈的发光和轻微震动
84
+ this.glowIntensity = Math.sin(this.animationTime * 3) * 0.4 + 0.6;
85
+ this.shakeAmount = Math.sin(this.animationTime * 8) * 2;
86
+ this.lidOffset = Math.sin(this.animationTime * 4) * 3;
87
+
88
+ // 生成蒸汽
89
+ if (Math.random() < 0.3) {
90
+ this.createSteamParticle();
91
+ }
92
+
93
+ // 生成魔法粒子
94
+ if (Math.random() < 0.2) {
95
+ this.createMagicParticle();
96
+ }
97
+ }
98
+
99
+ updateOverflowingAnimation() {
100
+ // 剧烈震动,锅盖被顶开
101
+ this.glowIntensity = 1;
102
+ this.shakeAmount = Math.sin(this.animationTime * 15) * 5;
103
+ this.lidOffset = -20 + Math.sin(this.animationTime * 10) * 5;
104
+
105
+ // 大量蒸汽
106
+ if (Math.random() < 0.8) {
107
+ this.createSteamParticle();
108
+ }
109
+
110
+ // 魔法粒子
111
+ if (Math.random() < 0.5) {
112
+ this.createMagicParticle();
113
+ }
114
+ }
115
+
116
+ createSteamParticle() {
117
+ const particle = {
118
+ x: this.x + (Math.random() - 0.5) * this.width * 0.8,
119
+ y: this.y - this.height * 0.3,
120
+ vx: (Math.random() - 0.5) * 20,
121
+ vy: -Math.random() * 50 - 30,
122
+ size: Math.random() * 8 + 4,
123
+ opacity: 0.8,
124
+ life: 1,
125
+ maxLife: Math.random() * 2 + 1
126
+ };
127
+
128
+ this.steamParticles.push(particle);
129
+ }
130
+
131
+ createMagicParticle() {
132
+ const angle = Math.random() * Math.PI * 2;
133
+ const radius = this.width * 0.6;
134
+
135
+ const particle = {
136
+ x: this.x + Math.cos(angle) * radius,
137
+ y: this.y + Math.sin(angle) * radius * 0.5,
138
+ vx: Math.cos(angle) * 30,
139
+ vy: Math.sin(angle) * 30 - 20,
140
+ size: Math.random() * 4 + 2,
141
+ opacity: 1,
142
+ life: 1,
143
+ maxLife: Math.random() * 1.5 + 0.5,
144
+ color: Math.random() < 0.5 ? '#FFD700' : '#FF69B4'
145
+ };
146
+
147
+ this.steamParticles.push(particle);
148
+ }
149
+
150
+ updateSteamParticles(deltaTime) {
151
+ for (let i = this.steamParticles.length - 1; i >= 0; i--) {
152
+ const particle = this.steamParticles[i];
153
+
154
+ // 更新位置
155
+ particle.x += particle.vx * deltaTime * 0.001;
156
+ particle.y += particle.vy * deltaTime * 0.001;
157
+
158
+ // 更新生命周期
159
+ particle.life -= deltaTime * 0.001 / particle.maxLife;
160
+ particle.opacity = particle.life;
161
+
162
+ // 蒸汽粒子逐渐变大
163
+ if (!particle.color) {
164
+ particle.size += deltaTime * 0.002;
165
+ }
166
+
167
+ // 移除死亡的粒子
168
+ if (particle.life <= 0) {
169
+ this.steamParticles.splice(i, 1);
170
+ }
171
+ }
172
+ }
173
+
174
+ render(ctx) {
175
+ const currentX = this.x + (Math.random() - 0.5) * this.shakeAmount;
176
+ const currentY = this.y + (Math.random() - 0.5) * this.shakeAmount;
177
+
178
+ // 绘制发光效果
179
+ if (this.glowIntensity > 0) {
180
+ this.renderGlow(ctx, currentX, currentY);
181
+ }
182
+
183
+ // 绘制魔法锅(图片或默认绘制)
184
+ if (this.imageLoaded && this.potImage) {
185
+ this.renderPotImage(ctx, currentX, currentY);
186
+ } else {
187
+ // 备用绘制方案
188
+ this.renderPotBody(ctx, currentX, currentY);
189
+ this.renderPotLid(ctx, currentX, currentY + this.lidOffset);
190
+ }
191
+
192
+ // 绘制蒸汽和魔法粒子
193
+ this.renderParticles(ctx);
194
+
195
+ // 绘制装饰
196
+ this.renderDecorations(ctx, currentX, currentY);
197
+ }
198
+
199
+ renderPotImage(ctx, x, y) {
200
+ ctx.save();
201
+
202
+ // 如果有发光效果,添加阴影
203
+ if (this.glowIntensity > 0) {
204
+ ctx.shadowBlur = this.glowIntensity * 30;
205
+ ctx.shadowColor = '#FFD700';
206
+ }
207
+
208
+ // 计算图片尺寸和位置
209
+ const imageWidth = this.width * 1.5; // 稍微放大一点
210
+ const imageHeight = this.height * 1.5;
211
+ const imageX = x - imageWidth / 2;
212
+ const imageY = y - imageHeight / 2 + this.lidOffset * 0.3; // 锅盖偏移效果
213
+
214
+ // 绘制魔法锅图片
215
+ ctx.drawImage(
216
+ this.potImage,
217
+ imageX,
218
+ imageY,
219
+ imageWidth,
220
+ imageHeight
221
+ );
222
+
223
+ ctx.restore();
224
+ }
225
+
226
+ renderGlow(ctx, x, y) {
227
+ const glowRadius = this.width * 0.8 * this.glowIntensity;
228
+ const gradient = ctx.createRadialGradient(x, y, 0, x, y, glowRadius);
229
+
230
+ gradient.addColorStop(0, `rgba(255, 215, 0, ${this.glowIntensity * 0.3})`);
231
+ gradient.addColorStop(0.5, `rgba(255, 105, 180, ${this.glowIntensity * 0.2})`);
232
+ gradient.addColorStop(1, 'rgba(255, 215, 0, 0)');
233
+
234
+ ctx.fillStyle = gradient;
235
+ ctx.beginPath();
236
+ ctx.arc(x, y, glowRadius, 0, Math.PI * 2);
237
+ ctx.fill();
238
+ }
239
+
240
+ renderPotBody(ctx, x, y) {
241
+ const potWidth = this.width;
242
+ const potHeight = this.height;
243
+
244
+ // 锅身主体
245
+ ctx.fillStyle = this.colors.pot;
246
+ ctx.beginPath();
247
+ ctx.ellipse(x, y + potHeight * 0.3, potWidth * 0.5, potHeight * 0.4, 0, 0, Math.PI * 2);
248
+ ctx.fill();
249
+
250
+ // 锅身侧面
251
+ ctx.fillStyle = this.colors.potShadow;
252
+ ctx.fillRect(x - potWidth * 0.5, y - potHeight * 0.2, potWidth, potHeight * 0.5);
253
+
254
+ // 锅身高光
255
+ ctx.fillStyle = this.colors.potHighlight;
256
+ ctx.beginPath();
257
+ ctx.ellipse(x - potWidth * 0.2, y - potHeight * 0.1, potWidth * 0.15, potHeight * 0.1, 0, 0, Math.PI * 2);
258
+ ctx.fill();
259
+
260
+ // 锅把手
261
+ this.renderHandles(ctx, x, y);
262
+ }
263
+
264
+ renderHandles(ctx, x, y) {
265
+ ctx.strokeStyle = this.colors.potShadow;
266
+ ctx.lineWidth = 8;
267
+ ctx.lineCap = 'round';
268
+
269
+ // 左把手
270
+ ctx.beginPath();
271
+ ctx.arc(x - this.width * 0.6, y, this.width * 0.15, -Math.PI * 0.3, Math.PI * 0.3);
272
+ ctx.stroke();
273
+
274
+ // 右把手
275
+ ctx.beginPath();
276
+ ctx.arc(x + this.width * 0.6, y, this.width * 0.15, Math.PI * 0.7, Math.PI * 1.3);
277
+ ctx.stroke();
278
+ }
279
+
280
+ renderPotLid(ctx, x, y) {
281
+ const lidWidth = this.width * 0.9;
282
+ const lidHeight = this.height * 0.2;
283
+
284
+ // 锅盖主体
285
+ ctx.fillStyle = this.colors.lid;
286
+ ctx.beginPath();
287
+ ctx.ellipse(x, y - this.height * 0.3, lidWidth * 0.5, lidHeight * 0.5, 0, 0, Math.PI * 2);
288
+ ctx.fill();
289
+
290
+ // 锅盖高光
291
+ ctx.fillStyle = this.colors.lidHighlight;
292
+ ctx.beginPath();
293
+ ctx.ellipse(x - lidWidth * 0.1, y - this.height * 0.35, lidWidth * 0.3, lidHeight * 0.3, 0, 0, Math.PI * 2);
294
+ ctx.fill();
295
+
296
+ // 锅盖把手
297
+ ctx.fillStyle = this.colors.potShadow;
298
+ ctx.beginPath();
299
+ ctx.ellipse(x, y - this.height * 0.45, 8, 12, 0, 0, Math.PI * 2);
300
+ ctx.fill();
301
+
302
+ ctx.fillStyle = this.colors.lidHighlight;
303
+ ctx.beginPath();
304
+ ctx.ellipse(x - 2, y - this.height * 0.48, 4, 6, 0, 0, Math.PI * 2);
305
+ ctx.fill();
306
+ }
307
+
308
+ renderParticles(ctx) {
309
+ this.steamParticles.forEach(particle => {
310
+ ctx.save();
311
+ ctx.globalAlpha = particle.opacity;
312
+
313
+ if (particle.color) {
314
+ // 魔法粒子
315
+ ctx.fillStyle = particle.color;
316
+ ctx.shadowBlur = 10;
317
+ ctx.shadowColor = particle.color;
318
+ } else {
319
+ // 蒸汽粒子
320
+ ctx.fillStyle = this.colors.steam;
321
+ }
322
+
323
+ ctx.beginPath();
324
+ ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
325
+ ctx.fill();
326
+
327
+ ctx.restore();
328
+ });
329
+ }
330
+
331
+ renderDecorations(ctx, x, y) {
332
+ // 绘制魔法符文(如果激活)
333
+ if (this.state !== 'idle') {
334
+ this.renderMagicRunes(ctx, x, y);
335
+ }
336
+
337
+ // 绘制食物图标(如果正在烹饪)
338
+ if (this.state === 'cooking' && this.currentFood) {
339
+ this.renderFoodIcon(ctx, x, y);
340
+ }
341
+ }
342
+
343
+ renderMagicRunes(ctx, x, y) {
344
+ const runeRadius = this.width * 0.8;
345
+ const runeCount = 6;
346
+ const time = this.animationTime * 2;
347
+
348
+ ctx.strokeStyle = this.colors.magic;
349
+ ctx.lineWidth = 2;
350
+ ctx.globalAlpha = this.glowIntensity * 0.5;
351
+
352
+ for (let i = 0; i < runeCount; i++) {
353
+ const angle = (i / runeCount) * Math.PI * 2 + time;
354
+ const runeX = x + Math.cos(angle) * runeRadius;
355
+ const runeY = y + Math.sin(angle) * runeRadius * 0.5;
356
+
357
+ // 绘制简单的符文形状
358
+ ctx.beginPath();
359
+ ctx.moveTo(runeX - 5, runeY - 5);
360
+ ctx.lineTo(runeX + 5, runeY + 5);
361
+ ctx.moveTo(runeX + 5, runeY - 5);
362
+ ctx.lineTo(runeX - 5, runeY + 5);
363
+ ctx.stroke();
364
+ }
365
+
366
+ ctx.globalAlpha = 1;
367
+ }
368
+
369
+ renderFoodIcon(ctx, x, y) {
370
+ // 在锅上方显示食物图标
371
+ const iconY = y - this.height * 0.8;
372
+
373
+ ctx.fillStyle = '#FFD700';
374
+ ctx.font = '24px Arial';
375
+ ctx.textAlign = 'center';
376
+ ctx.fillText(this.getFoodEmoji(this.currentFood), x, iconY);
377
+ }
378
+
379
+ getFoodEmoji(foodName) {
380
+ const emojiMap = {
381
+ 'apple': '🍎',
382
+ 'banana': '🍌',
383
+ 'orange': '🍊',
384
+ 'strawberry': '🍓',
385
+ 'watermelon': '🍉',
386
+ 'grape': '🍇',
387
+ 'pizza': '🍕',
388
+ 'burger': '🍔',
389
+ 'cake': '🍰',
390
+ 'cookie': '🍪',
391
+ 'bread': '🍞',
392
+ 'cheese': '🧀',
393
+ 'fish': '🐟',
394
+ 'chicken': '🍗',
395
+ 'rice': '🍚',
396
+ 'noodles': '🍜'
397
+ };
398
+
399
+ return emojiMap[foodName] || '🍽️';
400
+ }
401
+
402
+ // 状态控制方法
403
+ activate() {
404
+ this.state = 'activated';
405
+ this.animationTime = 0;
406
+ }
407
+
408
+ startCooking(foodName) {
409
+ this.state = 'cooking';
410
+ this.currentFood = foodName;
411
+ this.animationTime = 0;
412
+ }
413
+
414
+ startOverflowing() {
415
+ this.state = 'overflowing';
416
+ this.animationTime = 0;
417
+ }
418
+
419
+ stopOverflowing() {
420
+ this.state = 'activated';
421
+ this.lidOffset = 0;
422
+ this.shakeAmount = 0;
423
+ }
424
+
425
+ reset() {
426
+ this.state = 'idle';
427
+ this.currentFood = null;
428
+ this.animationTime = 0;
429
+ this.glowIntensity = 0;
430
+ this.shakeAmount = 0;
431
+ this.lidOffset = 0;
432
+ this.steamParticles = [];
433
+ }
434
+
435
+ setFoodAssets(foodImage, animalImage) {
436
+ this.foodImage = foodImage;
437
+ this.animalImage = animalImage;
438
+ }
439
+
440
+ // 碰撞检测
441
+ contains(x, y) {
442
+ const dx = x - this.x;
443
+ const dy = y - this.y;
444
+ const distance = Math.sqrt(dx * dx + dy * dy);
445
+ return distance < this.width * 0.5;
446
+ }
447
+
448
+ // 获取锅的边界
449
+ getBounds() {
450
+ return {
451
+ left: this.x - this.width * 0.5,
452
+ right: this.x + this.width * 0.5,
453
+ top: this.y - this.height * 0.5,
454
+ bottom: this.y + this.height * 0.5
455
+ };
456
+ }
457
+
458
+ // 加载魔法锅图片
459
+ loadPotImage() {
460
+ this.potImage = new Image();
461
+ this.potImage.onload = () => {
462
+ this.imageLoaded = true;
463
+ console.log('魔法锅图片加载成功');
464
+ };
465
+ this.potImage.onerror = () => {
466
+ console.warn('魔法锅图片加载失败,使用默认绘制');
467
+ this.imageLoaded = false;
468
+ };
469
+ this.potImage.src = 'assets/images/magic_pot.png';
470
+ }
471
+ }
js/textToImage.js ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 文生图API接口类
2
+ class TextToImageAPI {
3
+ constructor() {
4
+ this.apiEndpoint = 'https://api.openai.com/v1/images/generations'; // 示例API
5
+ this.apiKey = null; // 需要用户配置
6
+ this.cache = new Map(); // 图片缓存
7
+ this.isEnabled = false;
8
+
9
+ // 备用图片数据(Base64编码的小图标)
10
+ this.fallbackImages = this.initFallbackImages();
11
+
12
+ this.init();
13
+ }
14
+
15
+ init() {
16
+ // 检查是否有API密钥配置
17
+ this.apiKey = localStorage.getItem('textToImageApiKey');
18
+ this.isEnabled = !!this.apiKey;
19
+
20
+ if (!this.isEnabled) {
21
+ console.log('文生图API未配置,将使用备用图片');
22
+ }
23
+ }
24
+
25
+ async generateFood(foodName) {
26
+ try {
27
+ // 首先检查缓存
28
+ const cacheKey = `food_${foodName}`;
29
+ if (this.cache.has(cacheKey)) {
30
+ return this.cache.get(cacheKey);
31
+ }
32
+
33
+ let imageData;
34
+
35
+ if (this.isEnabled) {
36
+ // 使用真实API生成图片
37
+ imageData = await this.callAPI(`cartoon style ${foodName} food, cute, colorful, simple background`);
38
+ } else {
39
+ // 使用备用图片
40
+ imageData = this.getFallbackFoodImage(foodName);
41
+ }
42
+
43
+ // 缓存结果
44
+ this.cache.set(cacheKey, imageData);
45
+ return imageData;
46
+
47
+ } catch (error) {
48
+ console.error('生成食物图片失败:', error);
49
+ return this.getFallbackFoodImage(foodName);
50
+ }
51
+ }
52
+
53
+ async generateAnimal(foodName) {
54
+ try {
55
+ const cacheKey = `animal_${foodName}`;
56
+ if (this.cache.has(cacheKey)) {
57
+ return this.cache.get(cacheKey);
58
+ }
59
+
60
+ const animalType = this.getAnimalForFood(foodName);
61
+ let imageData;
62
+
63
+ if (this.isEnabled) {
64
+ imageData = await this.callAPI(`cartoon style cute ${animalType} animal, friendly, colorful, simple background`);
65
+ } else {
66
+ imageData = this.getFallbackAnimalImage(animalType);
67
+ }
68
+
69
+ this.cache.set(cacheKey, imageData);
70
+ return imageData;
71
+
72
+ } catch (error) {
73
+ console.error('生成动物图片失败:', error);
74
+ return this.getFallbackAnimalImage(this.getAnimalForFood(foodName));
75
+ }
76
+ }
77
+
78
+ async callAPI(prompt) {
79
+ if (!this.isEnabled) {
80
+ throw new Error('API未启用');
81
+ }
82
+
83
+ const response = await fetch(this.apiEndpoint, {
84
+ method: 'POST',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ 'Authorization': `Bearer ${this.apiKey}`
88
+ },
89
+ body: JSON.stringify({
90
+ prompt: prompt,
91
+ n: 1,
92
+ size: '256x256',
93
+ style: 'cartoon'
94
+ })
95
+ });
96
+
97
+ if (!response.ok) {
98
+ throw new Error(`API请求失败: ${response.status}`);
99
+ }
100
+
101
+ const data = await response.json();
102
+
103
+ if (data.data && data.data.length > 0) {
104
+ // 下载图片并转换为本地数据
105
+ return await this.downloadImage(data.data[0].url);
106
+ } else {
107
+ throw new Error('API返回数据格式错误');
108
+ }
109
+ }
110
+
111
+ async downloadImage(url) {
112
+ const response = await fetch(url);
113
+ const blob = await response.blob();
114
+
115
+ return new Promise((resolve, reject) => {
116
+ const reader = new FileReader();
117
+ reader.onload = () => resolve(reader.result);
118
+ reader.onerror = reject;
119
+ reader.readAsDataURL(blob);
120
+ });
121
+ }
122
+
123
+ getFallbackFoodImage(foodName) {
124
+ // 返回备用的食物图片数据
125
+ const fallbackFood = this.fallbackImages.food[foodName] || this.fallbackImages.food.default;
126
+ return this.createSimpleImage(fallbackFood.emoji, fallbackFood.color);
127
+ }
128
+
129
+ getFallbackAnimalImage(animalType) {
130
+ // 返回备用的动物图片数据
131
+ const fallbackAnimal = this.fallbackImages.animals[animalType] || this.fallbackImages.animals.default;
132
+ return this.createSimpleImage(fallbackAnimal.emoji, fallbackAnimal.color);
133
+ }
134
+
135
+ createSimpleImage(emoji, backgroundColor = '#FFFFFF') {
136
+ // 创建一个简单的Canvas图片
137
+ const canvas = document.createElement('canvas');
138
+ const ctx = canvas.getContext('2d');
139
+
140
+ canvas.width = 64;
141
+ canvas.height = 64;
142
+
143
+ // 背景
144
+ ctx.fillStyle = backgroundColor;
145
+ ctx.fillRect(0, 0, 64, 64);
146
+
147
+ // 表情符号
148
+ ctx.font = '40px Arial';
149
+ ctx.textAlign = 'center';
150
+ ctx.textBaseline = 'middle';
151
+ ctx.fillText(emoji, 32, 32);
152
+
153
+ return canvas.toDataURL();
154
+ }
155
+
156
+ initFallbackImages() {
157
+ return {
158
+ food: {
159
+ 'apple': { emoji: '🍎', color: '#FFE4E1' },
160
+ 'banana': { emoji: '🍌', color: '#FFFACD' },
161
+ 'orange': { emoji: '🍊', color: '#FFE4B5' },
162
+ 'strawberry': { emoji: '🍓', color: '#FFB6C1' },
163
+ 'watermelon': { emoji: '🍉', color: '#F0FFF0' },
164
+ 'grape': { emoji: '🍇', color: '#E6E6FA' },
165
+ 'pizza': { emoji: '🍕', color: '#FFEFD5' },
166
+ 'burger': { emoji: '🍔', color: '#F5DEB3' },
167
+ 'cake': { emoji: '🍰', color: '#FFF8DC' },
168
+ 'cookie': { emoji: '🍪', color: '#DEB887' },
169
+ 'bread': { emoji: '🍞', color: '#F5DEB3' },
170
+ 'cheese': { emoji: '🧀', color: '#FFFACD' },
171
+ 'fish': { emoji: '🐟', color: '#E0FFFF' },
172
+ 'chicken': { emoji: '🍗', color: '#FFEFD5' },
173
+ 'rice': { emoji: '🍚', color: '#F8F8FF' },
174
+ 'noodles': { emoji: '🍜', color: '#FFF8DC' },
175
+ 'default': { emoji: '🍽️', color: '#F0F8FF' }
176
+ },
177
+ animals: {
178
+ 'rabbit': { emoji: '🐰', color: '#F5F5DC' },
179
+ 'monkey': { emoji: '🐵', color: '#DEB887' },
180
+ 'bird': { emoji: '🐦', color: '#E0F6FF' },
181
+ 'bear': { emoji: '🐻', color: '#D2B48C' },
182
+ 'elephant': { emoji: '🐘', color: '#DCDCDC' },
183
+ 'fox': { emoji: '🦊', color: '#FFE4B5' },
184
+ 'cat': { emoji: '🐱', color: '#FFB6C1' },
185
+ 'dog': { emoji: '🐶', color: '#DEB887' },
186
+ 'mouse': { emoji: '🐭', color: '#D3D3D3' },
187
+ 'squirrel': { emoji: '🐿️', color: '#D2691E' },
188
+ 'duck': { emoji: '🦆', color: '#FFFACD' },
189
+ 'rat': { emoji: '🐀', color: '#A9A9A9' },
190
+ 'panda': { emoji: '🐼', color: '#F0F8FF' },
191
+ 'chicken': { emoji: '🐔', color: '#FFFACD' },
192
+ 'default': { emoji: '🐱', color: '#FFB6C1' }
193
+ }
194
+ };
195
+ }
196
+
197
+ getAnimalForFood(foodName) {
198
+ const animalMap = {
199
+ 'apple': 'rabbit',
200
+ 'banana': 'monkey',
201
+ 'orange': 'bird',
202
+ 'strawberry': 'bear',
203
+ 'watermelon': 'elephant',
204
+ 'grape': 'fox',
205
+ 'pizza': 'cat',
206
+ 'burger': 'dog',
207
+ 'cake': 'mouse',
208
+ 'cookie': 'squirrel',
209
+ 'bread': 'duck',
210
+ 'cheese': 'rat',
211
+ 'fish': 'cat',
212
+ 'chicken': 'dog',
213
+ 'rice': 'bird',
214
+ 'noodles': 'panda',
215
+ 'carrot': 'rabbit',
216
+ 'corn': 'chicken'
217
+ };
218
+
219
+ return animalMap[foodName] || 'cat';
220
+ }
221
+
222
+ // 配置API密钥
223
+ setApiKey(apiKey) {
224
+ this.apiKey = apiKey;
225
+ this.isEnabled = !!apiKey;
226
+
227
+ if (apiKey) {
228
+ localStorage.setItem('textToImageApiKey', apiKey);
229
+ } else {
230
+ localStorage.removeItem('textToImageApiKey');
231
+ }
232
+ }
233
+
234
+ // 清除缓存
235
+ clearCache() {
236
+ this.cache.clear();
237
+ }
238
+
239
+ // 获取缓存大小
240
+ getCacheSize() {
241
+ return this.cache.size;
242
+ }
243
+
244
+ // 预加载常用图片
245
+ async preloadCommonImages() {
246
+ const commonFoods = ['apple', 'banana', 'orange', 'strawberry', 'watermelon'];
247
+ const promises = [];
248
+
249
+ for (const food of commonFoods) {
250
+ promises.push(this.generateFood(food));
251
+ promises.push(this.generateAnimal(food));
252
+ }
253
+
254
+ try {
255
+ await Promise.all(promises);
256
+ console.log('常用图片预加载完成');
257
+ } catch (error) {
258
+ console.error('预加载图片失败:', error);
259
+ }
260
+ }
261
+
262
+ // 创建配置UI
263
+ createConfigUI() {
264
+ const configDiv = document.createElement('div');
265
+ configDiv.id = 'textToImageConfig';
266
+ configDiv.style.cssText = `
267
+ position: fixed;
268
+ top: 50%;
269
+ left: 50%;
270
+ transform: translate(-50%, -50%);
271
+ background: white;
272
+ padding: 20px;
273
+ border-radius: 10px;
274
+ box-shadow: 0 4px 20px rgba(0,0,0,0.3);
275
+ z-index: 10000;
276
+ display: none;
277
+ `;
278
+
279
+ configDiv.innerHTML = `
280
+ <h3>文生图API配置</h3>
281
+ <p>输入您的API密钥以启用AI图片生成功能:</p>
282
+ <input type="text" id="apiKeyInput" placeholder="输入API密钥" style="width: 300px; padding: 8px; margin: 10px 0;">
283
+ <br>
284
+ <button id="saveApiKey" style="padding: 8px 16px; margin-right: 10px;">保存</button>
285
+ <button id="cancelConfig" style="padding: 8px 16px;">取消</button>
286
+ <p style="font-size: 12px; color: #666; margin-top: 10px;">
287
+ 如果不配置API密钥,将使用备用的表情符号图片
288
+ </p>
289
+ `;
290
+
291
+ document.body.appendChild(configDiv);
292
+
293
+ // 事件监听
294
+ document.getElementById('saveApiKey').onclick = () => {
295
+ const apiKey = document.getElementById('apiKeyInput').value.trim();
296
+ this.setApiKey(apiKey);
297
+ configDiv.style.display = 'none';
298
+
299
+ if (apiKey) {
300
+ alert('API密钥已保存,现在将使用AI生成图片');
301
+ } else {
302
+ alert('API密钥已清除,将使用备用图片');
303
+ }
304
+ };
305
+
306
+ document.getElementById('cancelConfig').onclick = () => {
307
+ configDiv.style.display = 'none';
308
+ };
309
+
310
+ return configDiv;
311
+ }
312
+
313
+ // 显示配置界面
314
+ showConfig() {
315
+ let configDiv = document.getElementById('textToImageConfig');
316
+ if (!configDiv) {
317
+ configDiv = this.createConfigUI();
318
+ }
319
+
320
+ document.getElementById('apiKeyInput').value = this.apiKey || '';
321
+ configDiv.style.display = 'block';
322
+ }
323
+
324
+ // 测试API连接
325
+ async testAPI() {
326
+ if (!this.isEnabled) {
327
+ return { success: false, message: 'API未配置' };
328
+ }
329
+
330
+ try {
331
+ await this.callAPI('test image');
332
+ return { success: true, message: 'API连接正常' };
333
+ } catch (error) {
334
+ return { success: false, message: error.message };
335
+ }
336
+ }
337
+ }
338
+
339
+ // 添加全局快捷键来配置API
340
+ document.addEventListener('keydown', (e) => {
341
+ if (e.ctrlKey && e.shiftKey && e.key === 'I') {
342
+ if (window.game && window.game.textToImage) {
343
+ window.game.textToImage.showConfig();
344
+ }
345
+ }
346
+ });
js/voice.js ADDED
@@ -0,0 +1,404 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // 语音识别类
2
+ class VoiceRecognition {
3
+ constructor() {
4
+ this.recognition = null;
5
+ this.isListening = false;
6
+ this.isSupported = false;
7
+ this.onResult = null;
8
+ this.onError = null;
9
+ this.onStatusChange = null;
10
+
11
+ this.init();
12
+ }
13
+
14
+ init() {
15
+ // 检查浏览器支持
16
+ if ('webkitSpeechRecognition' in window) {
17
+ this.recognition = new webkitSpeechRecognition();
18
+ this.isSupported = true;
19
+ } else if ('SpeechRecognition' in window) {
20
+ this.recognition = new SpeechRecognition();
21
+ this.isSupported = true;
22
+ } else {
23
+ console.warn('浏览器不支持语音识别');
24
+ this.handleError('浏览器不支持语音识别');
25
+ return;
26
+ }
27
+
28
+ this.setupRecognition();
29
+ this.startListening();
30
+ }
31
+
32
+ setupRecognition() {
33
+ // 配置语音识别
34
+ this.recognition.continuous = true;
35
+ this.recognition.interimResults = false;
36
+ this.recognition.lang = 'en-US'; // 使用英文识别食物名称
37
+ this.recognition.maxAlternatives = 3;
38
+
39
+ // 事件监听
40
+ this.recognition.onstart = () => {
41
+ this.isListening = true;
42
+ this.updateStatus('listening');
43
+ console.log('语音识别开始');
44
+ };
45
+
46
+ this.recognition.onresult = (event) => {
47
+ this.handleResult(event);
48
+ };
49
+
50
+ this.recognition.onerror = (event) => {
51
+ this.handleError(event.error);
52
+ };
53
+
54
+ this.recognition.onend = () => {
55
+ this.isListening = false;
56
+ this.updateStatus('ready');
57
+ console.log('语音识别结束');
58
+
59
+ // 更谨慎的自动重启 - 参考腾讯云开发者社区建议
60
+ setTimeout(() => {
61
+ if (this.isSupported && !this.isListening) {
62
+ console.log('准备重新启动语音识别...');
63
+ this.startListening();
64
+ }
65
+ }, 1500); // 增加延迟时间
66
+ };
67
+ }
68
+
69
+ handleResult(event) {
70
+ this.updateStatus('processing');
71
+
72
+ let finalTranscript = '';
73
+ let interimTranscript = '';
74
+ let confidence = 0;
75
+
76
+ // 处理识别结果
77
+ for (let i = event.resultIndex; i < event.results.length; i++) {
78
+ const transcript = event.results[i][0].transcript;
79
+ confidence = event.results[i][0].confidence || 0;
80
+
81
+ if (event.results[i].isFinal) {
82
+ finalTranscript += transcript;
83
+ } else {
84
+ interimTranscript += transcript;
85
+ }
86
+ }
87
+
88
+ // 显示实时语音输入
89
+ const currentText = finalTranscript || interimTranscript;
90
+ if (currentText) {
91
+ this.showVoiceInput(currentText, confidence, !finalTranscript);
92
+ }
93
+
94
+ // 如果有最终结果,触发回调
95
+ if (finalTranscript && this.onResult) {
96
+ console.log('语音识别结果:', finalTranscript);
97
+ this.onResult(finalTranscript.trim());
98
+
99
+ // 短暂显示识别成功状态
100
+ setTimeout(() => {
101
+ this.hideVoiceInput();
102
+ }, 2000);
103
+ }
104
+
105
+ this.updateStatus('ready');
106
+ }
107
+
108
+ handleError(error) {
109
+ console.error('语音识别错误:', error);
110
+ this.updateStatus('error');
111
+
112
+ if (this.onError) {
113
+ this.onError(error);
114
+ }
115
+
116
+ // 根据错误类型处理
117
+ switch (error) {
118
+ case 'not-allowed':
119
+ this.showPermissionError();
120
+ break;
121
+ case 'no-speech':
122
+ // 没有检测到语音,继续监听
123
+ setTimeout(() => {
124
+ if (this.isSupported) {
125
+ this.startListening();
126
+ }
127
+ }, 1000);
128
+ break;
129
+ case 'network':
130
+ this.showNetworkError();
131
+ break;
132
+ default:
133
+ // 其他错误,尝试重新开始
134
+ setTimeout(() => {
135
+ if (this.isSupported) {
136
+ this.startListening();
137
+ }
138
+ }, 2000);
139
+ break;
140
+ }
141
+ }
142
+
143
+ startListening() {
144
+ if (!this.isSupported) {
145
+ this.showUnsupportedError();
146
+ return;
147
+ }
148
+
149
+ // 增强的状态检查 - 参考腾讯云开发者社区解决方案
150
+ if (this.isListening) {
151
+ console.log('语音识别已在运行中,跳过启动');
152
+ return;
153
+ }
154
+
155
+ try {
156
+ // 确保之前的识别已完全停止
157
+ if (this.recognition) {
158
+ this.recognition.abort(); // 强制停止之前的识别
159
+ }
160
+
161
+ // 短暂延迟后启动新的识别
162
+ setTimeout(() => {
163
+ try {
164
+ this.recognition.start();
165
+ } catch (error) {
166
+ if (error.message.includes('recognition has already started')) {
167
+ console.log('识别已启动,尝试停止后重新启动');
168
+ this.recognition.stop();
169
+ setTimeout(() => {
170
+ try {
171
+ this.recognition.start();
172
+ } catch (retryError) {
173
+ console.error('重试启动失败:', retryError);
174
+ this.handleError('start-failed');
175
+ }
176
+ }, 500);
177
+ } else {
178
+ throw error;
179
+ }
180
+ }
181
+ }, 100);
182
+
183
+ } catch (error) {
184
+ console.error('启动语音识别失败:', error);
185
+ this.handleError('start-failed');
186
+ }
187
+ }
188
+
189
+ stopListening() {
190
+ if (this.recognition && this.isListening) {
191
+ this.recognition.stop();
192
+ }
193
+ }
194
+
195
+ // 重启语音识别 - 参考腾讯云开发者社区解决方案
196
+ restart() {
197
+ console.log('重启语音识别系统...');
198
+
199
+ // 强制停止当前识别
200
+ if (this.recognition) {
201
+ this.isListening = false;
202
+ try {
203
+ this.recognition.abort();
204
+ } catch (error) {
205
+ console.log('停止识别时出错:', error);
206
+ }
207
+ }
208
+
209
+ // 等待一段时间后重新启动
210
+ setTimeout(() => {
211
+ this.startListening();
212
+ }, 1000);
213
+ }
214
+
215
+ updateStatus(status) {
216
+ if (this.onStatusChange) {
217
+ this.onStatusChange(status);
218
+ }
219
+ }
220
+
221
+ showVoiceInput(text, confidence = 0, isInterim = false) {
222
+ const voiceDisplay = document.getElementById('voiceInputDisplay');
223
+ const speechText = document.getElementById('speechText');
224
+ const speechConfidence = document.getElementById('speechConfidence');
225
+ const speechBubble = voiceDisplay.querySelector('.speech-bubble');
226
+
227
+ if (!voiceDisplay || !speechText) return;
228
+
229
+ // 显示语音输入框
230
+ voiceDisplay.style.display = 'block';
231
+
232
+ // 更新文本内容
233
+ speechText.textContent = text;
234
+
235
+ // 显示置信度
236
+ if (confidence > 0) {
237
+ speechConfidence.textContent = `置信度: ${Math.round(confidence * 100)}%`;
238
+ } else {
239
+ speechConfidence.textContent = isInterim ? '正在识别...' : '';
240
+ }
241
+
242
+ // 更新样式状态
243
+ speechBubble.className = 'speech-bubble';
244
+ if (isInterim) {
245
+ speechBubble.classList.add('listening');
246
+ } else {
247
+ speechBubble.classList.add('recognized');
248
+ }
249
+
250
+ // 清除之前的隐藏定时器
251
+ if (this.hideTimer) {
252
+ clearTimeout(this.hideTimer);
253
+ }
254
+
255
+ // 如果是临时结果,设置自动隐藏
256
+ if (isInterim) {
257
+ this.hideTimer = setTimeout(() => {
258
+ this.hideVoiceInput();
259
+ }, 3000);
260
+ }
261
+ }
262
+
263
+ hideVoiceInput() {
264
+ const voiceDisplay = document.getElementById('voiceInputDisplay');
265
+ if (voiceDisplay) {
266
+ voiceDisplay.style.display = 'none';
267
+ }
268
+
269
+ if (this.hideTimer) {
270
+ clearTimeout(this.hideTimer);
271
+ this.hideTimer = null;
272
+ }
273
+ }
274
+
275
+ showPermissionError() {
276
+ this.showNotification('需要麦克风权限才能使用语音功能', 'error');
277
+ }
278
+
279
+ showNetworkError() {
280
+ this.showNotification('网络连接错误,语音识别暂时不可用', 'error');
281
+ }
282
+
283
+ showUnsupportedError() {
284
+ this.showNotification('您的浏览器不支持语音识别功能', 'error');
285
+ }
286
+
287
+ showNotification(message, type = 'info') {
288
+ // 创建通知元素
289
+ const notification = document.createElement('div');
290
+ notification.className = `voice-notification ${type}`;
291
+ notification.textContent = message;
292
+
293
+ // 样式
294
+ Object.assign(notification.style, {
295
+ position: 'fixed',
296
+ top: '20px',
297
+ right: '20px',
298
+ background: type === 'error' ? '#e74c3c' : '#4ecdc4',
299
+ color: 'white',
300
+ padding: '15px 20px',
301
+ borderRadius: '10px',
302
+ boxShadow: '0 4px 15px rgba(0,0,0,0.2)',
303
+ zIndex: '10000',
304
+ fontSize: '14px',
305
+ maxWidth: '300px',
306
+ animation: 'slideInRight 0.3s ease'
307
+ });
308
+
309
+ document.body.appendChild(notification);
310
+
311
+ // 自动移除
312
+ setTimeout(() => {
313
+ notification.style.animation = 'slideOutRight 0.3s ease';
314
+ setTimeout(() => {
315
+ if (notification.parentNode) {
316
+ notification.parentNode.removeChild(notification);
317
+ }
318
+ }, 300);
319
+ }, 5000);
320
+ }
321
+
322
+ // 测试语音识别功能
323
+ test() {
324
+ if (!this.isSupported) {
325
+ console.log('语音识别不支持');
326
+ return false;
327
+ }
328
+
329
+ console.log('语音识别测试开始...');
330
+
331
+ // 设置测试回调
332
+ const originalOnResult = this.onResult;
333
+ this.onResult = (transcript) => {
334
+ console.log('测试结果:', transcript);
335
+ this.onResult = originalOnResult;
336
+ };
337
+
338
+ return true;
339
+ }
340
+
341
+ // 获取支持的语言列表
342
+ getSupportedLanguages() {
343
+ return [
344
+ { code: 'en-US', name: 'English (US)' },
345
+ { code: 'zh-CN', name: '中文 (普通话)' },
346
+ { code: 'ja-JP', name: '日本語' },
347
+ { code: 'ko-KR', name: '한국어' },
348
+ { code: 'fr-FR', name: 'Français' },
349
+ { code: 'de-DE', name: 'Deutsch' },
350
+ { code: 'es-ES', name: 'Español' },
351
+ { code: 'it-IT', name: 'Italiano' }
352
+ ];
353
+ }
354
+
355
+ // 切换语言
356
+ setLanguage(langCode) {
357
+ if (this.recognition) {
358
+ this.recognition.lang = langCode;
359
+ console.log(`语音识别语言切换为: ${langCode}`);
360
+ }
361
+ }
362
+
363
+ // 获取当前状态
364
+ getStatus() {
365
+ return {
366
+ isSupported: this.isSupported,
367
+ isListening: this.isListening,
368
+ currentLanguage: this.recognition ? this.recognition.lang : null
369
+ };
370
+ }
371
+ }
372
+
373
+ // 添加CSS动画
374
+ const style = document.createElement('style');
375
+ style.textContent = `
376
+ @keyframes slideInRight {
377
+ from {
378
+ transform: translateX(100%);
379
+ opacity: 0;
380
+ }
381
+ to {
382
+ transform: translateX(0);
383
+ opacity: 1;
384
+ }
385
+ }
386
+
387
+ @keyframes slideOutRight {
388
+ from {
389
+ transform: translateX(0);
390
+ opacity: 1;
391
+ }
392
+ to {
393
+ transform: translateX(100%);
394
+ opacity: 0;
395
+ }
396
+ }
397
+
398
+ .voice-notification {
399
+ font-family: 'Comic Neue', cursive;
400
+ font-weight: bold;
401
+ word-wrap: break-word;
402
+ }
403
+ `;
404
+ document.head.appendChild(style);
nginx.conf ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ server {
2
+ listen 80;
3
+ server_name localhost;
4
+
5
+ # 设置根目录
6
+ root /usr/share/nginx/html;
7
+ index index.html;
8
+
9
+ # 设置字符编码
10
+ charset utf-8;
11
+
12
+ # 主页面
13
+ location / {
14
+ try_files $uri $uri/ /index.html;
15
+ }
16
+
17
+ # 静态资源缓存设置
18
+ location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|mp3|wav|ogg)$ {
19
+ expires 1y;
20
+ add_header Cache-Control "public, immutable";
21
+ add_header Access-Control-Allow-Origin "*";
22
+ }
23
+
24
+ # HTML文件缓存设置
25
+ location ~* \.html$ {
26
+ expires 1h;
27
+ add_header Cache-Control "public";
28
+ }
29
+
30
+ # 启用gzip压缩
31
+ gzip on;
32
+ gzip_vary on;
33
+ gzip_min_length 1024;
34
+ gzip_proxied any;
35
+ gzip_comp_level 6;
36
+ gzip_types
37
+ application/atom+xml
38
+ application/geo+json
39
+ application/javascript
40
+ application/x-javascript
41
+ application/json
42
+ application/ld+json
43
+ application/manifest+json
44
+ application/rdf+xml
45
+ application/rss+xml
46
+ application/xhtml+xml
47
+ application/xml
48
+ font/eot
49
+ font/otf
50
+ font/ttf
51
+ image/svg+xml
52
+ text/css
53
+ text/javascript
54
+ text/plain
55
+ text/xml;
56
+
57
+ # 安全头设置
58
+ add_header X-Frame-Options "SAMEORIGIN" always;
59
+ add_header X-Content-Type-Options "nosniff" always;
60
+ add_header X-XSS-Protection "1; mode=block" always;
61
+ add_header Referrer-Policy "no-referrer-when-downgrade" always;
62
+
63
+ # 错误页面
64
+ error_page 404 /index.html;
65
+
66
+ # 日志
67
+ access_log /var/log/nginx/access.log;
68
+ error_log /var/log/nginx/error.log;
69
+ }
package-lock.json ADDED
The diff for this file is too large to render. See raw diff
 
package.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "magic-pot-game",
3
+ "version": "1.0.0",
4
+ "description": "一个卡通手绘风格的语音交互魔法锅游戏",
5
+ "main": "index.html",
6
+ "scripts": {
7
+ "start": "python -m http.server 8000",
8
+ "serve": "npx http-server -p 8000 -o",
9
+ "dev": "npx live-server --port=8000 --open=/index.html"
10
+ },
11
+ "keywords": [
12
+ "game",
13
+ "voice-recognition",
14
+ "canvas",
15
+ "javascript",
16
+ "cartoon",
17
+ "magic-pot"
18
+ ],
19
+ "author": "MagicPot Game Developer",
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "http-server": "^14.1.1",
23
+ "live-server": "^1.2.2"
24
+ },
25
+ "engines": {
26
+ "node": ">=14.0.0"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/your-username/magic-pot-game.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/your-username/magic-pot-game/issues"
34
+ },
35
+ "homepage": "https://github.com/your-username/magic-pot-game#readme"
36
+ }