Skip to content

Commit 5f6e137

Browse files
committed
feat: 引入调试助手,优化日志输出
- 在 `AudioManager`、`LoadManager`、`GameScene`、`MainMenuScene` 和 `PreloadScene` 中,替换原有的 `console.log` 调用为 `DebugHelper.debugLog` 方法,增强日志的可读性和可维护性。 - 新增 `DebugHelper` 类,提供统一的调试日志输出功能,仅在调试模式下显示,支持分类和附加数据的记录。 - 通过改进日志记录方式,提升了代码的整洁性和调试效率。
1 parent 8ae1651 commit 5f6e137

6 files changed

Lines changed: 51 additions & 26 deletions

File tree

src/game/managers/AudioManager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Phaser from 'phaser'
22
import { on } from '@/game/managers/EventBus'
33
import { LoadManager } from '@/game/managers/LoadManager'
4+
import { DebugHelper } from '@/utils/debugHelper'
45

56
type SoundCache = Record<string, Phaser.Sound.BaseSound>
67

@@ -131,17 +132,17 @@ export class AudioManager {
131132
}
132133

133134
this.requestedBgmKey = key
134-
console.log(`🎵 场景声明要播放BGM: ${key}`)
135+
DebugHelper.debugLog('BGM', `场景声明要播放BGM: ${key}`)
135136

136137
if (!this._bgmEnabled) {
137-
console.log('🔇 BGM已关闭')
138+
DebugHelper.debugLog('BGM', 'BGM已关闭')
138139
this.clearRequestedBgmKey()
139140
return
140141
}
141142

142143
// 检查是否已加载,使用LoadManager的加载状态
143144
if (LoadManager.isAudioLoaded(key)) {
144-
console.log(`✅ BGM已预加载,立即播放: ${key}`)
145+
DebugHelper.debugLog('BGM', `BGM已预加载,立即播放: ${key}`)
145146
this.tryStartBgm(key)
146147
this.clearRequestedBgmKey()
147148
return
@@ -152,7 +153,7 @@ export class AudioManager {
152153
try {
153154
const existingSound = this.scene.sound.get(key)
154155
if (existingSound) {
155-
console.log(`✅ BGM已在场景中,立即播放: ${key}`)
156+
DebugHelper.debugLog('BGM', `BGM已在场景中,立即播放: ${key}`)
156157
this.tryStartBgm(key)
157158
this.clearRequestedBgmKey()
158159
return
@@ -163,7 +164,7 @@ export class AudioManager {
163164
}
164165

165166
// 如果未加载,等待LoadManager的audio:loaded事件触发播放
166-
console.log(`⏳ BGM尚未加载,等待加载完成: ${key}`)
167+
DebugHelper.debugLog('BGM', `BGM尚未加载,等待加载完成: ${key}`)
167168
}
168169

169170
static playSfx(key: string, config: Phaser.Types.Sound.SoundConfig = { volume: 0.6 }) {

src/game/managers/LoadManager.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Phaser from 'phaser'
22
import { AssetConfig, AUDIO_LOADING_CONFIG } from '@/game/config/assetConfig'
33
import { emit } from '@/game/managers/EventBus'
44
import { AudioManager } from '@/game/managers/AudioManager'
5+
import { DebugHelper } from '@/utils/debugHelper'
56

67
export class LoadManager {
78
private static scene: Phaser.Scene | null = null
@@ -47,20 +48,20 @@ export class LoadManager {
4748

4849
// 如果所有图片都加载完成
4950
if (loadedCount >= totalAssets) {
50-
console.log('✅ 核心图片资源加载完成')
51+
DebugHelper.debugLog('Load', '核心图片资源加载完成')
5152
resolve()
5253
}
5354
})
5455

5556
// 监听所有文件加载完成
5657
this.scene!.load.on('complete', () => {
57-
console.log('✅ 核心图片资源加载完成')
58+
DebugHelper.debugLog('Load', '核心图片资源加载完成')
5859
resolve()
5960
})
6061

6162
// 监听加载错误
6263
this.scene!.load.on('loaderror', (file: any) => {
63-
console.error(`❌ 加载失败: ${file.key}`)
64+
DebugHelper.debugLog('Load', `加载失败: ${file.key}`, { file })
6465
reject(new Error(`Failed to load: ${file.key}`))
6566
})
6667

@@ -132,11 +133,11 @@ export class LoadManager {
132133
tempLoader.once('complete', () => {
133134
clearTimeout(timeoutId)
134135
this.loadedAssets.add(key)
135-
console.log(`✅ 音频加载完成: ${key}`)
136+
DebugHelper.debugLog('Audio', `音频加载完成: ${key}`)
136137

137138
// 检查AudioManager是否有期望播放这个BGM
138139
if (AudioManager.getRequestedBgmKey() === key) {
139-
console.log(`🎵 自动播放请求的BGM: ${key}`)
140+
DebugHelper.debugLog('BGM', `自动播放请求的BGM: ${key}`)
140141
AudioManager.tryStartBgm(key)
141142
AudioManager.clearRequestedBgmKey() // 清除期望
142143
}
@@ -217,9 +218,9 @@ export class LoadManager {
217218
// 在主菜单BGM加载完成后,开始加载游戏BGM
218219
try {
219220
// 直接加载游戏BGM,不重新加载主菜单BGM
220-
console.log('🎵 开始预加载游戏BGM...')
221+
DebugHelper.debugLog('BGM', '开始预加载游戏BGM...')
221222
await this.loadAudioAsync(bgmKeys, AUDIO_LOADING_CONFIG.bgmTimeout)
222-
console.log('✅ 游戏BGM预加载完成')
223+
DebugHelper.debugLog('BGM', '游戏BGM预加载完成')
223224
} catch (error) {
224225
console.warn('⚠️ 游戏BGM预加载失败,将在游戏场景中重试:', error)
225226
}

src/game/scenes/GameScene.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default class GameScene extends Phaser.Scene {
8686
}
8787

8888
// 请求播放游戏界面背景音乐
89-
console.log('🎵 游戏场景请求播放BGM: bgm_game')
89+
DebugHelper.debugLog('BGM', '游戏场景请求播放BGM: bgm_game')
9090
AudioManager.requestBgm('bgm_game')
9191

9292
this.nextQuestion()

src/game/scenes/MainMenuScene.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Strings } from '@/game/managers/Strings'
77
import type { LanguageCode } from '@/game/managers/Strings'
88
import { createTextButton } from '@/game/utils/uiFactory'
99
import { on, off } from '@/game/managers/EventBus'
10+
import { DebugHelper } from '@/utils/debugHelper'
1011

1112
export default class MainMenuScene extends Phaser.Scene {
1213
private bgmStatusText?: Phaser.GameObjects.Text
@@ -31,7 +32,7 @@ export default class MainMenuScene extends Phaser.Scene {
3132
LoadManager.init(this)
3233

3334
// 声明主菜单要播放的BGM
34-
console.log('🎵 主菜单声明要播放BGM: bgm_main')
35+
DebugHelper.debugLog('BGM', '主菜单声明要播放BGM: bgm_main')
3536
AudioManager.requestBgm('bgm_main')
3637

3738
// 开始预加载所有BGM(如果还没有开始)
@@ -318,7 +319,7 @@ export default class MainMenuScene extends Phaser.Scene {
318319
* 开始预加载所有BGM(主菜单BGM + 游戏BGM)
319320
*/
320321
private async startAllBGMPreload() {
321-
console.log('🎵 开始预加载所有BGM...')
322+
DebugHelper.debugLog('BGM', '开始预加载所有BGM...')
322323

323324
try {
324325
// 并行预加载主菜单BGM和游戏BGM
@@ -328,7 +329,7 @@ export default class MainMenuScene extends Phaser.Scene {
328329
]
329330

330331
await Promise.allSettled(promises)
331-
console.log('✅ 所有BGM预加载完成')
332+
DebugHelper.debugLog('BGM', '所有BGM预加载完成')
332333
} catch (error) {
333334
console.warn('⚠️ BGM预加载过程中出现错误:', error)
334335
}
@@ -340,11 +341,11 @@ export default class MainMenuScene extends Phaser.Scene {
340341
private checkAndInitializeBGMStatus(width: number, height: number) {
341342
// 检查主菜单BGM是否已经加载完成
342343
if (LoadManager.isAudioLoaded('bgm_main')) {
343-
console.log('✅ 主菜单BGM已加载,隐藏状态指示器')
344+
DebugHelper.debugLog('BGM', '主菜单BGM已加载,隐藏状态指示器')
344345
// BGM已加载完成,不需要显示状态指示器
345346
// 可以选择隐藏或者显示"已就绪"状态后淡出
346347
} else {
347-
console.log('⏳ 主菜单BGM尚未加载,显示状态指示器')
348+
DebugHelper.debugLog('BGM', '主菜单BGM尚未加载,显示状态指示器')
348349
// BGM还未加载完成,显示状态指示器
349350
this.createBGMStatusIndicator(width, height)
350351
this.updateBGMStatus('loading')
@@ -393,7 +394,7 @@ export default class MainMenuScene extends Phaser.Scene {
393394
private updateBGMStatus(status: 'loading' | 'ready' | 'failed') {
394395
try {
395396
if (!this.bgmStatusText || !this.bgmLoadingIndicator) {
396-
console.log('BGM状态指示器未初始化,跳过状态更新')
397+
DebugHelper.debugLog('BGM', 'BGM状态指示器未初始化,跳过状态更新')
397398
return
398399
}
399400

@@ -419,7 +420,7 @@ export default class MainMenuScene extends Phaser.Scene {
419420
const text = Strings.t(textKey)
420421
this.bgmStatusText.setText(text)
421422
this.bgmStatusText.setColor(color)
422-
console.log(`BGM状态更新: ${text}`)
423+
DebugHelper.debugLog('BGM', `BGM状态更新: ${text}`)
423424

424425
// 加载完成或失败后3秒隐藏指示器
425426
if (status === 'ready' || status === 'failed') {
@@ -448,6 +449,6 @@ export default class MainMenuScene extends Phaser.Scene {
448449
shutdown() {
449450
// 重置BGM加载标志,允许重新进入时重新加载
450451
this.bgmLoadStarted = false
451-
console.log('MainMenuScene shutdown - BGM加载状态已重置')
452+
DebugHelper.debugLog('BGM', 'MainMenuScene shutdown - BGM加载状态已重置')
452453
}
453454
}

src/game/scenes/PreloadScene.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { ToolManager } from '@/game/managers/ToolManager'
55
import { AudioManager } from '@/game/managers/AudioManager'
66
import { LoadManager } from '@/game/managers/LoadManager'
77
import { on } from '@/game/managers/EventBus'
8+
import { DebugHelper } from '@/utils/debugHelper'
89

910
export default class PreloadScene extends Phaser.Scene {
1011
constructor() {
1112
super('PreloadScene')
1213
}
1314

1415
preload() {
15-
console.log('🚀 开始加载核心资源...')
16+
DebugHelper.debugLog('Load', '开始加载核心资源...')
1617

1718
// 加载配置文件
1819
this.load.json('difficulty', 'game/config/digit-difficulty.json')
@@ -41,16 +42,16 @@ export default class PreloadScene extends Phaser.Scene {
4142

4243
// 设置加载事件监听器
4344
this.load.on('complete', () => {
44-
console.log('✅ 核心图片和音效资源加载完成')
45+
DebugHelper.debugLog('Load', '核心图片和音效资源加载完成')
4546
})
4647

4748
this.load.on('loaderror', (file: any) => {
48-
console.error(`❌ 核心资源加载失败: ${file.key}`)
49+
DebugHelper.debugLog('Load', `核心资源加载失败: ${file.key}`, { file })
4950
})
5051
}
5152

5253
create() {
53-
console.log('🎮 初始化游戏系统...')
54+
DebugHelper.debugLog('System', '初始化游戏系统...')
5455

5556
const diff = this.cache.json.get('difficulty')
5657

@@ -94,7 +95,7 @@ export default class PreloadScene extends Phaser.Scene {
9495
// 移除 ui:choice 事件的音效,因为答题音效已经由 ui:feedback 处理
9596
on('tool:use', () => AudioManager.playSfx('sfx_click'))
9697

97-
console.log('✅ 游戏系统初始化完成,进入主菜单')
98+
DebugHelper.debugLog('System', '游戏系统初始化完成,进入主菜单')
9899
this.scene.start('MainMenuScene')
99100
}
100101
}

src/utils/debugHelper.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
import { SaveManager } from '@/game/managers/SaveManager'
33

44
export class DebugHelper {
5+
/**
6+
* 调试日志输出 - 只在debug模式下显示
7+
* @param category 日志分类(如 'BGM', 'Audio', 'Load' 等)
8+
* @param message 日志消息
9+
* @param data 可选的额外数据
10+
*/
11+
public static debugLog(category: string, message: string, data?: any): void {
12+
if (!this.isDebugMode()) {
13+
return
14+
}
15+
16+
const timestamp = new Date().toLocaleTimeString()
17+
const prefix = `[${timestamp}] [${category}]`
18+
19+
if (data !== undefined) {
20+
console.log(`${prefix} ${message}`, data)
21+
} else {
22+
console.log(`${prefix} ${message}`)
23+
}
24+
}
25+
526
/**
627
* 检查是否为调试模式
728
* 统一的调试模式判断逻辑,避免重复实现

0 commit comments

Comments
 (0)