wordmaker: queue word-place animations (#807)
- Commit
b63ba6c133b24ad7bc7086420a726fa389e3da73- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-13 19:49 UTC (1 month ago)
- Parents
fa983ec
Diff
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameScreen.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameScreen.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameScreen.ktindex 603195de8b..e0741a2743 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameScreen.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameScreen.kt@@ -6,7 +6,6 @@ package com.vayunmathur.games.wordmaker.ui import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.AnimationVector1D-import androidx.compose.animation.core.tween import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.rememberTransformableState import androidx.compose.foundation.gestures.transformable@@ -29,7 +28,6 @@ import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember-import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier@@ -51,8 +49,6 @@ import com.vayunmathur.games.wordmaker.data.CrosswordData import com.vayunmathur.games.wordmaker.data.GameMode import com.vayunmathur.games.wordmaker.platform.WordGameActions import com.vayunmathur.games.wordmaker.platform.WordGameUiState-import com.vayunmathur.games.wordmaker.ui.components.AnimatedLetter-import com.vayunmathur.games.wordmaker.ui.components.WordToAnimate import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.DesktopMaxWidthContainer@@ -66,8 +62,6 @@ import com.vayunmathur.library.util.AchievementsManager import com.vayunmathur.games.wordmaker.ui.components.CrosswordBoard import com.vayunmathur.games.wordmaker.ui.components.WordMakerModeChooser import kotlinx.coroutines.delay-import kotlinx.coroutines.joinAll-import kotlinx.coroutines.launch /** Room above the wheel for the formed-word box and its padding. */ private val WHEEL_HEADROOM = 70.dp@@ -107,10 +101,8 @@ fun WordGameScreen( var rootOffset by remember { mutableStateOf(Offset.Zero) } var wordWithDefinition by remember { mutableStateOf<Pair<String, List<String>>?>(null) } - // Animation state- val coroutineScope = rememberCoroutineScope()- var animatedWord by remember(levelKey) { mutableStateOf<String?>(null) }- val animationProgress = remember(levelKey) { Animatable(0f) }+ // Animation state: solution and bonus word-place flights queue up (#807) so a fast+ // second word waits for the first flight to finish instead of cancelling it. var wordBoxOffset by remember(levelKey) { mutableStateOf(Offset.Zero) } var bonusButtonOffset by remember(levelKey) { mutableStateOf(Offset.Zero) } var crosswordCellPositions by remember(levelKey) {@@ -123,8 +115,16 @@ fun WordGameScreen( emptyMap() ) }- var wordToAnimate by remember(levelKey) { mutableStateOf<WordToAnimate?>(null) }- var animatedLetters by remember(levelKey) { mutableStateOf<List<AnimatedLetter>>(emptyList()) }+ val wordPlaceAnimations = rememberWordPlaceAnimations(+ levelKey = levelKey,+ crosswordData = crosswordData,+ foundWords = foundWords,+ actions = actions,+ density = density,+ rootOffset = rootOffset,+ letterChooserPositions = letterChooserPositions,+ crosswordCellPositions = crosswordCellPositions,+ ) // Animatables for shaking (we'll animate them directly when submission fails) val wordShakeAnim = remember { Animatable(0f) }@@ -138,40 +138,6 @@ fun WordGameScreen( } - LaunchedEffect(wordToAnimate) {- wordToAnimate?.let { animationInfo ->- val word = animationInfo.word- val letterPositions = crosswordData.letterPositions[word]?.firstOrNull()- if (letterPositions != null) {- val letters = word.mapIndexed { index, char ->- val id = animationInfo.letterIds[index]- val start = (letterChooserPositions[id] ?: Offset.Zero) - rootOffset- val end =- (crosswordCellPositions[letterPositions[index]] ?: Offset.Zero) - rootOffset-- val offsetCorrection = with(density) { 15.dp.toPx() }- val correctedStart = start.plus(Offset(offsetCorrection, offsetCorrection))-- AnimatedLetter(char, correctedStart, end, Animatable(0f))- }- animatedLetters = letters-- // Animate- val jobs = letters.map {- launch {- it.progress.animateTo(1f, animationSpec = tween(durationMillis = 800))- }- }- jobs.joinAll()-- // After animation- actions.addFoundWord(word)- wordToAnimate = null- animatedLetters = emptyList()- }- }- }- val isWon = crosswordData.winsWith(foundWords) LaunchedEffect(competitiveDeadline, isCompetitive, isWon, levelKey) {@@ -241,7 +207,7 @@ fun WordGameScreen( foundWords = foundWords, revealedHints = revealedHints, crosswordData = crosswordData,- wordToAnimate = wordToAnimate?.word,+ wordToAnimate = wordPlaceAnimations.activeSolutionWord, onCellPositioned = { position, offset -> if (crosswordCellPositions[position] != offset) { crosswordCellPositions =@@ -286,15 +252,10 @@ fun WordGameScreen( crosswordData = crosswordData, foundWords = foundWords, bonusWords = bonusWords,- wordToAnimate = wordToAnimate,- animatedWord = animatedWord,- scope = coroutineScope,- animationProgress = animationProgress,+ animations = wordPlaceAnimations, density = density, wordShakeAnim = wordShakeAnim, bonusShakeAnim = bonusShakeAnim,- onAnimateSolution = { wordToAnimate = it },- onAnimateBonus = { animatedWord = it }, onWordBoxPositioned = { wordBoxOffset = it }, letterChooserPositions = letterChooserPositions, onLetterPositioned = { id, offset ->@@ -326,11 +287,11 @@ fun WordGameScreen( bonusWords = bonusWords, wordWithDefinition = wordWithDefinition, onDismissDefinition = { wordWithDefinition = null },- animatedWord = animatedWord,- animationProgress = animationProgress,+ animatedWord = wordPlaceAnimations.activeBonusWord,+ animationProgress = wordPlaceAnimations.bonusProgress, wordBoxOffset = wordBoxOffset, bonusButtonOffset = bonusButtonOffset,- animatedLetters = animatedLetters,+ animatedLetters = wordPlaceAnimations.animatedLetters, scale = scale, ) }
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameSubmission.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameSubmission.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameSubmission.ktindex 9429a2445a..422aded048 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameSubmission.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameSubmission.kt@@ -23,16 +23,14 @@ fun wordSubmitHandler( crosswordData: CrosswordData, foundWords: Set<String>, bonusWords: Set<String>,- wordToAnimate: WordToAnimate?,- animatedWord: String?,+ pendingSolutionWords: Set<String>,+ queuedBonusWords: Set<String>, actions: WordGameActions,- scope: CoroutineScope,- animationProgress: Animatable<Float, AnimationVector1D>, density: Density, wordShakeAnim: Animatable<Float, AnimationVector1D>, bonusShakeAnim: Animatable<Float, AnimationVector1D>, onAnimateSolution: (WordToAnimate) -> Unit,- onAnimateBonus: (String?) -> Unit,+ onAnimateBonus: (String) -> Unit, ): suspend CoroutineScope.(String, List<Int>) -> Unit = { word, ids -> suspend fun shakeAnim(anim: Animatable<Float, AnimationVector1D>, duration: Int = 40) { for (o in listOf(-16f, 12f, -8f, 6f, -3f, 0f)) {@@ -45,10 +43,10 @@ fun wordSubmitHandler( // #543 — re-entering an already-found/bonus word must not // replay the board/bonus animation. Check for duplicates- // first, including words that are mid-animation (not yet- // flushed to DataStore + foundWords/bonusWords flow).- val alreadyFound = word in foundWords || word == wordToAnimate?.word- val alreadyBonus = word in bonusWords || word == animatedWord+ // first, including words still queued or mid-flight (#807,+ // not yet flushed to DataStore + foundWords/bonusWords flow).+ val alreadyFound = word in foundWords || word in pendingSolutionWords+ val alreadyBonus = word in bonusWords || word in queuedBonusWords when { isSolution && alreadyFound -> shakeAnim(wordShakeAnim) isBonus && alreadyBonus -> {@@ -60,15 +58,10 @@ fun wordSubmitHandler( onAnimateSolution(WordToAnimate(word, ids)) actions.onSolutionWordFound(word) }- isBonus && word !in bonusWords -> {- scope.launch {- onAnimateBonus(word)- animationProgress.snapTo(0f)- animationProgress.animateTo(1f, tween(800))- actions.addBonusWord(word)- onAnimateBonus(null)- }- }+ // #807: only enqueue — the screen drains the bonus queue one flight at a+ // time, so a fast second word no longer steals the shared progress+ // Animatable and cancels the first word's flight (and its addBonusWord).+ isBonus && word !in bonusWords -> onAnimateBonus(word) else -> shakeAnim(wordShakeAnim) } }
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameWheel.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameWheel.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameWheel.ktindex 2824eff796..09132cf20c 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameWheel.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameWheel.kt@@ -26,18 +26,15 @@ import com.vayunmathur.games.wordmaker.R import com.vayunmathur.games.wordmaker.data.CrosswordData import com.vayunmathur.games.wordmaker.data.WheelSpacing import com.vayunmathur.games.wordmaker.platform.WordGameActions-import com.vayunmathur.games.wordmaker.ui.components.AnimatedLetter import com.vayunmathur.games.wordmaker.ui.components.CompetitiveStatusBar import com.vayunmathur.games.wordmaker.ui.components.DailyStatusBar import com.vayunmathur.games.wordmaker.ui.components.LetterChooser-import com.vayunmathur.games.wordmaker.ui.components.WordToAnimate import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator import com.vayunmathur.library.ui.FilledIconButton import com.vayunmathur.library.ui.Icon import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.Text-import kotlinx.coroutines.CoroutineScope /** * Status bars plus the letter wheel below the board, extracted from@@ -63,15 +60,10 @@ fun WordGameWheel( crosswordData: CrosswordData, foundWords: Set<String>, bonusWords: Set<String>,- wordToAnimate: WordToAnimate?,- animatedWord: String?,- scope: CoroutineScope,- animationProgress: Animatable<Float, AnimationVector1D>,+ animations: WordPlaceAnimations, density: Density, wordShakeAnim: Animatable<Float, AnimationVector1D>, bonusShakeAnim: Animatable<Float, AnimationVector1D>,- onAnimateSolution: (WordToAnimate) -> Unit,- onAnimateBonus: (String?) -> Unit, onWordBoxPositioned: (Offset) -> Unit, letterChooserPositions: Map<Int, Offset>, onLetterPositioned: (Int, Offset) -> Unit,@@ -114,16 +106,14 @@ fun WordGameWheel( crosswordData = crosswordData, foundWords = foundWords, bonusWords = bonusWords,- wordToAnimate = wordToAnimate,- animatedWord = animatedWord,+ pendingSolutionWords = animations.pendingSolutionWords,+ queuedBonusWords = animations.queuedBonusWords, actions = actions,- scope = scope,- animationProgress = animationProgress, density = density, wordShakeAnim = wordShakeAnim, bonusShakeAnim = bonusShakeAnim,- onAnimateSolution = onAnimateSolution,- onAnimateBonus = onAnimateBonus,+ onAnimateSolution = animations.enqueueSolution,+ onAnimateBonus = animations.enqueueBonus, ), onWordBoxPositioned = onWordBoxPositioned, onLetterPositioned = { id, offset ->
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordPlaceAnimations.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordPlaceAnimations.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordPlaceAnimations.ktnew file mode 100644index 0000000000..729fc300d7--- /dev/null+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordPlaceAnimations.kt@@ -0,0 +1,128 @@+// RAW ANIMATION EXCEPTION: the letter-flight and bonus-flight are imperative motion - letters+// fly from the chooser to their grid cells on per-letter Animatables drained from a FIFO queue,+// which is not a state a declarative animation can settle on.+package com.vayunmathur.games.wordmaker.ui++import androidx.compose.animation.core.Animatable+import androidx.compose.animation.core.AnimationVector1D+import androidx.compose.animation.core.tween+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.unit.Density+import androidx.compose.ui.unit.dp+import com.vayunmathur.games.wordmaker.data.CrosswordData+import com.vayunmathur.games.wordmaker.platform.WordGameActions+import com.vayunmathur.games.wordmaker.ui.components.AnimatedLetter+import com.vayunmathur.games.wordmaker.ui.components.WordToAnimate+import kotlinx.coroutines.joinAll+import kotlinx.coroutines.launch++/**+ * Live state of the queued word-place flights, drained by [rememberWordPlaceAnimations].+ */+class WordPlaceAnimations(+ /** Solution words accepted but not yet visible in the found-words flow. */+ val pendingSolutionWords: Set<String>,+ /** Bonus words accepted but not yet visible in the bonus-words flow. */+ val queuedBonusWords: Set<String>,+ /** Solution word currently flying into the grid, for the board's hide-while-flying slot. */+ val activeSolutionWord: String?,+ /** Bonus word currently flying toward the bonus button. */+ val activeBonusWord: String?,+ val bonusProgress: Animatable<Float, AnimationVector1D>,+ val animatedLetters: List<AnimatedLetter>,+ val enqueueSolution: (WordToAnimate) -> Unit,+ val enqueueBonus: (String) -> Unit,+)++/**+ * FIFO word-place flights for the letter wheel (#807), extracted from [WordGameScreen]+ * to keep that file under the length limit. Every submitted word finishes its full flight:+ * a fast second word queues behind the first instead of cancelling it (which used to drop+ * the first word's grid/bonus credit entirely).+ */+@Composable+fun rememberWordPlaceAnimations(+ levelKey: String,+ crosswordData: CrosswordData,+ foundWords: Set<String>,+ actions: WordGameActions,+ density: Density,+ rootOffset: Offset,+ letterChooserPositions: Map<Int, Offset>,+ crosswordCellPositions: Map<Pair<Int, Int>, Offset>,+): WordPlaceAnimations {+ var solutionQueue by remember(levelKey) { mutableStateOf<List<WordToAnimate>>(emptyList()) }+ var activeSolutionWord by remember(levelKey) { mutableStateOf<String?>(null) }+ var flushedSolutions by remember(levelKey) { mutableStateOf<Set<String>>(emptySet()) }+ var bonusQueue by remember(levelKey) { mutableStateOf<List<String>>(emptyList()) }+ var activeBonusWord by remember(levelKey) { mutableStateOf<String?>(null) }+ val bonusProgress = remember(levelKey) { Animatable(0f) }+ var animatedLetters by remember(levelKey) { mutableStateOf<List<AnimatedLetter>>(emptyList()) }++ // Solution flights drain head-to-tail: appending to the queue never restarts the+ // in-flight effect because its key (the head word) is unchanged, so each word flies+ // the full flight before the next one starts.+ LaunchedEffect(solutionQueue.firstOrNull()) {+ val animationInfo = solutionQueue.firstOrNull() ?: return@LaunchedEffect+ val word = animationInfo.word+ val letterPositions = crosswordData.letterPositions[word]?.firstOrNull()+ if (letterPositions == null) {+ solutionQueue = solutionQueue.drop(1)+ return@LaunchedEffect+ }+ activeSolutionWord = word+ val letters = word.mapIndexed { index, char ->+ val id = animationInfo.letterIds[index]+ val start = (letterChooserPositions[id] ?: Offset.Zero) - rootOffset+ val end = (crosswordCellPositions[letterPositions[index]] ?: Offset.Zero) - rootOffset+ val offsetCorrection = with(density) { 15.dp.toPx() }+ val correctedStart = start.plus(Offset(offsetCorrection, offsetCorrection))+ AnimatedLetter(char, correctedStart, end, Animatable(0f))+ }+ animatedLetters = letters+ letters.map { launch { it.progress.animateTo(1f, tween(LETTER_FLIGHT_MILLIS)) } }.joinAll()+ actions.addFoundWord(word)+ animatedLetters = emptyList()+ activeSolutionWord = null+ flushedSolutions = flushedSolutions + word+ solutionQueue = solutionQueue.drop(1)+ }++ // A finished flight takes a DataStore round-trip to show up in the found-words flow;+ // keep it in the dedup set until then so re-entering it shakes instead of replaying.+ LaunchedEffect(foundWords) {+ if (flushedSolutions.isNotEmpty()) flushedSolutions = flushedSolutions - foundWords+ }++ // Bonus flights share one progress Animatable, so they fly one at a time too: a fast+ // second word used to steal it mid-flight and skip the first word's addBonusWord.+ LaunchedEffect(bonusQueue.firstOrNull()) {+ val word = bonusQueue.firstOrNull() ?: return@LaunchedEffect+ activeBonusWord = word+ bonusProgress.snapTo(0f)+ bonusProgress.animateTo(1f, tween(BONUS_FLIGHT_MILLIS))+ actions.addBonusWord(word)+ activeBonusWord = null+ bonusQueue = bonusQueue.drop(1)+ }++ return WordPlaceAnimations(+ pendingSolutionWords = (solutionQueue.map { it.word } + flushedSolutions).toSet(),+ queuedBonusWords = bonusQueue.toSet(),+ activeSolutionWord = activeSolutionWord,+ activeBonusWord = activeBonusWord,+ bonusProgress = bonusProgress,+ animatedLetters = animatedLetters,+ enqueueSolution = { solutionQueue = solutionQueue + it },+ enqueueBonus = { bonusQueue = bonusQueue + it },+ )+}++private const val LETTER_FLIGHT_MILLIS = 800+private const val BONUS_FLIGHT_MILLIS = 800