Give Snake its sprites and an original chiptune (0.77.0)
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m9s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m21s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-02 14:51:36 +00:00
co-authored by Claude Opus 5.5
parent 458eb042c9
commit 847c9a108d
14 changed files with 124 additions and 24 deletions
@@ -66,7 +66,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
connection.connectTimeout = 10_000
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.76.3\"")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.77.0\"")
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
if (request.method == "POST") {
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
@@ -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)
}
}
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.76.3")
next.setRequestProperty("User-Agent", "OrbitTV/0.77.0")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
@@ -28,6 +28,9 @@ import androidx.tv.material3.Text
/** Choices about how Orbit behaves on this box. Nothing here is private, so it needs no encrypted storage. */
internal class OrbitSettings(context: Context) {
private val saved = context.getSharedPreferences("orbit_settings", Context.MODE_PRIVATE)
var snakeMusic: Boolean
get() = saved.getBoolean("snake_music", true)
set(value) { saved.edit().putBoolean("snake_music", value).apply() }
var snakeBest: Int
get() = saved.getInt("snake_best", 0)
set(value) { saved.edit().putInt("snake_best", value).apply() }
@@ -137,6 +140,7 @@ private fun AccountDetail(server: String, titles: Long?, onSignOut: () -> Unit,
}
} else SettingLine("Sign out", "Disconnect this box from Emby.") { OrbitButton("Sign out", onClick = { confirming = true }) }
Text("Orbit TV $version", color = Muted, fontSize = 12.sp)
Text("Snake graphics by Bas de Reuver, bdrgames.nl", color = Muted, fontSize = 12.sp)
}
}