diff --git a/lib/src/features/game/game_play_screen.dart b/lib/src/features/game/game_play_screen.dart index 7a2670f..f763683 100644 --- a/lib/src/features/game/game_play_screen.dart +++ b/lib/src/features/game/game_play_screen.dart @@ -221,6 +221,31 @@ class _GamePlayScreenState extends State _lastProcessedEventCount = events.length; } + /// 초기 BGM 재생 (게임 시작/로드 시) + void _playInitialBgm(GameState state) { + final audio = widget.audioService; + if (audio == null) return; + + final combat = state.progress.currentCombat; + final isInCombat = combat != null && combat.isActive; + + if (isInCombat) { + // 전투 중: 보스 여부에 따라 BGM 선택 + final monsterLevel = state.progress.currentTask.monsterLevel ?? 0; + final playerLevel = state.traits.level; + final isBoss = monsterLevel >= playerLevel + 5; + + if (isBoss) { + audio.playBgm('boss'); + } else { + audio.playBgm('battle'); + } + } else { + // 비전투: 마을 BGM + audio.playBgm('town'); + } + } + /// 전투 상태에 따른 BGM 전환 void _updateBgmForCombatState(GameState state) { final audio = widget.audioService; @@ -450,17 +475,20 @@ class _GamePlayScreenState extends State _lastPlotStageCount = state.progress.plotStageCount; _lastAct = getActForLevel(state.traits.level); - // 초기 전투 상태 확인 + // 초기 전투 상태 확인 및 BGM 설정 final combat = state.progress.currentCombat; _wasInCombat = combat != null && combat.isActive; + + // 초기 BGM 재생 (전투 상태에 따라) + _playInitialBgm(state); + } else { + // 상태가 없으면 기본 마을 BGM + widget.audioService?.playBgm('town'); } // 누적 통계 로드 widget.controller.loadCumulativeStats(); - // 초기 BGM 재생 (마을 테마) - widget.audioService?.playBgm('town'); - // 오디오 볼륨 초기화 _initAudioVolumes(); } @@ -485,15 +513,52 @@ class _GamePlayScreenState extends State } @override - void didChangeAppLifecycleState(AppLifecycleState state) { - super.didChangeAppLifecycleState(state); + void didChangeAppLifecycleState(AppLifecycleState appState) { + super.didChangeAppLifecycleState(appState); - // 앱이 백그라운드로 가거나 비활성화될 때 자동 저장 - if (state == AppLifecycleState.paused || - state == AppLifecycleState.inactive || - state == AppLifecycleState.detached) { + // 모바일 환경 확인 (iOS/Android) + final isMobile = !kIsWeb && + (defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.android); + + // 앱이 백그라운드로 가거나 비활성화될 때 + if (appState == AppLifecycleState.paused || + appState == AppLifecycleState.inactive || + appState == AppLifecycleState.detached) { + // 저장 _saveGameState(); + + // 모바일: 게임 일시정지 + 사운드 정지 + if (isMobile) { + widget.controller.pause(saveOnStop: false); + widget.audioService?.stopBgm(); + } } + + // 모바일: 앱이 포그라운드로 돌아올 때 전체 재로드 + if (appState == AppLifecycleState.resumed && isMobile) { + _reloadGameScreen(); + } + } + + /// 모바일 재진입 시 전체 화면 재로드 + Future _reloadGameScreen() async { + // 세이브 파일에서 다시 로드 + await widget.controller.loadAndStart(cheatsEnabled: widget.controller.cheatsEnabled); + + if (!mounted) return; + + // 화면 재생성 (상태 초기화) + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (_) => GamePlayScreen( + controller: widget.controller, + audioService: widget.audioService, + currentThemeMode: widget.currentThemeMode, + onThemeModeChange: widget.onThemeModeChange, + ), + ), + ); } Future _saveGameState() async {