From 301bf1e64f844e990451267068d89cd8ec13be95 Mon Sep 17 00:00:00 2001 From: cmclark00 Date: Tue, 1 Apr 2025 15:21:03 -0400 Subject: [PATCH] Fix: Play game over sound with animation start --- .../java/com/pixelmintdrop/MainActivity.kt | 75 +++++++++++-------- .../pixelmintdrop/MainActivityViewModel.kt | 22 ++++++ 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/pixelmintdrop/MainActivity.kt b/app/src/main/java/com/pixelmintdrop/MainActivity.kt index f681fb2..b9ceee7 100644 --- a/app/src/main/java/com/pixelmintdrop/MainActivity.kt +++ b/app/src/main/java/com/pixelmintdrop/MainActivity.kt @@ -84,8 +84,6 @@ class MainActivity : AppCompatActivity(), private var pauseMenuScrollView: ScrollView? = null // Game state - private var isSoundEnabled = true - private var isMusicEnabled = true private var piecesPlaced = 0 private var gameStartTime = 0L private var selectedLevel = 1 @@ -232,6 +230,17 @@ class MainActivity : AppCompatActivity(), binding.currentLevelText.text = newLevel.toString() }) + // Observe Sound/Music state + viewModel.isSoundEnabled.observe(this, Observer { enabled -> + updateSoundToggleUI(enabled) + }) + + viewModel.isMusicEnabled.observe(this, Observer { enabled -> + updateMusicToggleUI(enabled) + // Also update GameMusic immediately + gameMusic.setEnabled(enabled) + }) + // Load random mode setting isRandomModeEnabled = getSharedPreferences("com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE) .getBoolean("random_mode_enabled", false) @@ -354,9 +363,8 @@ class MainActivity : AppCompatActivity(), // Set up music toggle binding.musicToggle.setOnClickListener { - isMusicEnabled = !isMusicEnabled - gameMusic.setEnabled(isMusicEnabled) - updateMusicToggleUI() + viewModel.toggleMusic() // Use ViewModel + // Observer will call updateMusicToggleUI and gameMusic.setEnabled } // Set up callbacks @@ -365,9 +373,12 @@ class MainActivity : AppCompatActivity(), } gameView.onGameOver = { finalScore -> - // Start animation & pause music + // Start animation & pause music & play sound gameView.startGameOverAnimation() gameMusic.pause() + if (viewModel.isSoundEnabled.value == true) { // Play sound if enabled + gameMusic.playGameOver() + } // Calculate final stats, XP, and high score val timePlayedMs = System.currentTimeMillis() - gameStartTime @@ -405,7 +416,7 @@ class MainActivity : AppCompatActivity(), gameView.onLineClear = { lineCount -> Log.d(TAG, "Received line clear callback: $lineCount lines") // Use enhanced haptic feedback for line clears - if (isSoundEnabled) { + if (viewModel.isSoundEnabled.value == true) { // Read from ViewModel Log.d(TAG, "Sound is enabled, triggering haptic feedback") try { gameHaptics.vibrateForLineClear(lineCount) @@ -422,7 +433,7 @@ class MainActivity : AppCompatActivity(), // Add callbacks for piece movement and locking gameView.onPieceMove = { - if (isSoundEnabled) { + if (viewModel.isSoundEnabled.value == true) { // Read from ViewModel gameHaptics.vibrateForPieceMove() } } @@ -443,7 +454,8 @@ class MainActivity : AppCompatActivity(), binding.settingsButton.setOnClickListener { gameHaptics.performHapticFeedback(it, HapticFeedbackConstants.VIRTUAL_KEY) - toggleSound() + viewModel.toggleSound() // Use ViewModel + // Observer will call updateSoundToggleUI } // Set up pause menu buttons @@ -569,11 +581,6 @@ class MainActivity : AppCompatActivity(), // Make the container visible binding.gameOverContainer.visibility = View.VISIBLE - // Play game over sound if not already played by progression - if (isSoundEnabled && progressionScreen.visibility != View.VISIBLE) { - gameMusic.playGameOver() - } - // Vibrate if not already vibrated by progression if (progressionScreen.visibility != View.VISIBLE) { vibrate(VibrationEffect.EFFECT_DOUBLE_CLICK) @@ -824,19 +831,6 @@ class MainActivity : AppCompatActivity(), binding.pauseContainer.visibility = View.GONE } - /** - * Toggle sound on/off - */ - private fun toggleSound() { - isSoundEnabled = !isSoundEnabled - binding.settingsButton.text = getString( - if (isSoundEnabled) R.string.sound_on else R.string.sound_off - ) - - // Vibrate to provide feedback - vibrate(VibrationEffect.EFFECT_CLICK) - } - /** * Update the level selector display */ @@ -862,9 +856,21 @@ class MainActivity : AppCompatActivity(), } } - private fun updateMusicToggleUI() { + /** + * Update the sound toggle button text + */ + private fun updateSoundToggleUI(enabled: Boolean) { + binding.settingsButton.text = getString( + if (enabled) R.string.sound_on else R.string.sound_off + ) + } + + /** + * Update the music toggle button icon + */ + private fun updateMusicToggleUI(enabled: Boolean) { binding.musicToggle.setImageResource( - if (isMusicEnabled) R.drawable.ic_volume_up + if (enabled) R.drawable.ic_volume_up else R.drawable.ic_volume_off ) } @@ -923,9 +929,12 @@ class MainActivity : AppCompatActivity(), } gameView.onGameOver = { finalScore -> - // Start animation & pause music + // Start animation & pause music & play sound gameView.startGameOverAnimation() gameMusic.pause() + if (viewModel.isSoundEnabled.value == true) { // Play sound if enabled + gameMusic.playGameOver() + } // Calculate final stats, XP, and high score val timePlayedMs = System.currentTimeMillis() - gameStartTime @@ -977,13 +986,13 @@ class MainActivity : AppCompatActivity(), binding.resumeButton.visibility = View.GONE // Start background music if enabled - if (isMusicEnabled) { + if (viewModel.isMusicEnabled.value == true) { // Read from ViewModel gameMusic.start() } // Start the game gameView.start() - gameMusic.setEnabled(isMusicEnabled) + // Observer ensures gameMusic is enabled/disabled correctly via gameMusic.setEnabled() // Reset session stats statsManager.startNewSession() @@ -1000,7 +1009,7 @@ class MainActivity : AppCompatActivity(), private fun resumeGame() { gameView.resume() - if (isMusicEnabled) { + if (viewModel.isMusicEnabled.value == true) { // Read from ViewModel gameMusic.resume() } // Force a redraw to ensure pieces aren't frozen diff --git a/app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt b/app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt index e62a138..f513892 100644 --- a/app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt +++ b/app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt @@ -14,6 +14,14 @@ class MainActivityViewModel : ViewModel() { val currentScore: LiveData = _currentScore val currentLevel: LiveData = _currentLevel + // --- Sound & Music State --- + private val _isSoundEnabled = MutableLiveData(true) // Default to true + val isSoundEnabled: LiveData = _isSoundEnabled + + private val _isMusicEnabled = MutableLiveData(true) // Default to true + val isMusicEnabled: LiveData = _isMusicEnabled + // --------------------------- + // Example function to update the score (logic would be moved here) fun incrementScore(points: Long) { _currentScore.value = (_currentScore.value ?: 0L) + points @@ -36,5 +44,19 @@ class MainActivityViewModel : ViewModel() { // Reset other game state within the ViewModel as needed } + // --- Sound & Music Logic --- + fun toggleSound() { + _isSoundEnabled.value = !(_isSoundEnabled.value ?: true) + } + + fun setMusicEnabled(enabled: Boolean) { + _isMusicEnabled.value = enabled + } + + fun toggleMusic() { + setMusicEnabled(!(_isMusicEnabled.value ?: true)) + } + // -------------------------- + // Add other state variables and logic related to game state here } \ No newline at end of file