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 // 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) {
override fun handleOnBackPressed() {
// If we're playing the game, handle it as a pause action instead of exiting // If we're playing the game, handle it as a pause action instead of exiting
if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) { if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) {
gameView.pause() gameView.pause()
@ -709,6 +710,7 @@ class MainActivity : AppCompatActivity() {
onBackPressedDispatcher.onBackPressed() 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,28 +5,27 @@ 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)
fun vibrateForLineClear(lineCount: Int) { fun vibrateForLineClear(lineCount: Int) {

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,17 +909,22 @@ 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 true
} }
}
return super.onTouchEvent(event)
}
/** /**
* Get the current score * Get the current score