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,7 +688,8 @@ 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) {
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()
@ -709,6 +710,7 @@ class MainActivity : AppCompatActivity() {
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 =

View file

@ -5,28 +5,27 @@ 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>()
vibratorManager?.defaultVibrator ?: throw IllegalStateException("No vibrator available")
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)
fun vibrateForLineClear(lineCount: Int) {

View file

@ -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,17 +909,22 @@ 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)
}
/**
* Get the current score