Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions app/src/main/java/io/github/plastix/buzz/detail/Confetti.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.plastix.buzz.detail

import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import io.github.plastix.buzz.util.radians
import io.github.plastix.buzz.util.randomFloat
import kotlin.math.cos
import kotlin.math.sin

data class Projectile(val origin: Offset, val angle: Float, val speed: Float, val color: Color) {
val G = -60.8f;
fun position(t: Float): Offset {
val x = speed * t * cos(angle)
val y = (speed * t * sin(angle)) - (0.5f * G * t * t)
return origin + Offset(x, y)
}
}

@Composable
fun ConfettiCanvas(trigger: Boolean) {
if (!trigger) return;

val animationDuration = 1000
val confettiColors = remember { listOf(Color.Red, Color.Green, Color.Blue, Color.Yellow) }

val infiniteTransition = rememberInfiniteTransition()
val alpha by infiniteTransition.animateFloat(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta remind me to swap out the infiniteTransition for a normal animation. It's easier to test if you can let it loop, but no reason to leave it like that.

initialValue = 1f,
targetValue = 0f,
animationSpec = infiniteRepeatable(
animation = tween(animationDuration, easing = FastOutLinearInEasing),
)
)

val t by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 10f,
animationSpec = infiniteRepeatable(
animation = tween(animationDuration, easing = LinearEasing),
)
)

val xSpread = 100f
val ySpread = 10f
val angleSpread = 15f;

val projectiles = remember {
List(50) {
Projectile(
Offset(randomFloat(-xSpread, xSpread), randomFloat(-ySpread, ySpread)),
randomFloat(270f - angleSpread, 270f + angleSpread).radians(),
randomFloat(150f, 300f),
confettiColors.random()
)
}
}

Canvas(modifier = Modifier.fillMaxSize()) {
projectiles.forEach {
val prevPos = center + it.position(t - 0.1f)
val pos = center + it.position(t)
drawLine(it.color.copy(alpha = alpha), prevPos, pos, strokeWidth = 8f)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ fun PuzzleDetailUi(
}
}


@Composable
fun PuzzleDetailScreen(viewModel: PuzzleDetailViewModel) {
when (val state =
viewModel.viewStates.observeAsState(PuzzleDetailViewState.Loading).value) {
is PuzzleDetailViewState.Loading -> PuzzleDetailLoadingState()
is PuzzleDetailViewState.Success -> {
val gameState = state.boardGameState
ConfettiCanvas(gameState.activeWordToast != null)
if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE) {
PuzzleBoardLandscape(
gameState,
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/io/github/plastix/buzz/util/MathUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.plastix.buzz.util

import kotlin.math.PI
import kotlin.random.Random

fun Float.radians(): Float {
return (this / 180f * PI).toFloat()
}

fun randomFloat(min: Float = 0f, max: Float = 1f): Float {
return min + Random.nextFloat() * (max - min)
}