|
|
|
@@ -17,6 +17,8 @@ import tv.orbit.classic.arcade.classicPace
|
|
|
|
|
import tv.orbit.classic.arcade.classicPanel
|
|
|
|
|
import tv.orbit.classic.arcade.gameKeys
|
|
|
|
|
import tv.orbit.classic.arcade.previousLine
|
|
|
|
|
import tv.orbit.classic.arcade.SNAKE_CONSTELLATIONS_SCORES
|
|
|
|
|
import tv.orbit.classic.arcade.SpeechBubble
|
|
|
|
|
|
|
|
|
|
import android.view.KeyEvent
|
|
|
|
|
import androidx.activity.compose.BackHandler
|
|
|
|
@@ -77,7 +79,17 @@ internal data class SnakeCell(val x: Int, val y: Int)
|
|
|
|
|
* was lost); it's taken on the next step.
|
|
|
|
|
*/
|
|
|
|
|
internal data class SnakeGame(val width: Int, val height: Int, val body: List<SnakeCell>, val heading: SnakeDirection, val wanted: SnakeDirection, val food: SnakeCell, val score: Int = 0, val alive: Boolean = true,
|
|
|
|
|
val queued: SnakeDirection? = null) {
|
|
|
|
|
val queued: SnakeDirection? = null,
|
|
|
|
|
/** Constellations (Phase 2): the stars of this constellation in the order to collect them, and which is next. Classic: none. */
|
|
|
|
|
val pattern: List<SnakeCell> = emptyList(), val nextStar: Int = 0,
|
|
|
|
|
/** Which constellation this is (by [Constellations]), and how many have been lit. */
|
|
|
|
|
val constellation: Int = 0, val lit: Int = 0,
|
|
|
|
|
/** A pair of wormholes: into one, out of the other, still heading the same way. */
|
|
|
|
|
val wormholes: List<SnakeCell> = emptyList(),
|
|
|
|
|
/** Stardust (Gentle): the tail never grows past this; 0 means it grows for ever, as in Classic. */
|
|
|
|
|
val maxLen: Int = 0,
|
|
|
|
|
/** The constellation just lit, for Luna to name; null until the first. */
|
|
|
|
|
val named: String? = null) {
|
|
|
|
|
/** A turn back on yourself is ignored, as it could only ever end the game. A turn while one is waiting goes next. */
|
|
|
|
|
fun turn(direction: SnakeDirection): SnakeGame {
|
|
|
|
|
if (wanted != heading) return if (direction == wanted || direction == wanted.opposite) this else copy(queued = direction)
|
|
|
|
@@ -87,20 +99,43 @@ internal data class SnakeGame(val width: Int, val height: Int, val body: List<Sn
|
|
|
|
|
fun step(random: java.util.Random): SnakeGame {
|
|
|
|
|
if (!alive) return this
|
|
|
|
|
val head = body.first()
|
|
|
|
|
val next = SnakeCell(head.x + wanted.dx, head.y + wanted.dy)
|
|
|
|
|
var next = SnakeCell(head.x + wanted.dx, head.y + wanted.dy)
|
|
|
|
|
// Through a wormhole: out of the other one.
|
|
|
|
|
if (wormholes.size == 2) { if (next == wormholes[0]) next = wormholes[1] else if (next == wormholes[1]) next = wormholes[0] }
|
|
|
|
|
val eating = next == food
|
|
|
|
|
// The tail moves away this step, so it's safe to follow it unless it's about to grow.
|
|
|
|
|
val solid = if (eating) body else body.dropLast(1)
|
|
|
|
|
if (next.x !in 0 until width || next.y !in 0 until height || next in solid) return copy(heading = wanted, alive = false, queued = null)
|
|
|
|
|
val grown = listOf(next) + solid
|
|
|
|
|
val grown = (listOf(next) + solid).let { if (maxLen > 0 && it.size > maxLen) it.take(maxLen) else it }
|
|
|
|
|
if (pattern.isNotEmpty()) return starStep(grown, eating, random)
|
|
|
|
|
return copy(body = grown, heading = wanted, wanted = queued ?: wanted, queued = null, food = if (eating) nextFood(width, height, grown, random) ?: food else food,
|
|
|
|
|
score = if (eating) score + 10 else score, alive = grown.size < width * height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Constellations: the next star in order is the food; the last lights the constellation, Luna names it, and a new one appears. */
|
|
|
|
|
private fun starStep(grown: List<SnakeCell>, eating: Boolean, random: java.util.Random): SnakeGame {
|
|
|
|
|
val moved = copy(body = grown, heading = wanted, wanted = queued ?: wanted, queued = null)
|
|
|
|
|
if (!eating) return moved
|
|
|
|
|
val n = nextStar + 1
|
|
|
|
|
if (n < pattern.size) return moved.copy(nextStar = n, food = pattern[n], score = score + 10)
|
|
|
|
|
val c = (constellation + 1) % Constellations.size
|
|
|
|
|
val placed = placeConstellation(c, width, height, grown.toSet() + wormholes, random)
|
|
|
|
|
return moved.copy(score = score + 10 + CONSTELLATION_BONUS * pattern.size, lit = lit + 1, named = Constellations[constellation].first,
|
|
|
|
|
constellation = c, pattern = placed, nextStar = 0, food = placed.first())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Starts at a walking pace and quickens noticeably with every bite, up to a limit. */
|
|
|
|
|
val tickMs get() = (150L - score / 10 * 5).coerceAtLeast(50L)
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
/** A Constellations game: two wormholes, the first constellation, and stardust on Gentle. */
|
|
|
|
|
fun constellations(width: Int, height: Int, random: java.util.Random, gentle: Boolean): SnakeGame {
|
|
|
|
|
val g = start(width, height, random)
|
|
|
|
|
val holes = listOf(SnakeCell(width / 6, height / 5), SnakeCell(width - 1 - width / 6, height - 1 - height / 5))
|
|
|
|
|
val first = placeConstellation(0, width, height, g.body.toSet() + holes, random)
|
|
|
|
|
return g.copy(wormholes = holes, pattern = first, nextStar = 0, food = first.first(), maxLen = if (gentle) STARDUST_LENGTH else 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun start(width: Int, height: Int, random: java.util.Random): SnakeGame {
|
|
|
|
|
val y = height / 2
|
|
|
|
|
val body = List(4) { SnakeCell(width / 4 - it, y) }
|
|
|
|
@@ -124,13 +159,40 @@ internal data class SnakeGame(val width: Int, val height: Int, val body: List<Sn
|
|
|
|
|
|
|
|
|
|
internal const val SNAKE_BACKDROPS = 4
|
|
|
|
|
|
|
|
|
|
/** Constellations' bonus per star when one is lit, and Gentle's stardust tail length. */
|
|
|
|
|
internal const val CONSTELLATION_BONUS = 20
|
|
|
|
|
internal const val STARDUST_LENGTH = 12
|
|
|
|
|
|
|
|
|
|
/** Orbit's own constellations, Luna's names for them, each a set of stars in the order to collect them. */
|
|
|
|
|
internal val Constellations: List<Pair<String, List<Pair<Int, Int>>>> = listOf(
|
|
|
|
|
"The Telly" to listOf(0 to 0, 4 to 0, 4 to 3, 0 to 3, 2 to 5),
|
|
|
|
|
"Ollie's Ring" to listOf(0 to 2, 2 to 0, 5 to 0, 7 to 2, 5 to 4, 2 to 4),
|
|
|
|
|
"The Big Dish" to listOf(0 to 0, 1 to 2, 3 to 3, 5 to 2, 6 to 0, 3 to 1),
|
|
|
|
|
"The Comet" to listOf(0 to 0, 2 to 1, 4 to 2, 6 to 3, 8 to 4),
|
|
|
|
|
"Bumble's Hive" to listOf(1 to 0, 3 to 0, 4 to 2, 3 to 4, 1 to 4, 0 to 2),
|
|
|
|
|
"The Remote" to listOf(1 to 0, 1 to 2, 0 to 4, 2 to 4, 1 to 6))
|
|
|
|
|
|
|
|
|
|
/** Where a constellation goes: somewhere it fits, clear of [taken]; tried at random, then anywhere free. */
|
|
|
|
|
internal fun placeConstellation(index: Int, width: Int, height: Int, taken: Set<SnakeCell>, random: java.util.Random): List<SnakeCell> {
|
|
|
|
|
val shape = Constellations[index].second
|
|
|
|
|
val w = shape.maxOf { it.first } + 1; val h = shape.maxOf { it.second } + 1
|
|
|
|
|
repeat(80) {
|
|
|
|
|
val ox = 1 + random.nextInt((width - w - 1).coerceAtLeast(1)); val oy = 1 + random.nextInt((height - h - 1).coerceAtLeast(1))
|
|
|
|
|
val cells = shape.map { (x, y) -> SnakeCell(ox + x, oy + y) }
|
|
|
|
|
if (cells.none { it in taken } && cells.all { it.x in 0 until width && it.y in 0 until height }) return cells
|
|
|
|
|
}
|
|
|
|
|
val free = (0 until height).flatMap { y -> (0 until width).map { x -> SnakeCell(x, y) } }.filter { it !in taken }.shuffled(random)
|
|
|
|
|
return free.take(shape.size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A new backdrop every ten bites, going round. */
|
|
|
|
|
internal fun snakeBackdrop(score: Int) = (score / 100) % SNAKE_BACKDROPS
|
|
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
|
// Out of a wormhole the head appears at once, rather than sliding across the sky.
|
|
|
|
|
val from = before?.getOrNull(index)?.takeIf { game.alive && kotlin.math.abs(it.x - cell.x) + kotlin.math.abs(it.y - cell.y) <= 1 } ?: cell
|
|
|
|
|
(from.x + (cell.x - from.x) * progress) to (from.y + (cell.y - from.y) * progress)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -147,6 +209,21 @@ private fun SnakeField(game: SnakeGame, modifier: Modifier, before: List<SnakeCe
|
|
|
|
|
for (y in 0 until game.height) for (x in 0 until game.width) if ((x + y) % 2 == 0)
|
|
|
|
|
drawRect(ArcadeColours.Ink.copy(alpha = .025f), Offset(left + x * cell, top + y * cell), Size(cell, cell))
|
|
|
|
|
fun at(x: Float, y: Float) = Offset(left + (x + .5f) * cell, top + (y + .5f) * cell)
|
|
|
|
|
// Constellations: the stars already collected joined up, the rest waiting faintly, and the wormholes.
|
|
|
|
|
if (game.pattern.isNotEmpty()) {
|
|
|
|
|
for (i in 1 until game.nextStar + 1) if (i < game.pattern.size) drawLine(Color(0xFFFFDE59).copy(alpha = .5f),
|
|
|
|
|
at(game.pattern[i - 1].x.toFloat(), game.pattern[i - 1].y.toFloat()), at(game.pattern[i].x.toFloat(), game.pattern[i].y.toFloat()), cell * .08f)
|
|
|
|
|
game.pattern.forEachIndexed { i, st ->
|
|
|
|
|
if (i < game.nextStar) drawPath(starPath(at(st.x.toFloat(), st.y.toFloat()), cell * .3f, cell * .13f, 0f), Color(0xFFFFDE59).copy(alpha = .8f))
|
|
|
|
|
else if (i > game.nextStar) drawCircle(Color.White.copy(alpha = .35f), cell * .12f, at(st.x.toFloat(), st.y.toFloat()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for ((k, w) in game.wormholes.withIndex()) {
|
|
|
|
|
val c = at(w.x.toFloat(), w.y.toFloat())
|
|
|
|
|
drawCircle(Color(0xFFA77BFF).copy(alpha = .25f), cell * .75f, c)
|
|
|
|
|
drawCircle(Color(0xFFA77BFF), cell * .45f, c, style = androidx.compose.ui.graphics.drawscope.Stroke(cell * .1f))
|
|
|
|
|
drawCircle(if (k == 0) Color(0xFF35D6F2) else Color(0xFFFF4FA3), cell * .15f, c)
|
|
|
|
|
}
|
|
|
|
|
drawPath(starPath(at(game.food.x.toFloat(), game.food.y.toFloat()), cell * .5f, cell * .22f, 0f), Color(0xFFFFDE59))
|
|
|
|
|
val line = snakeLine(game, before, progress)
|
|
|
|
|
val tail = line.drop(1)
|
|
|
|
@@ -172,12 +249,15 @@ internal fun SnakeScreen(play: ArcadePlay) {
|
|
|
|
|
var stage by remember { mutableStateOf(ClassicStage.Title) }
|
|
|
|
|
var paused by remember { mutableStateOf(false) }
|
|
|
|
|
var howTo by remember { mutableStateOf(false) }
|
|
|
|
|
var bestBefore by remember { mutableIntStateOf(play.best) }
|
|
|
|
|
// Classic, or Constellations (Phase 2): chosen on the title, remembered, each with its own High Scores.
|
|
|
|
|
var stars by remember { mutableStateOf(play.level["mode"] == "Constellations") }
|
|
|
|
|
val table = if (stars) SNAKE_CONSTELLATIONS_SCORES else gameDef.id
|
|
|
|
|
var bestBefore by remember { mutableIntStateOf(play.bestIn(table)) }
|
|
|
|
|
val focus = remember { FocusRequester() }
|
|
|
|
|
val pace = classicPace(play.difficulty)
|
|
|
|
|
PauseWhenHidden(stage == ClassicStage.Play && !paused) { paused = true }
|
|
|
|
|
val run = rememberRun(play.onRunEnd) { RunEnd(game.score, finished = false) }
|
|
|
|
|
fun startRun() { bestBefore = latestPlay.best; game = SnakeGame.start(30, 22, random); before = null; progress = 1f; run.begin(); paused = false; stage = ClassicStage.Play }
|
|
|
|
|
val run = rememberRun({ end -> if (game.pattern.isNotEmpty()) latestPlay.onRunEndIn(SNAKE_CONSTELLATIONS_SCORES, end) else latestPlay.onRunEnd(end) }) { RunEnd(game.score, finished = false) }
|
|
|
|
|
fun startRun() { bestBefore = latestPlay.bestIn(table); game = if (stars) SnakeGame.constellations(30, 22, random, gentle = latestPlay.difficulty == tv.orbit.classic.arcade.Difficulty.Gentle) else SnakeGame.start(30, 22, random); before = null; progress = 1f; run.begin(); paused = false; stage = ClassicStage.Play }
|
|
|
|
|
fun leave() { run.end(RunEnd(game.score, finished = !game.alive)); latestPlay.onLeave() }
|
|
|
|
|
// Steps on the clock (Difficulty sets its pace; Normal as it always was), drawn on every frame between them.
|
|
|
|
|
var sinceStep by remember { mutableLongStateOf(0L) }
|
|
|
|
@@ -198,12 +278,19 @@ internal fun SnakeScreen(play: ArcadePlay) {
|
|
|
|
|
LaunchedEffect(playing) { play.audio.music(R.raw.snake_theme, playing, base = .5f, gameOn = musicOn) }
|
|
|
|
|
DisposableEffect(Unit) { onDispose { play.audio.stopMusic() } }
|
|
|
|
|
LaunchedEffect(stage, paused) { if (stage == ClassicStage.Play && !paused) runCatching { focus.requestFocus() } }
|
|
|
|
|
// Luna names each constellation as it lights up, for a moment.
|
|
|
|
|
var named by remember { mutableStateOf<String?>(null) }
|
|
|
|
|
LaunchedEffect(game.lit) { if (game.lit > 0) { named = game.named; kotlinx.coroutines.delay(2_500); named = null } }
|
|
|
|
|
BackHandler(stage == ClassicStage.Play && !paused) { paused = true }
|
|
|
|
|
|
|
|
|
|
if (stage == ClassicStage.Title) {
|
|
|
|
|
ClassicTitle(gameDef, ArcadeLook.comet(0), OrbCharacter.Blaze.colour, "Stars! I'm starving!", listOf("Your best" to if (play.best > 0) "%,d".format(play.best) else "Not played yet"),
|
|
|
|
|
val best = play.bestIn(table)
|
|
|
|
|
ClassicTitle(gameDef, ArcadeLook.comet(0), OrbCharacter.Blaze.colour, if (stars) "Join the stars up, in order!" else "Stars! I'm starving!",
|
|
|
|
|
listOf("Your best" to if (best > 0) "%,d".format(best) else "Not played yet"),
|
|
|
|
|
timed = true, difficulty = play.difficulty, refocus = !howTo, onPlay = ::startRun, onHowTo = { howTo = true }, onBack = { latestPlay.onLeave() },
|
|
|
|
|
note = "He speeds up with every star, and the sky changes every hundred points.")
|
|
|
|
|
mode = if (stars) "Constellations" else "Classic", modes = 2, onMode = { stars = !stars; latestPlay.setLevel("mode", if (stars) "Constellations" else "Classic") },
|
|
|
|
|
note = if (stars) "Each constellation's stars wait in a pattern: collect them in order to light it up for a big bonus, and Luna names it. Two wormholes take Blaze across the sky. On Gentle his tail is stardust: it never grows past twelve."
|
|
|
|
|
else "He speeds up with every star, and the sky changes every hundred points.")
|
|
|
|
|
if (howTo) HowToPlayCard(gameDef, firstRun = false, everyTime = null, onEveryTime = {}, onPlay = { howTo = false }, onBack = { howTo = false })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -227,11 +314,18 @@ internal fun SnakeScreen(play: ArcadePlay) {
|
|
|
|
|
HudLabel("BEST"); HudValue("%,d".format(maxOf(bestBefore, game.score)))
|
|
|
|
|
Spacer(Modifier.height(6.dp))
|
|
|
|
|
HudLabel("TAIL"); HudValue("${game.body.size - 1}")
|
|
|
|
|
if (game.pattern.isNotEmpty()) {
|
|
|
|
|
Spacer(Modifier.height(6.dp))
|
|
|
|
|
HudLabel("CONSTELLATION"); Text("Star ${game.nextStar + 1} of ${game.pattern.size}", color = ArcadeColours.Ink, fontSize = 16.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
HudLabel("LIT ${game.lit}")
|
|
|
|
|
Spacer(Modifier.weight(1f))
|
|
|
|
|
if (named != null) { SpeechBubble("That's ${named}!"); Canvas(Modifier.size(48.dp)) { drawOrbChaser(center, size.minDimension * .38f, OrbCharacter.Luna, OrbDir.Down, false, false, false, 0f) } }
|
|
|
|
|
}
|
|
|
|
|
}) {
|
|
|
|
|
SnakeField(game, Modifier.fillMaxSize().classicPanel().padding(6.dp), before, progress)
|
|
|
|
|
}
|
|
|
|
|
if (stage == ClassicStage.Result) ClassicResult(gameDef, "COMET TRAIL · ${game.body.size - 1} TAIL PUFFS", "SCORE", "%,d".format(game.score),
|
|
|
|
|
newBest = game.score > bestBefore && game.score > 0, previous = previousLine(gameDef.scoreKind, bestBefore, gameDef.title), detail = null,
|
|
|
|
|
if (stage == ClassicStage.Result) ClassicResult(gameDef, if (game.pattern.isNotEmpty()) "COMET TRAIL · CONSTELLATIONS · ${game.lit} LIT" else "COMET TRAIL · ${game.body.size - 1} TAIL PUFFS", "SCORE", "%,d".format(game.score),
|
|
|
|
|
newBest = game.score > bestBefore && game.score > 0, previous = previousLine(gameDef.scoreKind, bestBefore, if (game.pattern.isNotEmpty()) "Constellations" else gameDef.title), detail = null,
|
|
|
|
|
cheer = Mascot.Zip, onAgain = ::startRun, onBack = ::leave)
|
|
|
|
|
if (paused && stage == ClassicStage.Play) ArcadePauseMenu(gameDef, "Comet Trail · ${"%,d".format(game.score)}",
|
|
|
|
|
onResume = { paused = false }, onRestart = { run.end(RunEnd(game.score, finished = false)); startRun() }, onExit = ::leave,
|
|
|
|
|