From d0700202b7c6e20fa982825990a1f75d6c978c14 Mon Sep 17 00:00:00 2001 From: cmclark00 Date: Fri, 28 Mar 2025 20:41:03 -0400 Subject: [PATCH] Update game mechanics and haptic feedback implementation --- app/src/main/java/com/mintris/MainActivity.kt | 42 ++++++++++--------- .../main/java/com/mintris/game/GameHaptics.kt | 21 +++++----- .../main/java/com/mintris/game/GameView.kt | 15 ++++--- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/mintris/MainActivity.kt b/app/src/main/java/com/mintris/MainActivity.kt index e16ef0b..c084f6c 100644 --- a/app/src/main/java/com/mintris/MainActivity.kt +++ b/app/src/main/java/com/mintris/MainActivity.kt @@ -688,27 +688,29 @@ class MainActivity : AppCompatActivity() { // Add an on back pressed callback to handle back button/gesture if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - onBackPressedDispatcher.addCallback(this) { - // If we're playing the game, handle it as a pause action instead of exiting - if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) { - gameView.pause() - gameMusic.pause() - showPauseMenu() - binding.pauseStartButton.visibility = View.GONE - binding.resumeButton.visibility = View.VISIBLE - } else if (binding.pauseContainer.visibility == View.VISIBLE) { - // If pause menu is showing, handle as a resume - resumeGame() - } else if (binding.gameOverContainer.visibility == View.VISIBLE) { - // If game over is showing, go back to title - hideGameOver() - showTitleScreen() - } else if (titleScreen.visibility == View.VISIBLE) { - // If title screen is showing, allow normal back behavior (exit app) - isEnabled = false - onBackPressedDispatcher.onBackPressed() + onBackPressedDispatcher.addCallback(this, object : androidx.activity.OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + // If we're playing the game, handle it as a pause action instead of exiting + if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) { + gameView.pause() + gameMusic.pause() + showPauseMenu() + binding.pauseStartButton.visibility = View.GONE + binding.resumeButton.visibility = View.VISIBLE + } else if (binding.pauseContainer.visibility == View.VISIBLE) { + // If pause menu is showing, handle as a resume + resumeGame() + } else if (binding.gameOverContainer.visibility == View.VISIBLE) { + // If game over is showing, go back to title + hideGameOver() + showTitleScreen() + } else if (titleScreen.visibility == View.VISIBLE) { + // If title screen is showing, allow normal back behavior (exit app) + isEnabled = false + onBackPressedDispatcher.onBackPressed() + } } - } + }) } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // For Android 11 (R) to Android 12 (S), use the WindowInsetsController to disable gestures window.insetsController?.systemBarsBehavior = diff --git a/app/src/main/java/com/mintris/game/GameHaptics.kt b/app/src/main/java/com/mintris/game/GameHaptics.kt index 7a46482..44e5011 100644 --- a/app/src/main/java/com/mintris/game/GameHaptics.kt +++ b/app/src/main/java/com/mintris/game/GameHaptics.kt @@ -5,27 +5,26 @@ import android.os.Build import android.os.VibrationEffect import android.os.Vibrator import android.os.VibratorManager -import android.util.Log import android.view.HapticFeedbackConstants import android.view.View -import androidx.core.content.getSystemService +import android.util.Log /** * Handles haptic feedback for game events */ class GameHaptics(private val context: Context) { - private val TAG = "GameHaptics" + companion object { + private const val TAG = "GameHaptics" + } // Vibrator service - private val vibrator: Vibrator by lazy { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - val vibratorManager = context.getSystemService() - vibratorManager?.defaultVibrator ?: throw IllegalStateException("No vibrator available") - } else { - @Suppress("DEPRECATION") - context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator - } + private val vibrator: Vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager + vibratorManager.defaultVibrator + } else { + @Suppress("DEPRECATION") + context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator } // Vibrate for line clear (more intense for more lines) diff --git a/app/src/main/java/com/mintris/game/GameView.kt b/app/src/main/java/com/mintris/game/GameView.kt index 74273f8..e4d7625 100644 --- a/app/src/main/java/com/mintris/game/GameView.kt +++ b/app/src/main/java/com/mintris/game/GameView.kt @@ -820,7 +820,6 @@ class GameView @JvmOverloads constructor( } } lastTapTime = currentTime - return true } MotionEvent.ACTION_MOVE -> { @@ -878,7 +877,6 @@ class GameView @JvmOverloads constructor( // No direction lock yet, don't process movement } } - return true } MotionEvent.ACTION_UP -> { @@ -911,16 +909,21 @@ class GameView @JvmOverloads constructor( } else if (moveTime < minTapTime && abs(deltaY) < maxTapMovement && abs(deltaX) < maxTapMovement) { - // This was a tap, perform click - performClick() + // Quick tap with minimal movement (rotation) + if (currentTime - lastRotationTime >= rotationCooldown) { + Log.d("GameView", "Rotation detected") + gameBoard.rotate() + lastRotationTime = currentTime + invalidate() + } } // Reset direction lock lockedDirection = null - return true } } - return super.onTouchEvent(event) + + return true } /**