mirror of
https://github.com/cmclark00/mintris.git
synced 2025-05-18 02:35:21 +01:00
Fix hard drop double drop issue at high stack levels by improving state management and preventing race conditions
This commit is contained in:
parent
8125d0f7b3
commit
222f48e8c4
1 changed files with 27 additions and 5 deletions
|
@ -31,6 +31,8 @@ class GameBoard(
|
||||||
var level = 1
|
var level = 1
|
||||||
var lines = 0
|
var lines = 0
|
||||||
var isGameOver = false
|
var isGameOver = false
|
||||||
|
var isHardDropInProgress = false // Make public
|
||||||
|
var isPieceLocking = false // Make public
|
||||||
|
|
||||||
// Scoring state
|
// Scoring state
|
||||||
private var combo = 0
|
private var combo = 0
|
||||||
|
@ -150,6 +152,9 @@ class GameBoard(
|
||||||
* Move the current piece down (soft drop)
|
* Move the current piece down (soft drop)
|
||||||
*/
|
*/
|
||||||
fun moveDown(): Boolean {
|
fun moveDown(): Boolean {
|
||||||
|
// Don't allow movement if a hard drop is in progress or piece is locking
|
||||||
|
if (isHardDropInProgress || isPieceLocking) return false
|
||||||
|
|
||||||
return if (canMove(0, 1)) {
|
return if (canMove(0, 1)) {
|
||||||
currentPiece?.y = currentPiece?.y?.plus(1) ?: 0
|
currentPiece?.y = currentPiece?.y?.plus(1) ?: 0
|
||||||
onPieceMove?.invoke()
|
onPieceMove?.invoke()
|
||||||
|
@ -164,9 +169,19 @@ class GameBoard(
|
||||||
* Hard drop the current piece
|
* Hard drop the current piece
|
||||||
*/
|
*/
|
||||||
fun hardDrop() {
|
fun hardDrop() {
|
||||||
while (moveDown()) {
|
if (isHardDropInProgress || isPieceLocking) return // Prevent multiple hard drops
|
||||||
// Keep moving down until blocked
|
|
||||||
|
isHardDropInProgress = true
|
||||||
|
val piece = currentPiece ?: return
|
||||||
|
|
||||||
|
// Move piece down until it can't move anymore
|
||||||
|
while (canMove(0, 1)) {
|
||||||
|
piece.y++
|
||||||
|
onPieceMove?.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lock the piece immediately
|
||||||
|
lockPiece()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -249,9 +264,12 @@ class GameBoard(
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lock the current piece in place and check for completed lines
|
* Lock the current piece in place
|
||||||
*/
|
*/
|
||||||
fun lockPiece() {
|
private fun lockPiece() {
|
||||||
|
if (isPieceLocking) return // Prevent recursive locking
|
||||||
|
isPieceLocking = true
|
||||||
|
|
||||||
val piece = currentPiece ?: return
|
val piece = currentPiece ?: return
|
||||||
|
|
||||||
// Add the piece to the grid
|
// Add the piece to the grid
|
||||||
|
@ -275,11 +293,15 @@ class GameBoard(
|
||||||
// Find and clear lines immediately
|
// Find and clear lines immediately
|
||||||
findAndClearLines()
|
findAndClearLines()
|
||||||
|
|
||||||
// Spawn new piece
|
// Spawn new piece immediately
|
||||||
spawnPiece()
|
spawnPiece()
|
||||||
|
|
||||||
// Allow holding piece again after locking
|
// Allow holding piece again after locking
|
||||||
canHold = true
|
canHold = true
|
||||||
|
|
||||||
|
// Reset both states after everything is done
|
||||||
|
isPieceLocking = false
|
||||||
|
isHardDropInProgress = false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue