Update game mechanics and haptic feedback implementation

This commit is contained in:
cmclark00 2025-03-28 20:41:03 -04:00
parent af0082a6db
commit d0700202b7
3 changed files with 41 additions and 37 deletions

View file

@ -688,27 +688,29 @@ class MainActivity : AppCompatActivity() {
// Add an on back pressed callback to handle back button/gesture // Add an on back pressed callback to handle back button/gesture
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackPressedDispatcher.addCallback(this) { onBackPressedDispatcher.addCallback(this, object : androidx.activity.OnBackPressedCallback(true) {
// If we're playing the game, handle it as a pause action instead of exiting override fun handleOnBackPressed() {
if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) { // If we're playing the game, handle it as a pause action instead of exiting
gameView.pause() if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) {
gameMusic.pause() gameView.pause()
showPauseMenu() gameMusic.pause()
binding.pauseStartButton.visibility = View.GONE showPauseMenu()
binding.resumeButton.visibility = View.VISIBLE binding.pauseStartButton.visibility = View.GONE
} else if (binding.pauseContainer.visibility == View.VISIBLE) { binding.resumeButton.visibility = View.VISIBLE
// If pause menu is showing, handle as a resume } else if (binding.pauseContainer.visibility == View.VISIBLE) {
resumeGame() // If pause menu is showing, handle as a resume
} else if (binding.gameOverContainer.visibility == View.VISIBLE) { resumeGame()
// If game over is showing, go back to title } else if (binding.gameOverContainer.visibility == View.VISIBLE) {
hideGameOver() // If game over is showing, go back to title
showTitleScreen() hideGameOver()
} else if (titleScreen.visibility == View.VISIBLE) { showTitleScreen()
// If title screen is showing, allow normal back behavior (exit app) } else if (titleScreen.visibility == View.VISIBLE) {
isEnabled = false // If title screen is showing, allow normal back behavior (exit app)
onBackPressedDispatcher.onBackPressed() isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
} }
} })
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// For Android 11 (R) to Android 12 (S), use the WindowInsetsController to disable gestures // For Android 11 (R) to Android 12 (S), use the WindowInsetsController to disable gestures
window.insetsController?.systemBarsBehavior = window.insetsController?.systemBarsBehavior =

View file

@ -5,27 +5,26 @@ import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.os.VibratorManager import android.os.VibratorManager
import android.util.Log
import android.view.HapticFeedbackConstants import android.view.HapticFeedbackConstants
import android.view.View import android.view.View
import androidx.core.content.getSystemService import android.util.Log
/** /**
* Handles haptic feedback for game events * Handles haptic feedback for game events
*/ */
class GameHaptics(private val context: Context) { class GameHaptics(private val context: Context) {
private val TAG = "GameHaptics" companion object {
private const val TAG = "GameHaptics"
}
// Vibrator service // Vibrator service
private val vibrator: Vibrator by lazy { private val vibrator: Vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val vibratorManager = context.getSystemService<VibratorManager>() vibratorManager.defaultVibrator
vibratorManager?.defaultVibrator ?: throw IllegalStateException("No vibrator available") } else {
} else { @Suppress("DEPRECATION")
@Suppress("DEPRECATION") context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
} }
// Vibrate for line clear (more intense for more lines) // Vibrate for line clear (more intense for more lines)

View file

@ -820,7 +820,6 @@ class GameView @JvmOverloads constructor(
} }
} }
lastTapTime = currentTime lastTapTime = currentTime
return true
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
@ -878,7 +877,6 @@ class GameView @JvmOverloads constructor(
// No direction lock yet, don't process movement // No direction lock yet, don't process movement
} }
} }
return true
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
@ -911,16 +909,21 @@ class GameView @JvmOverloads constructor(
} else if (moveTime < minTapTime && } else if (moveTime < minTapTime &&
abs(deltaY) < maxTapMovement && abs(deltaY) < maxTapMovement &&
abs(deltaX) < maxTapMovement) { abs(deltaX) < maxTapMovement) {
// This was a tap, perform click // Quick tap with minimal movement (rotation)
performClick() if (currentTime - lastRotationTime >= rotationCooldown) {
Log.d("GameView", "Rotation detected")
gameBoard.rotate()
lastRotationTime = currentTime
invalidate()
}
} }
// Reset direction lock // Reset direction lock
lockedDirection = null lockedDirection = null
return true
} }
} }
return super.onTouchEvent(event)
return true
} }
/** /**