diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeShared.kt b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeShared.kt index 9a011da6..9435b027 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeShared.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeShared.kt @@ -101,7 +101,7 @@ internal fun ArcadeScrim(content: @Composable BoxScope.() -> Unit) { */ @Composable internal fun ArcadePauseMenu(game: ArcadeGame, line: String, onResume: () -> Unit, onRestart: () -> Unit, onExit: () -> Unit, - sound: String? = null, onSound: (() -> Unit)? = null, extra: Pair Unit>? = null) { + sound: String? = null, onSound: (() -> Unit)? = null, extra: Pair Unit>? = null, extraIcon: String = "↶") { var howTo by remember { mutableStateOf(false) } if (howTo) { HowToPlayCard(game, firstRun = false, everyTime = null, onEveryTime = {}, onPlay = { howTo = false }, onBack = { howTo = false }); return } val first = remember { FocusRequester() } @@ -116,7 +116,7 @@ internal fun ArcadePauseMenu(game: ArcadeGame, line: String, onResume: () -> Uni Spacer(Modifier.height(2.dp)) ArcadeButton("Resume", Modifier.fillMaxWidth().focusRequester(first), icon = "▶", onClick = onResume) // A game's own line, where it has one (Patience's Undo on Gentle). - if (extra != null) ArcadeButton(extra.first, Modifier.fillMaxWidth(), icon = "↶", onClick = extra.second) + if (extra != null) ArcadeButton(extra.first, Modifier.fillMaxWidth(), icon = extraIcon, onClick = extra.second) ArcadeButton("Restart Level", Modifier.fillMaxWidth(), icon = "↻", onClick = onRestart) ArcadeButton("How to Play", Modifier.fillMaxWidth(), icon = "?", onClick = { howTo = true }) if (sound != null && onSound != null) ArcadeButton("Sound", Modifier.fillMaxWidth(), icon = "♪", detail = sound, onClick = onSound) diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShift.kt b/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShift.kt index 8ee4a4cb..85fb163d 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShift.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShift.kt @@ -35,6 +35,18 @@ internal fun sides(piece: Char, turns: Int): Int { /** Only these turn under OK: the star and the mast stay put. */ internal fun turnable(piece: Char) = piece == 'I' || piece == 'L' || piece == 'T' +/** Sleepy Time's doze, in hops. */ +internal const val SLEEPY_HOPS = 10 + +/** Night Shift's power-ups: how many each level gives (Sleepy Time only where there are clouds). */ +internal enum class NightPower(val title: String, val line: String) { + Moonbeam("Moonbeam", "Turns the nearest piece the light needs"), + SleepyTime("Sleepy Time", "The clouds doze for ten hops"), + StarMagnet("Star Magnet", "Nite Owl flies to the next piece to turn"), + NightVision("Night Vision", "Shows the way the light should go"); + fun forLevel(level: NightLevel) = if (this == SleepyTime && level.clouds.isEmpty()) 0 else 1 +} + internal class NightLevel(val rows: List, val solution: List, val par: Int, val clouds: List>>) { fun piece(i: Int) = rows[i / NW][i % NW * 2] fun turns(i: Int) = rows[i / NW][i % NW * 2 + 1] - '0' @@ -58,25 +70,50 @@ internal fun cloudAt(lane: List>, hops: Int): Int { } /** One level being played. Turns count as moves; hops don't, but each hop moves the clouds on. */ -internal class NightGame(val level: NightLevel, val turns: IntArray = IntArray(NW * NH) { level.turns(it) }, val owl: Int = start(level), val moves: Int = 0, val hops: Int = 0) { +internal class NightGame(val level: NightLevel, val turns: IntArray = IntArray(NW * NH) { level.turns(it) }, val owl: Int = start(level), val moves: Int = 0, val hops: Int = 0, + /** The clouds' own clock: a tick a hop, except while Sleepy Time has them dozing ([dozeFor] hops left). */ + val cloudClock: Int = hops, val dozeFor: Int = 0) { fun piece(i: Int) = level.piece(i) /** Nite Owl hops to the next tile, if there is one; the clouds drift on a tile. */ fun hop(dx: Int, dy: Int): NightGame { val x = owl % NW + dx; val y = owl / NW + dy if (x !in 0 until NW || y !in 0 until NH) return this - return NightGame(level, turns, y * NW + x, moves, hops + 1) + return NightGame(level, turns, y * NW + x, moves, hops + 1, if (dozeFor > 0) cloudClock else cloudClock + 1, (dozeFor - 1).coerceAtLeast(0)) } /** OK: the piece under Nite Owl turns a quarter, clockwise. Nothing there to turn, nothing happens. */ fun turn(): NightGame { if (!turnable(piece(owl))) return this val t = turns.copyOf(); t[owl] = (t[owl] + 1) % 4 - return NightGame(level, t, owl, moves + 1, hops) + return NightGame(level, t, owl, moves + 1, hops, cloudClock, dozeFor) } + /** The tiles the level's solution lights (clouds aside): the pieces that matter. */ + fun solutionLit(): BooleanArray = NightGame(NightLevel(level.solution, level.solution, level.par, emptyList())).lit() + private fun solutionTurns(i: Int) = level.solution[i / NW][i % NW * 2 + 1] - '0' + /** The pieces the light needs that aren't turned the right way yet, nearest Nite Owl first. */ + fun wrongPieces(): List { + val need = solutionLit() + return (0 until NW * NH).filter { need[it] && turnable(piece(it)) && sides(piece(it), turns[it]) != sides(piece(it), solutionTurns(it)) } + .sortedBy { kotlin.math.abs(it % NW - owl % NW) + kotlin.math.abs(it / NW - owl / NW) } + } + + // ---------- the power-ups (frame 41), each optional and limited per level; none counts as a move ---------- + + /** Moonbeam: the nearest piece the light needs turns the right way, free. */ + fun moonbeam(): NightGame { + val i = wrongPieces().firstOrNull() ?: return this + val t = turns.copyOf(); t[i] = solutionTurns(i) + return NightGame(level, t, owl, moves, hops, cloudClock, dozeFor) + } + /** Sleepy Time: the clouds doze where they are for the next ten hops. */ + fun sleepyTime() = NightGame(level, turns, owl, moves, hops, cloudClock, SLEEPY_HOPS) + /** Star Magnet: Nite Owl is drawn straight to the next piece the light needs. */ + fun starMagnet(): NightGame = wrongPieces().firstOrNull()?.let { NightGame(level, turns, it, moves, hops, cloudClock, dozeFor) } ?: this + /** The tiles a cloud sits on now ([at] hops), each blocking the light. */ - fun cloudTiles(at: Int = hops): IntArray = IntArray(level.clouds.size) { cloudAt(level.clouds[it], at) } + fun cloudTiles(at: Int = cloudClock): IntArray = IntArray(level.clouds.size) { cloudAt(level.clouds[it], at) } /** Every tile the light reaches: from the star along its beam, through pieces whose sides meet; clouds stop it. */ fun lit(): BooleanArray { diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShiftScreen.kt b/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShiftScreen.kt index 1e0e21ba..7fd5e252 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShiftScreen.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/nightshift/NightShiftScreen.kt @@ -102,10 +102,23 @@ internal fun NightShiftScreen(play: ArcadePlay) { var paused by remember { mutableStateOf(false) } var howTo by remember { mutableStateOf(false) } var finished by remember { mutableStateOf?>(null) } // stars, moves, par + // The level's power-ups (from the pause menu), and whether Night Vision is showing the way. + var powers by remember { mutableStateOf(NightPower.entries.associateWith { it.forLevel(game.level) }) } + var nightVision by remember { mutableStateOf(false) } + var powerMenu by remember { mutableStateOf(false) } val par = nightPar(game.level, play.difficulty) val focus = remember { FocusRequester() } - fun startLevel(c: Int, l: Int) { chapter = c; levelIx = l; game = NightGame(NightChapters[c].levels[l]); paused = false; finished = null; stage = NightStage.Play } + fun startLevel(c: Int, l: Int) { + chapter = c; levelIx = l; game = NightGame(NightChapters[c].levels[l]); paused = false; finished = null; stage = NightStage.Play + powers = NightPower.entries.associateWith { it.forLevel(game.level) }; nightVision = false; powerMenu = false + } + fun use(p: NightPower) { + if ((powers[p] ?: 0) <= 0) return + powers = powers + (p to (powers[p] ?: 0) - 1) + when (p) { NightPower.Moonbeam -> game = game.moonbeam(); NightPower.SleepyTime -> game = game.sleepyTime(); NightPower.StarMagnet -> game = game.starMagnet(); NightPower.NightVision -> nightVision = true } + powerMenu = false; paused = false + } // A finished level: its best saved (more stars, or fewer moves), and the profile's total stars in the High Scores. LaunchedEffect(game) { if (stage != NightStage.Play || !game.won) return@LaunchedEffect @@ -145,7 +158,7 @@ internal fun NightShiftScreen(play: ArcadePlay) { if (next != null) game = next next != null } - NightPlay(game, chapter, levelIx, par, keys, still = Motion.reduce || play.reduceFlashing) + NightPlay(game, chapter, levelIx, par, keys, still = Motion.reduce || play.reduceFlashing, powers = powers, nightVision = nightVision) if (stage == NightStage.Complete) finished?.let { (stars, moves, p) -> NightComplete(chapter, levelIx, stars, moves, p, last = chapter == NightChapters.lastIndex && levelIx == NightChapters[chapter].levels.lastIndex, onNext = { @@ -156,9 +169,10 @@ internal fun NightShiftScreen(play: ArcadePlay) { }, onReplay = { startLevel(chapter, levelIx) }, onChapters = { stage = NightStage.Chapters }) } - if (paused && stage == NightStage.Play) ArcadePauseMenu(gameDef, "Chapter ${chapter + 1} · Level ${levelIx + 1} · ${game.moves} moves", + if (paused && stage == NightStage.Play && powerMenu) NightPowerMenu(powers, onUse = ::use, onBack = { powerMenu = false }) + else if (paused && stage == NightStage.Play) ArcadePauseMenu(gameDef, "Chapter ${chapter + 1} · Level ${levelIx + 1} · ${game.moves} moves", onResume = { paused = false }, onRestart = { startLevel(chapter, levelIx) }, onExit = { latestPlay.onLeave() }, - extra = "Chapters" to { paused = false; stage = NightStage.Chapters }) + extra = "Power-ups (${powers.values.sum()} left)" to { powerMenu = true }, extraIcon = "✦") } } } @@ -320,10 +334,12 @@ private fun NightChaptersPage(saved: Map, current: Int, onPlay: @Suppress("ModifierParameter") // [keys] is the screen's key handling, not a layout modifier @Composable -private fun NightPlay(game: NightGame, chapter: Int, level: Int, par: Int, keys: Modifier, still: Boolean) { +private fun NightPlay(game: NightGame, chapter: Int, level: Int, par: Int, keys: Modifier, still: Boolean, powers: Map, nightVision: Boolean) { val on = remember(game) { game.lit() } val clouds = remember(game) { game.cloudTiles() } - val nextClouds = remember(game) { game.cloudTiles(game.hops + 1) } + // Where the clouds go on the next hop (nowhere while they doze). + val nextClouds = remember(game) { game.cloudTiles(if (game.dozeFor > 0) game.cloudClock else game.cloudClock + 1) } + val way = remember(game.level, nightVision) { if (nightVision) game.solutionLit() else null } Column(Modifier.fillMaxSize().then(keys).padding(start = 32.dp, end = 32.dp, top = 22.dp, bottom = 10.dp)) { Row(Modifier.fillMaxWidth().weight(1f), horizontalArrangement = Arrangement.spacedBy(18.dp)) { Column(Modifier.width(176.dp).fillMaxHeight(), verticalArrangement = Arrangement.spacedBy(2.dp)) { @@ -347,9 +363,20 @@ private fun NightPlay(game: NightGame, chapter: Int, level: Int, par: Int, keys: } Box(Modifier.weight(1f).fillMaxHeight(), contentAlignment = Alignment.Center) { Canvas(Modifier.fillMaxHeight().aspectRatio(NW / NH.toFloat()).background(NightColours.Panel.copy(alpha = .6f), RoundedCornerShape(14.dp)) - .border(1.dp, NightColours.TileEdge, RoundedCornerShape(14.dp)).padding(8.dp)) { drawBoard(game, on, clouds, nextClouds, still) } + .border(1.dp, NightColours.TileEdge, RoundedCornerShape(14.dp)).padding(8.dp)) { drawBoard(game, on, clouds, nextClouds, still, way) } } - Column(Modifier.width(110.dp).fillMaxHeight(), horizontalAlignment = Alignment.End) { + Column(Modifier.width(150.dp).fillMaxHeight(), horizontalAlignment = Alignment.End, verticalArrangement = Arrangement.spacedBy(4.dp)) { + // The level's power-ups (frame 41): used from the pause menu, so no Menu key is needed. + Text("POWER-UPS", color = NightColours.Muted, fontSize = 14.sp, letterSpacing = 2.sp) + for (p in NightPower.entries) { + val n = powers[p] ?: 0 + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) { + Text(p.title, color = if (n > 0) NightColours.Ink else NightColours.Muted.copy(alpha = .6f), fontSize = 14.sp) + Text("×$n", color = NightColours.Muted, fontSize = 14.sp) + } + } + if (game.dozeFor > 0) Text("Clouds dozing: ${game.dozeFor}", color = NightColours.Glow, fontSize = 13.sp) + Text("Pause to use one", color = NightColours.Muted, fontSize = 13.sp) Spacer(Modifier.weight(1f)) // Luna in her Night Cap keeps Nite Owl company. Canvas(Modifier.size(80.dp)) { drawNightCap(center + Offset(0f, size.height * .08f), size.minDimension * .34f) } @@ -361,7 +388,7 @@ private fun NightPlay(game: NightGame, chapter: Int, level: Int, par: Int, keys: } /** The board: tiles, beam pieces (lit where the light reaches), the star and mast, the clouds and where they go next, and Nite Owl. */ -private fun DrawScope.drawBoard(game: NightGame, on: BooleanArray, clouds: IntArray, nextClouds: IntArray, still: Boolean) { +private fun DrawScope.drawBoard(game: NightGame, on: BooleanArray, clouds: IntArray, nextClouds: IntArray, still: Boolean, way: BooleanArray? = null) { val cell = minOf(size.width / NW, size.height / NH) val ox = (size.width - cell * NW) / 2; val oy = (size.height - cell * NH) / 2 val pad = cell * .05f @@ -372,6 +399,8 @@ private fun DrawScope.drawBoard(game: NightGame, on: BooleanArray, clouds: IntAr if (nextClouds.contains(i) && !clouds.contains(i)) drawRoundRect(NightColours.Muted.copy(alpha = .55f), Offset(x + pad * 2.5f, y + pad * 2.5f), Size(cell - 5 * pad, cell - 5 * pad), CornerRadius(cell * .1f), style = Stroke(1.5f, pathEffect = androidx.compose.ui.graphics.PathEffect.dashPathEffect(floatArrayOf(6f, 5f)))) + // Night Vision: the way the light should go, softly outlined. + if (way != null && way[i]) drawRoundRect(NightColours.Star.copy(alpha = .35f), Offset(x + pad * 1.5f, y + pad * 1.5f), Size(cell - 3 * pad, cell - 3 * pad), CornerRadius(cell * .12f), style = Stroke(2f)) val c = Offset(x + cell / 2, y + cell / 2) val p = game.piece(i) when (p) { @@ -451,3 +480,28 @@ private fun NightComplete(chapter: Int, level: Int, stars: Int, moves: Int, par: KeyFooter(listOf(Control("◀▶", "Choose"), Control("OK", "Select"), Control("Back", "Chapters")), Modifier.align(Alignment.BottomStart).padding(start = 24.dp, end = 24.dp, bottom = 10.dp)) } } + +// ---------- Power-ups (from the pause menu) ---------- + +@Composable +private fun NightPowerMenu(powers: Map, onUse: (NightPower) -> Unit, onBack: () -> Unit) { + val first = remember { FocusRequester() } + LaunchedEffect(Unit) { runCatching { first.requestFocus() } } + BackHandler(onBack = onBack) + tv.orbit.classic.arcade.ArcadeScrim { + Column(Modifier.width(360.dp).onBackKey(action = onBack).background(NightColours.Panel, RoundedCornerShape(12.dp)).border(1.dp, NightColours.TileEdge, RoundedCornerShape(12.dp)).padding(16.dp), + verticalArrangement = Arrangement.spacedBy(8.dp)) { + Text("POWER-UPS", color = NightColours.Ink, fontFamily = Anton, fontSize = 26.sp) + Text("Optional: par and stars work without them.", color = NightColours.Muted, fontSize = 13.sp) + NightPower.entries.forEachIndexed { k, p -> + val n = powers[p] ?: 0 + Column { + ArcadeButton(p.title, Modifier.fillMaxWidth().then(if (k == 0) Modifier.focusRequester(first) else Modifier), detail = "×$n", enabled = n > 0, onClick = { onUse(p) }) + Text(p.line, Modifier.padding(start = 12.dp, top = 2.dp), color = NightColours.Muted, fontSize = 12.sp) + } + } + ArcadeButton("Back", Modifier.fillMaxWidth(), icon = "←", onClick = onBack) + } + KeyFooter(listOf(Control("▲▼", "Choose"), Control("OK", "Use"), Control("Back", "Back")), Modifier.align(Alignment.BottomStart).padding(start = 32.dp, end = 32.dp, bottom = 16.dp)) + } +} diff --git a/android/app/src/test/java/tv/orbit/classic/arcade/nightshift/NightShiftLevelsTest.kt b/android/app/src/test/java/tv/orbit/classic/arcade/nightshift/NightShiftLevelsTest.kt index 472c41e3..160efb4a 100644 --- a/android/app/src/test/java/tv/orbit/classic/arcade/nightshift/NightShiftLevelsTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/arcade/nightshift/NightShiftLevelsTest.kt @@ -89,4 +89,23 @@ class NightShiftLevelsTest { assertEquals(24, NightProgress.totalStars(saved)); assertEquals("First Light done · The Quiet Mast in progress", chapterStatus(saved)) assertTrue(NightProgress.better(2 to 20, 3, 30)); assertTrue(NightProgress.better(2 to 20, 2, 19)); assertFalse(NightProgress.better(2 to 20, 2, 25)) } + + @Test fun thePowerUpsHelpWithoutCountingAsMoves() { + val lv = NightChapters[2].levels[3] + val g = NightGame(lv) + val wrong = g.wrongPieces() + assertTrue(wrong.isNotEmpty()) + // Moonbeam turns the nearest needed piece right, free. + val m = g.moonbeam() + assertEquals(wrong.size - 1, m.wrongPieces().size); assertEquals(0, m.moves) + // Star Magnet takes Owl to the next piece to turn. + assertEquals(wrong.first(), g.starMagnet().owl) + // Sleepy Time: the clouds stay put for ten hops, then drift again. + var s = g.sleepyTime(); val before = s.cloudTiles().toList() + repeat(SLEEPY_HOPS) { s = s.hop(if (s.owl % NW == 0) 1 else -1, 0) } + assertEquals(before, s.cloudTiles().toList()) + assertNotEquals(before, s.hop(if (s.owl % NW == 0) 1 else -1, 0).cloudTiles().toList()) + // One of each a level, Sleepy Time only where there are clouds. + assertEquals(0, NightPower.SleepyTime.forLevel(NightChapters[0].levels[0])); assertEquals(1, NightPower.SleepyTime.forLevel(lv)) + } }