|
|
|
@@ -23,18 +23,22 @@ import androidx.compose.ui.geometry.Offset
|
|
|
|
|
import androidx.compose.ui.geometry.Size
|
|
|
|
|
import androidx.compose.ui.graphics.Brush
|
|
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
|
|
import androidx.compose.ui.graphics.FilterQuality
|
|
|
|
|
import androidx.compose.ui.graphics.ImageBitmap
|
|
|
|
|
import androidx.compose.ui.graphics.Path
|
|
|
|
|
import androidx.compose.ui.graphics.StrokeCap
|
|
|
|
|
import androidx.compose.ui.graphics.drawscope.Stroke
|
|
|
|
|
import androidx.compose.ui.graphics.drawscope.rotate
|
|
|
|
|
import androidx.compose.ui.graphics.drawscope.withTransform
|
|
|
|
|
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
|
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
|
|
import androidx.compose.ui.platform.LocalWindowInfo
|
|
|
|
|
import androidx.compose.ui.res.imageResource
|
|
|
|
|
import androidx.compose.ui.res.painterResource
|
|
|
|
|
import androidx.compose.ui.text.font.Font
|
|
|
|
|
import androidx.compose.ui.text.font.FontFamily
|
|
|
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
|
|
|
import androidx.compose.ui.unit.IntOffset
|
|
|
|
|
import androidx.compose.ui.unit.IntSize
|
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import androidx.compose.ui.unit.sp
|
|
|
|
|
import androidx.tv.material3.Text
|
|
|
|
@@ -85,8 +89,6 @@ internal data class SnakeGame(val width: Int, val height: Int, val body: List<Sn
|
|
|
|
|
|
|
|
|
|
private val Marquee = FontFamily(Font(R.font.anton_regular))
|
|
|
|
|
private val Honey = Color(0xFFFFD84A)
|
|
|
|
|
private val Sky = Color(0xFF3FC1FF)
|
|
|
|
|
private val Berry = Color(0xFFFF5A4F)
|
|
|
|
|
|
|
|
|
|
private fun hexagon(center: Offset, radius: Float) = Path().apply {
|
|
|
|
|
for (i in 0..5) {
|
|
|
|
@@ -141,31 +143,34 @@ private fun SnakeBackdrop(scene: Int, modifier: Modifier) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The snake's line from head to tail, partway between two steps, so it glides from cell to cell rather than jumping. */
|
|
|
|
|
internal fun snakeLine(game: SnakeGame, before: List<SnakeCell>?, progress: Float): List<Pair<Float, Float>> {
|
|
|
|
|
val cells = game.body.map { it.x.toFloat() to it.y.toFloat() }.toMutableList()
|
|
|
|
|
if (before.isNullOrEmpty() || !game.alive) return cells
|
|
|
|
|
fun between(from: SnakeCell, to: SnakeCell) = (from.x + (to.x - from.x) * progress) to (from.y + (to.y - from.y) * progress)
|
|
|
|
|
cells[0] = between(before.first(), game.body.first())
|
|
|
|
|
cells[cells.lastIndex] = between(before.last(), game.body.last())
|
|
|
|
|
return cells
|
|
|
|
|
}
|
|
|
|
|
/** Where each piece of the snake is drawn, partway between two steps, so it glides from cell to cell rather than jumping. */
|
|
|
|
|
internal fun snakeLine(game: SnakeGame, before: List<SnakeCell>?, progress: Float): List<Pair<Float, Float>> =
|
|
|
|
|
game.body.mapIndexed { index, cell ->
|
|
|
|
|
val from = before?.getOrNull(index)?.takeIf { game.alive } ?: cell
|
|
|
|
|
(from.x + (cell.x - from.x) * progress) to (from.y + (cell.y - from.y) * progress)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
private fun SnakeField(game: SnakeGame, modifier: Modifier, before: List<SnakeCell>? = null, progress: Float = 1f) {
|
|
|
|
|
val head = ImageBitmap.imageResource(R.drawable.snake_head)
|
|
|
|
|
val dead = ImageBitmap.imageResource(R.drawable.snake_dead)
|
|
|
|
|
val blob = ImageBitmap.imageResource(R.drawable.snake_blob)
|
|
|
|
|
val apple = ImageBitmap.imageResource(R.drawable.snake_apple)
|
|
|
|
|
Canvas(modifier) {
|
|
|
|
|
val cell = minOf(size.width / game.width, size.height / game.height)
|
|
|
|
|
val left = (size.width - cell * game.width) / 2
|
|
|
|
|
val top = (size.height - cell * game.height) / 2
|
|
|
|
|
for (y in 0 until game.height) for (x in 0 until game.width) if ((x + y) % 2 == 0)
|
|
|
|
|
drawRect(Color.White.copy(alpha = .025f), Offset(left + x * cell, top + y * cell), Size(cell, cell))
|
|
|
|
|
fun center(x: Float, y: Float) = Offset(left + (x + .5f) * cell, top + (y + .5f) * cell)
|
|
|
|
|
drawPath(hexagon(center(game.food.x.toFloat(), game.food.y.toFloat()), cell * .46f), Berry)
|
|
|
|
|
val line = snakeLine(game, before, progress).map { center(it.first, it.second) }
|
|
|
|
|
line.zipWithNext { a, b -> drawLine(Sky, a, b, strokeWidth = cell * .68f, cap = StrokeCap.Round) }
|
|
|
|
|
drawCircle(Color.White, cell * .42f, line.first())
|
|
|
|
|
val look = Offset(game.heading.dx * cell * .16f, game.heading.dy * cell * .16f)
|
|
|
|
|
drawCircle(Navy, cell * .13f, line.first() + look)
|
|
|
|
|
fun sprite(image: ImageBitmap, x: Float, y: Float, scale: Float) {
|
|
|
|
|
val side = (cell * scale).toInt()
|
|
|
|
|
val offset = (cell - side) / 2
|
|
|
|
|
drawImage(image, dstOffset = IntOffset((left + x * cell + offset).toInt(), (top + y * cell + offset).toInt()), dstSize = IntSize(side, side), filterQuality = FilterQuality.Medium)
|
|
|
|
|
}
|
|
|
|
|
sprite(apple, game.food.x.toFloat(), game.food.y.toFloat(), 1.05f)
|
|
|
|
|
val line = snakeLine(game, before, progress)
|
|
|
|
|
line.drop(1).asReversed().forEach { (x, y) -> sprite(blob, x, y, 1f) }
|
|
|
|
|
line.first().let { (x, y) -> sprite(if (game.alive) head else dead, x, y, 1.25f) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -196,6 +201,13 @@ private fun SnakeScreen(best: Int, onBest: (Int) -> Unit, onLeave: () -> Unit) {
|
|
|
|
|
}
|
|
|
|
|
if (!game.alive && game.score > best) onBest(game.score)
|
|
|
|
|
}
|
|
|
|
|
val context = LocalContext.current
|
|
|
|
|
val settings = remember { OrbitSettings(context) }
|
|
|
|
|
var musicOn by remember { mutableStateOf(settings.snakeMusic) }
|
|
|
|
|
val music = remember { android.media.MediaPlayer.create(context, R.raw.snake_theme)?.apply { isLooping = true; setVolume(.5f, .5f) } }
|
|
|
|
|
DisposableEffect(music) { onDispose { music?.release() } }
|
|
|
|
|
val playing = musicOn && !paused && game.alive
|
|
|
|
|
LaunchedEffect(playing) { if (playing) music?.start() else if (music?.isPlaying == true) music.pause() }
|
|
|
|
|
val menuFocus = remember { FocusRequester() }
|
|
|
|
|
LaunchedEffect(paused, game.alive) { if (paused || !game.alive) menuFocus.requestFocus() else focus.requestFocus() }
|
|
|
|
|
BackHandler { if (game.alive) paused = !paused else onLeave() }
|
|
|
|
@@ -233,6 +245,7 @@ private fun SnakeScreen(best: Int, onBest: (Int) -> Unit, onLeave: () -> Unit) {
|
|
|
|
|
if (!game.alive) { game = SnakeGame.start(30, 22, random); before = null; progress = 1f }
|
|
|
|
|
paused = false
|
|
|
|
|
})
|
|
|
|
|
OrbitButton("Music: ${if (musicOn) "On" else "Off"}", Modifier.width(200.dp), onClick = { musicOn = !musicOn; settings.snakeMusic = musicOn })
|
|
|
|
|
OrbitButton("Leave the game", Modifier.width(200.dp), onClick = onLeave)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|