Orbiteroids (0.116.0, versionCode 306): Zip clears the sky, Scheme A, five power-ups that switch on when picked up, bot-balanced, zero allocation

- Sectors of rocks, tellies, satellites, interference clouds, a rogue beacon and the cosmic anomaly; Drift to find
- Hull and shield bars; title, play, sector card (waits for OK) and shared result from Stage B frames 50-53
- OrbiteroidsSimTest: rules, a three-player balance bot on each difficulty, 0 bytes over 10,000 steps
- All five Originals now play

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 10:31:14 +00:00
co-authored by Claude Opus 5.5
parent d375aae621
commit dd91e87001
10 changed files with 980 additions and 16 deletions
@@ -91,9 +91,11 @@ internal object ArcadeGames {
listOf(Control("◀▶▲▼", "Hop"), Control("OK", "Turn the beam"), BackPause), "Light the mast. No timer and no fail: just Nite Owl's par.") {
tv.orbit.classic.arcade.nightshift.NightShiftScreen(it)
}
// Playable from 9 November 2026: tv.orbit.classic.arcade.orbiteroids (Scheme A, Paul's choice).
val Orbiteroids: ArcadeGame = GameDef("Orbiteroids", "Orbiteroids", "Zip clears the sky", "Fly Zip through the debris and keep the sky clear.", Mascot.Zip,
kidsSuitable = true, original = true, launchable = false, ScoreKind.Points, keepsLevels = false, legacyRecordKey = null,
listOf(Control("◀▶", "Turn"), Control("▲", "Thrust"), Control("OK", "Fire"), BackPause), "Clear each sector, and watch your hull.")
kidsSuitable = true, original = true, launchable = true, ScoreKind.Points, keepsLevels = false, legacyRecordKey = null,
listOf(Control("◀▶▲▼", "Fly that way: Zip aims and fires"), BackPause), "Clear each sector, and watch your hull. Fly through power-ups to switch them on.",
listOf(GameSetting("sounds", "Sound effects"))) { tv.orbit.classic.arcade.orbiteroids.OrbiteroidsScreen(it) }
// Classics: the eight Kotlin games, unchanged, each in a thin wrapper.
val OrbMan: ArcadeGame = GameDef("OrbMan", "Star Chase", "Maze chase starring Ollie", "Help Ollie clear the stars, and dodge Blaze, Zip, Drift and Luna.", Mascot.Ollie,
@@ -0,0 +1,405 @@
package tv.orbit.classic.arcade.orbiteroids
import android.view.KeyEvent
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.rotateRad
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
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
import tv.orbit.classic.Anton
import tv.orbit.classic.Motion
import tv.orbit.classic.OrbCharacter
import tv.orbit.classic.OrbDir
import tv.orbit.classic.R
import tv.orbit.classic.arcade.ArcadeButton
import tv.orbit.classic.arcade.ArcadeColours
import tv.orbit.classic.arcade.ArcadeGameLoop
import tv.orbit.classic.arcade.ArcadeGames
import tv.orbit.classic.arcade.ArcadeKey
import tv.orbit.classic.arcade.ArcadePauseMenu
import tv.orbit.classic.arcade.ArcadePlay
import tv.orbit.classic.arcade.ClassicResult
import tv.orbit.classic.arcade.Control
import tv.orbit.classic.arcade.HowToPlayCard
import tv.orbit.classic.arcade.KeyCap
import tv.orbit.classic.arcade.KeyFooter
import tv.orbit.classic.arcade.Mascot
import tv.orbit.classic.arcade.PauseWhenHidden
import tv.orbit.classic.arcade.RunEnd
import tv.orbit.classic.arcade.RunTracker
import tv.orbit.classic.arcade.SpeechBubble
import tv.orbit.classic.arcade.arcadeKey
import tv.orbit.classic.arcade.backStep
import tv.orbit.classic.arcade.BackStep
import tv.orbit.classic.arcade.onBackKey
import tv.orbit.classic.arcade.previousLine
import tv.orbit.classic.arcade.raiders.Pilot
import tv.orbit.classic.arcade.raiders.pilotFace
import tv.orbit.classic.drawOrbChaser
import tv.orbit.classic.starPath
/** Orbiteroids' colours (frames 50–53). */
internal object OColours {
val Space = Color(0xFF0A1236)
val Rock = Color(0xFF8A93A8); val RockDark = Color(0xFF5E6680)
val Hull = Color(0xFF4CD98A); val Shield = Color(0xFF35D6F2); val Empty = Color(0xFF1C2A5A)
val Shot = Color(0xFFFF6FB5)
val Pink = Color(0xFFFF4FA3); val Body = Color(0xFFF1F3FA); val Flame = Color(0xFFFFC23D)
val Beacon = Color(0xFFFF5A4F); val Anomaly = Color(0xFFA77BFF); val Static = Color(0xFFB4BCD6)
fun power(p: OPower) = when (p) { OPower.OrbitShield -> Color(0xFF22C3D8); OPower.MagnetPulse -> Color(0xFFFF5A4F); OPower.SlowOrbit -> Color(0xFF7B7BFF); OPower.SplitBeam -> Color(0xFFFF9F1C); OPower.SignalRepair -> Color(0xFF4CD98A) }
}
/** The shapes, made once: rocks, the rocket, the satellite and the TV, each at a unit size and scaled when drawn. */
internal class OrbiteroidsArt(val zip: ImageBitmap) {
val rock = Path().apply {
val pts = floatArrayOf(1f, .82f, .95f, .78f, 1f, .86f, .92f, .8f)
for (k in pts.indices) { val a = k * 6.2832f / pts.size; val r = pts[k]; val x = kotlin.math.sin(a) * r; val y = -kotlin.math.cos(a) * r; if (k == 0) moveTo(x, y) else lineTo(x, y) }
close()
}
val body = Path().apply { moveTo(0f, -16f); cubicTo(7f, -10f, 7f, 4f, 5f, 10f); lineTo(-5f, 10f); cubicTo(-7f, 4f, -7f, -10f, 0f, -16f); close() }
val nose = Path().apply { moveTo(0f, -16f); cubicTo(3.6f, -13f, 5f, -11f, 5.6f, -9f); lineTo(-5.6f, -9f); cubicTo(-5f, -11f, -3.6f, -13f, 0f, -16f); close() }
val finL = Path().apply { moveTo(-5f, 0f); lineTo(-13f, 8f); lineTo(-13f, 11f); lineTo(-5f, 8f); close() }
val finR = Path().apply { moveTo(5f, 0f); lineTo(13f, 8f); lineTo(13f, 11f); lineTo(5f, 8f); close() }
val flame = Path().apply { moveTo(-3f, 10f); quadraticTo(0f, 22f, 3f, 10f); close() }
val hex = Path().apply { for (k in 0..5) { val a = Math.toRadians(60.0 * k + 30); val x = kotlin.math.cos(a).toFloat(); val y = kotlin.math.sin(a).toFloat(); if (k == 0) moveTo(x, y) else lineTo(x, y) }; close() }
val dash = Stroke(1.5f, pathEffect = PathEffect.dashPathEffect(floatArrayOf(5f, 5f)))
val thin = Stroke(1.5f); val mid = Stroke(2f, cap = StrokeCap.Round); val wide = Stroke(3f, cap = StrokeCap.Round)
val zipSize = IntSize(zip.width, zip.height)
fun DrawScope.ship(x: Float, y: Float, angle: Float, hurt: Boolean, flicker: Float) {
translate(x, y) {
rotateRad(angle, Offset.Zero) {
scale(1.2f, 1.2f, Offset.Zero) {
drawPath(flame, OColours.Flame, alpha = .8f + .2f * flicker)
drawPath(finL, OColours.Pink); drawPath(finR, OColours.Pink)
drawPath(body, if (hurt) Color(0xFFFFB3B3) else OColours.Body)
drawPath(nose, OColours.Pink)
drawCircle(Color(0xFF1B2A4E), 4.6f, Offset(0f, -3f))
drawImage(zip, IntOffset.Zero, zipSize, IntOffset(-4, -7), IntSize(8, 8))
}
}
}
}
}
private enum class OStage { Title, Play, Result }
/** Orbiteroids' HUD numbers, read once a frame from the sim; Compose only redraws what changed. */
private class OHud {
var score by mutableIntStateOf(0); var sector by mutableIntStateOf(1); var hull by mutableIntStateOf(5); var shield by mutableIntStateOf(5)
var bounces by mutableIntStateOf(0); var powerSecs by mutableIntStateOf(0); var between by mutableStateOf(false)
var power by mutableStateOf<OPower?>(null); var left by mutableIntStateOf(0)
var frame by mutableIntStateOf(0)
fun read(s: OrbiteroidsSim) {
score = s.score; sector = s.sector; hull = s.hull; shield = s.shield; bounces = s.bounces; between = s.between; left = s.left()
power = s.power ?: s.lastPower?.takeIf { s.lastPowerFor > 0f }
powerSecs = kotlin.math.ceil(s.powerLeft).toInt()
frame++
}
}
@Composable
internal fun OrbiteroidsScreen(play: ArcadePlay) {
val gameDef = ArcadeGames.Orbiteroids
val latestPlay by rememberUpdatedState(play)
val calm = Motion.reduce
val density = LocalDensity.current
val art = remember(density) { OrbiteroidsArt(pilotFace(Pilot.Zip, 96, density)) }
val sim = remember { OrbiteroidsSim(play.difficulty) }
val hud = remember { OHud() }
val stepper = remember { tv.orbit.classic.arcade.raiders.FixedStep() }
var stage by remember { mutableStateOf(OStage.Title) }
var paused by remember { mutableStateOf(false) }
var howTo by remember { mutableStateOf(false) }
var soundsOn by remember { mutableStateOf(play.setting("sounds")) }
var bestBefore by remember { mutableIntStateOf(play.best) }
val tracker = remember { RunTracker { end -> latestPlay.onRunEnd(end) } }
fun endRun(finished: Boolean) { tracker.end(RunEnd(sim.score, finished, "Sector ${sim.sector}")) }
DisposableEffect(Unit) { onDispose { endRun(false) } }
LaunchedEffect(Unit) { play.audio.preload(R.raw.power_wide_orbit, R.raw.power_zip_split) }
fun startRun() {
bestBefore = maxOf(latestPlay.best, bestBefore)
sim.newRun(latestPlay.difficulty, System.nanoTime())
stepper.reset(); hud.read(sim); tracker.begin(); paused = false; stage = OStage.Play
}
fun pause() { if (stage == OStage.Play && !paused) { paused = true; sim.held = 0; stepper.reset() } }
fun leave() { endRun(sim.over); latestPlay.onLeave() }
PauseWhenHidden(stage == OStage.Play && !paused) { pause() }
ArcadeGameLoop(running = stage == OStage.Play && !paused) { _, dt ->
val n = stepper.ticks(dt, paused)
var ev = 0
for (i in 0 until n) { sim.step(); ev = ev or sim.events }
if (ev and (OEvents.POWER or OEvents.DRIFT) != 0) latestPlay.audio.cue(R.raw.power_zip_split, .6f, soundsOn)
if (ev and OEvents.SECTOR != 0) latestPlay.audio.cue(R.raw.power_wide_orbit, .6f, soundsOn)
hud.read(sim)
if (sim.over) { endRun(true); stage = OStage.Result }
}
val playFocus = remember { FocusRequester() }
LaunchedEffect(stage, paused, hud.between) { if (stage == OStage.Play && !paused && !hud.between) runCatching { playFocus.requestFocus() } }
BackHandler(stage == OStage.Play && !paused) { pause() }
when (stage) {
OStage.Title -> {
OTitle(art, play.best, play.difficulty.title, refocus = !howTo, onPlay = ::startRun, onHowTo = { howTo = true }, onBack = { latestPlay.onLeave() })
if (howTo) HowToPlayCard(gameDef, firstRun = false, everyTime = null, onEveryTime = {}, onPlay = { howTo = false }, onBack = { howTo = false })
}
OStage.Play, OStage.Result -> {
Box(Modifier.fillMaxSize().focusRequester(playFocus).focusable().onPreviewKeyEvent { e ->
if (stage != OStage.Play || paused || hud.between) return@onPreviewKeyEvent false
val n = e.nativeKeyEvent
val key = arcadeKey(n.keyCode)
val down = n.action == KeyEvent.ACTION_DOWN
// Back by context: nothing is ever held in Orbiteroids, so Back (and play/pause) pause.
when (backStep(key, down, n.repeatCount, active = false)) { BackStep.Pause -> { pause(); return@onPreviewKeyEvent true }; BackStep.Cancel -> return@onPreviewKeyEvent true; BackStep.None -> {} }
if (key == ArcadeKey.Back || key == ArcadeKey.PlayPause) return@onPreviewKeyEvent true
// Scheme A: hold an arrow to fly that way (a bit each, up, right, down, left).
val bit = when (key) { ArcadeKey.Up -> 1; ArcadeKey.Right -> 2; ArcadeKey.Down -> 4; ArcadeKey.Left -> 8; else -> 0 }
if (bit == 0) return@onPreviewKeyEvent key == ArcadeKey.Ok
sim.held = if (down) sim.held or bit else sim.held and bit.inv()
true
}) {
OPlay(art, sim, hud, bestBefore, calm, play.reduceFlashing)
if (hud.between && stage == OStage.Play && !paused) SectorCard(hud.sector, onNext = { sim.nextSector(); stepper.reset(); hud.read(sim) })
if (stage == OStage.Result) ClassicResult(gameDef, "ORBITEROIDS · SECTOR ${sim.sector} REACHED", "SCORE", "%,d".format(sim.score),
newBest = sim.score > bestBefore && sim.score > 0, previous = previousLine(gameDef.scoreKind, bestBefore, gameDef.title), detail = orbiteroidsLine(sim),
cheer = Mascot.Blaze, onAgain = { endRun(sim.over); startRun() }, onBack = ::leave)
}
if (paused && stage == OStage.Play) ArcadePauseMenu(gameDef, "Orbiteroids · Sector ${sim.sector} · ${"%,d".format(sim.score)}",
onResume = { paused = false; stepper.reset() }, onRestart = { endRun(false); startRun() }, onExit = ::leave,
sound = if (soundsOn) "Effects on" else "Effects off", onSound = { soundsOn = !soundsOn; latestPlay.setSetting("sounds", soundsOn) })
}
}
}
// ---------- Title (frame 50) ----------
@Composable
private fun OTitle(art: OrbiteroidsArt, best: Int, difficulty: String, refocus: Boolean, onPlay: () -> Unit, onHowTo: () -> Unit, onBack: () -> Unit) {
val first = remember { FocusRequester() }
LaunchedEffect(refocus) { if (refocus) runCatching { first.requestFocus() } }
Box(Modifier.fillMaxSize().onBackKey(action = onBack).spaceSky()) {
BackHandler(onBack = onBack)
Canvas(Modifier.fillMaxSize()) {
val w = size.width; val h = size.height
drawCircle(Color(0xFF5865D8), h * .25f, Offset(w * .97f, h * 1.02f))
drawCircle(Color(0xFF2A1C4E), h * .26f, Offset(w * .34f, h * .55f))
for ((x, y, r) in listOf(Triple(.09f, .48f, .035f), Triple(.61f, .42f, .03f), Triple(.27f, .74f, .02f), Triple(.55f, .78f, .022f), Triple(.57f, .12f, .02f)))
translate(w * x, h * y) { scale(h * r, h * r, Offset.Zero) { drawPath(art.rock, OColours.Rock) } }
}
// Zip's rocket, large, over its planet.
Canvas(Modifier.fillMaxSize()) { val w = size.width; val h = size.height; translate(w * .34f, h * .55f) { scale(h / 90f, h / 90f, Offset.Zero) { with(art) { ship(0f, 0f, .6f, false, 1f) } } } }
Column(Modifier.fillMaxSize().padding(start = 48.dp, end = 48.dp, top = 24.dp, bottom = 12.dp)) {
Row(Modifier.fillMaxWidth().weight(1f)) {
Column(Modifier.weight(1f)) {
Text("AN ORBIT ARCADE ORIGINAL", Modifier.padding(start = 90.dp), color = ArcadeColours.Muted, fontSize = 13.sp, letterSpacing = 5.sp)
Text("ORBITEROIDS", Modifier.padding(start = 60.dp), color = Color(0xFFF5F7FF), fontFamily = Anton, fontSize = 70.sp,
style = TextStyle(shadow = Shadow(ArcadeColours.Cyan, Offset(5f, 5f), 0f)))
Spacer(Modifier.weight(1f))
Row { Text("Pilot: Zip", color = ArcadeColours.Ink, fontSize = 14.sp, fontWeight = FontWeight.Bold); Text(" · Controls: Scheme A, fly and it fires for you", color = ArcadeColours.Muted, fontSize = 14.sp) }
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.padding(top = 4.dp)) {
Text("Your best ", color = ArcadeColours.Muted, fontSize = 14.sp); Text(if (best > 0) "%,d".format(best) else "Not played yet", color = ArcadeColours.Ink, fontSize = 14.sp, fontWeight = FontWeight.Bold)
Text(difficulty, Modifier.background(ArcadeColours.Cyan, RoundedCornerShape(10.dp)).padding(horizontal = 8.dp), color = ArcadeColours.Night, fontSize = 12.sp, fontWeight = FontWeight.Bold)
}
}
Column(horizontalAlignment = Alignment.End) {
Row(verticalAlignment = Alignment.CenterVertically) {
SpeechBubble("Rocks ahead.\nLet's fly!")
Canvas(Modifier.size(76.dp)) { drawOrbChaser(center, size.minDimension * .4f, OrbCharacter.Zip, OrbDir.Left, false, false, false, 0f) }
}
Spacer(Modifier.weight(1f))
Column(Modifier.width(230.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
ArcadeButton("Play", Modifier.fillMaxWidth().focusRequester(first), icon = "▶", primary = true, height = 34.dp, onClick = onPlay)
ArcadeButton("How to Play", Modifier.fillMaxWidth(), icon = "?", height = 34.dp, onClick = onHowTo)
ArcadeButton("Back to Arcade", Modifier.fillMaxWidth(), icon = "←", height = 34.dp, onClick = onBack)
}
Spacer(Modifier.weight(1f))
}
}
KeyFooter(listOf(Control("▲▼", "Choose"), Control("OK", "Select"), Control("Back", "Arcade home")))
}
}
}
/** Deep space behind everything: a navy field of coloured pinpricks, drawn once in its own layer. */
private fun Modifier.spaceSky(): Modifier = graphicsLayer().drawWithCache {
val rnd = java.util.Random(55)
val n = 140
val pts = FloatArray(n * 3) { k -> when (k % 3) { 0 -> rnd.nextFloat() * size.width; 1 -> rnd.nextFloat() * size.height; else -> rnd.nextFloat() } }
val tints = arrayOf(Color.White, Color(0xFFFF4FA3), Color(0xFF35D6F2), Color(0xFFFFDE59))
onDrawBehind {
drawRect(OColours.Space)
for (i in 0 until n) drawCircle(tints[(pts[i * 3 + 2] * 10).toInt() % 4].copy(alpha = .35f + pts[i * 3 + 2] * .5f), .8f + pts[i * 3 + 2] * 1.4f, Offset(pts[i * 3], pts[i * 3 + 1]))
}
}
// ---------- Play (frame 51) ----------
@Composable
private fun OPlay(art: OrbiteroidsArt, sim: OrbiteroidsSim, hud: OHud, best: Int, calm: Boolean, reduceFlashing: Boolean) {
Box(Modifier.fillMaxSize().spaceSky()) {
Canvas(Modifier.fillMaxSize()) {
hud.frame // a redraw each frame, without recomposing
val k = minOf(size.width / OW, size.height / OH)
val ox = (size.width - OW * k) / 2; val oy = (size.height - OH * k) / 2
translate(ox, oy) { scale(k, k, Offset.Zero) { drawField(art, sim, calm, reduceFlashing) } }
}
Column(Modifier.fillMaxSize().padding(start = 32.dp, end = 32.dp, top = 22.dp, bottom = 10.dp)) {
Row(Modifier.fillMaxWidth().weight(1f)) {
Column(Modifier.weight(1f)) {
Row(horizontalArrangement = Arrangement.spacedBy(28.dp)) {
Column { OLabel("SCORE"); Text("%,d".format(hud.score), color = ArcadeColours.Ink, fontFamily = Anton, fontSize = 30.sp) }
Column { OLabel("SECTOR"); Text("${hud.sector}", color = ArcadeColours.Ink, fontFamily = Anton, fontSize = 30.sp) }
}
OLabel("BEST"); Text("%,d".format(maxOf(best, hud.score)), color = ArcadeColours.Ink, fontFamily = Anton, fontSize = 24.sp)
Text("${hud.left} to clear", color = ArcadeColours.Muted, fontSize = 14.sp)
}
Column(horizontalAlignment = Alignment.End, verticalArrangement = Arrangement.spacedBy(4.dp)) {
OLabel("HULL"); Bars(hud.hull, OrbiteroidsSim.HULL, OColours.Hull)
OLabel("SHIELD"); Bars(hud.shield, OrbiteroidsSim.SHIELD, OColours.Shield)
if (hud.bounces > 0) Text("Orbit Shield ×${hud.bounces}", color = OColours.power(OPower.OrbitShield), fontSize = 14.sp, fontWeight = FontWeight.SemiBold)
Spacer(Modifier.height(6.dp))
OLabel("POWER-UP")
val p = hud.power
Canvas(Modifier.size(48.dp)) { if (p != null) badge(art, p, center, size.minDimension * .4f) else drawCircle(OColours.Empty, size.minDimension * .4f, style = Stroke(2f)) }
if (p != null) {
Text(p.title, color = ArcadeColours.Ink, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, textAlign = TextAlign.End)
Text(if (p.seconds > 0f && hud.powerSecs > 0) "${hud.powerSecs} s left" else p.line, color = OColours.power(p), fontSize = 13.sp, textAlign = TextAlign.End)
} else Text("Fly through one", color = ArcadeColours.Muted, fontSize = 13.sp)
}
}
KeyFooter(listOf(Control("◀▶▲▼", "Fly"), Control("Back", "Pause")), note = "Scheme A · aims and fires for you")
}
}
}
@Composable
private fun OLabel(text: String) = Text(text, color = ArcadeColours.Muted, fontSize = 16.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 2.sp)
@Composable
private fun Bars(n: Int, of: Int, colour: Color) = Row(horizontalArrangement = Arrangement.spacedBy(3.dp)) {
for (i in 0 until of) Box(Modifier.size(24.dp, 10.dp).background(if (i < n) colour else OColours.Empty, RoundedCornerShape(3.dp)))
}
private fun DrawScope.badge(art: OrbiteroidsArt, p: OPower, c: Offset, r: Float) {
translate(c.x, c.y) { scale(r, r, Offset.Zero) { drawPath(art.hex, OColours.power(p)) } }
drawCircle(Color.White.copy(alpha = .9f), r * .3f, c, style = Stroke(r * .12f))
when (p) {
OPower.SplitBeam -> for (a in floatArrayOf(-.4f, 0f, .4f)) drawLine(Color.White, c + Offset(0f, r * .3f), c + Offset(kotlin.math.sin(a) * r * .55f, -kotlin.math.cos(a) * r * .55f), r * .1f)
OPower.SignalRepair -> { drawLine(Color.White, c - Offset(r * .25f, 0f), c + Offset(r * .25f, 0f), r * .12f); drawLine(Color.White, c - Offset(0f, r * .25f), c + Offset(0f, r * .25f), r * .12f) }
else -> {}
}
}
/** The field: hazards, Drift, pickups, shots and Zip's rocket, in field units. */
private fun DrawScope.drawField(art: OrbiteroidsArt, s: OrbiteroidsSim, calm: Boolean, reduceFlashing: Boolean) {
val t = s.clock
for (i in 0 until OrbiteroidsSim.MAX_HZ) {
if (!s.hzOn[i]) continue
val k = s.hzKind[i]; val x = s.hzX[i]; val y = s.hzY[i]; val r = Hz.radius[k]
val hitTint = s.hzTint[i] > 0f
when (k) {
Hz.ROCK_L, Hz.ROCK_M, Hz.ROCK_S -> translate(x, y) { rotateRad(if (calm) 0f else s.hzSpin[i], Offset.Zero) { scale(r, r, Offset.Zero) {
drawPath(art.rock, if (hitTint) Color.White else OColours.Rock); drawCircle(OColours.RockDark, .18f, Offset(.25f, .1f)); drawCircle(OColours.RockDark, .12f, Offset(-.3f, -.3f)) } } }
Hz.TV -> translate(x, y) { rotateRad(if (calm) .3f else s.hzSpin[i] * .5f, Offset.Zero) {
drawRoundRect(if (hitTint) Color.White else OColours.Static, Offset(-r, -r * .7f), Size(r * 2, r * 1.4f), androidx.compose.ui.geometry.CornerRadius(r * .2f), style = art.mid)
drawLine(OColours.Static, Offset(-r * .5f, -r * .3f), Offset(r * .2f, r * .3f), 1.5f); drawLine(OColours.Static, Offset(-r * .2f, -r * .7f), Offset(-r * .5f, -r * 1.1f), 1.5f) } }
Hz.SAT, Hz.SAT_PART -> translate(x, y) { rotateRad(if (calm) 0f else s.hzSpin[i] * .4f, Offset.Zero) {
val w = if (k == Hz.SAT) r else r * .7f
drawRect(Color(0xFF8D95AE), Offset(-w * .35f, -w * .5f), Size(w * .7f, w))
drawRect(Color(0xFF3F6FD8), Offset(-w * 1.15f, -w * .3f), Size(w * .7f, w * .6f)); if (k == Hz.SAT) drawRect(Color(0xFF3F6FD8), Offset(w * .45f, -w * .3f), Size(w * .7f, w * .6f))
drawCircle(Color.White, w * .12f, Offset(-w * .12f, -w * .1f)); drawCircle(Color.White, w * .12f, Offset(w * .14f, -w * .1f))
drawCircle(Color(0xFF14213D), w * .06f, Offset(-w * .12f, -w * .08f)); drawCircle(Color(0xFF14213D), w * .06f, Offset(w * .14f, -w * .08f)) } }
Hz.CLOUD -> {
val a = if (hitTint) .8f else .55f
drawCircle(OColours.Static.copy(alpha = a * .5f), r * .6f, Offset(x - r * .3f, y)); drawCircle(OColours.Static.copy(alpha = a * .5f), r * .7f, Offset(x + r * .2f, y - r * .2f))
drawCircle(OColours.Static.copy(alpha = a * .5f), r * .55f, Offset(x + r * .45f, y + r * .15f))
for (l in -2..2) drawLine(Color.White.copy(alpha = .5f), Offset(x - r * .5f, y + l * r * .15f), Offset(x + r * .5f, y + l * r * .15f + (if (calm) 0f else kotlin.math.sin(t * 6f + l) * 2f)), 1f)
}
Hz.BEACON -> {
for (ring in 1..3) drawCircle(OColours.Beacon.copy(alpha = .45f), ring * 40f + (if (calm) 0f else (t * 30f) % 40f), Offset(x, y), style = art.dash)
drawRect(Color(0xFF3A3F55), Offset(x - 5f, y - 4f), Size(10f, 16f)); drawCircle(if (hitTint) Color.White else OColours.Beacon, 6f, Offset(x, y - 6f))
}
Hz.ANOMALY -> {
drawCircle(OColours.Anomaly.copy(alpha = .18f), r * 2f, Offset(x, y))
for (arm in 0 until 3) drawArc(OColours.Anomaly, (if (calm) 0f else t * 90f) + arm * 120f, 100f, false, Offset(x - r, y - r), Size(r * 2, r * 2), style = art.wide)
drawCircle(Color(0xFF14213D), r * .25f, Offset(x, y)); drawCircle(OColours.Anomaly, r * .25f, Offset(x, y), style = art.mid)
}
Hz.DRIFT -> drawOrbChaser(Offset(x, y), r, OrbCharacter.Drift, OrbDir.Left, false, false, false, if (calm) 0f else t)
}
}
for (p in 0 until OrbiteroidsSim.MAX_PICKS) if (s.pickOn[p]) badge(art, OPower.entries[s.pickKind[p]], Offset(s.pickX[p], s.pickY[p]), 12f)
for (q in 0 until OrbiteroidsSim.MAX_SHOTS) if (s.shotOn[q]) {
val len = .018f
drawLine(OColours.Shot, Offset(s.shotX[q], s.shotY[q]), Offset(s.shotX[q] - s.shotVx[q] * len, s.shotY[q] - s.shotVy[q] * len), 3f, StrokeCap.Round)
}
// Zip's rocket, inside its shield ring while the shield holds; hurt, it tints (it never flashes the screen).
val hurt = s.hurtFor > 0f && (reduceFlashing || calm || (t * 10).toInt() % 2 == 0)
if (s.shield > 0 || s.bounces > 0) {
val c = if (s.bounces > 0) OColours.power(OPower.OrbitShield) else OColours.Shield
drawCircle(c.copy(alpha = .12f), 30f, Offset(s.shipX, s.shipY)); drawCircle(c.copy(alpha = .6f), 30f, Offset(s.shipX, s.shipY), style = art.thin)
}
with(art) { ship(s.shipX, s.shipY, s.shipAngle, hurt, if (calm) 1f else kotlin.math.sin(t * 30f)) }
}
// ---------- Between sectors ----------
/** A sector cleared: the bonus, and the next sector only when OK is pressed. */
@Composable
private fun SectorCard(sector: Int, onNext: () -> Unit) {
val first = remember { FocusRequester() }
LaunchedEffect(sector) { runCatching { first.requestFocus() } }
Box(Modifier.fillMaxSize().background(ArcadeColours.Night.copy(alpha = .55f)), contentAlignment = Alignment.Center) {
Column(Modifier.width(380.dp).background(ArcadeColours.PanelDeep, RoundedCornerShape(14.dp)).border(1.dp, ArcadeColours.Line, RoundedCornerShape(14.dp)).padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("SECTOR $sector CLEAR", color = ArcadeColours.Ink, fontFamily = Anton, fontSize = 32.sp)
Text("+${"%,d".format(1000 * sector)} sector bonus", color = ArcadeColours.Yellow, fontSize = 16.sp, fontWeight = FontWeight.SemiBold)
Text(sectorTeaser(sector + 1), color = ArcadeColours.Muted, fontSize = 14.sp, textAlign = TextAlign.Center)
ArcadeButton("On to sector ${sector + 1}", Modifier.width(240.dp).focusRequester(first), icon = "▶", primary = true, height = 34.dp, onClick = onNext)
}
}
}
/** What's new in the next sector, when something is. */
internal fun sectorTeaser(next: Int) = when (next) {
2 -> "Broken tellies are drifting about. And is that Drift, lost? Fly into him to bring him home."
3 -> "Satellites ahead: they split in two."
4 -> "An interference cloud: shoot it away, don't fly through it."
5 -> "A rogue beacon pulls you in. Five hits to silence it."
6 -> "A cosmic anomaly: it can't be cleared, so keep clear of it."
else -> "More rocks, a little quicker. The shield mends two bars."
}
@@ -0,0 +1,316 @@
package tv.orbit.classic.arcade.orbiteroids
import tv.orbit.classic.arcade.Difficulty
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt
/**
* Orbiteroids (Stage B frames 50–53, 9 November 2026): Zip clears the sky. Scheme A, Paul's choice: the arrows fly the ship
* that way (it turns for you), and it aims at the nearest hazard and fires on its own. Power-ups switch on when picked up.
* Sector by sector: clear every hazard that can be cleared and the next sector comes. Hull and shield bars, no lives.
*
* Fixed 60 Hz steps over pooled arrays: nothing is made while it runs (checked by OrbiteroidsSimTest). The field is 960 by
* 540 units, wrapping at the edges; the screen scales it.
*/
internal const val OW = 960f
internal const val OH = 540f
internal const val OSTEP = 1f / 60f
/** The hazards (frame 51), and Drift, lost in a cloud of himself: a friend to find, not to shoot. */
internal object Hz {
const val ROCK_L = 0; const val ROCK_M = 1; const val ROCK_S = 2; const val TV = 3; const val SAT = 4; const val SAT_PART = 5
const val CLOUD = 6; const val BEACON = 7; const val ANOMALY = 8; const val DRIFT = 9
val radius = floatArrayOf(26f, 16f, 9f, 13f, 22f, 12f, 42f, 14f, 30f, 16f)
val hits = intArrayOf(1, 1, 1, 1, 2, 1, 4, 5, 0, 0)
val points = intArrayOf(20, 50, 100, 150, 200, 100, 250, 300, 0, 500)
/** Counts towards clearing a sector (the anomaly can't be cleared, and Drift is a friend). */
fun clears(kind: Int) = kind != ANOMALY && kind != DRIFT
/** Something the guns aim at. */
fun target(kind: Int) = kind != ANOMALY && kind != DRIFT
}
/** Orbiteroids' power-ups (the Stage B sheet): each switches on when Zip flies through it. */
internal enum class OPower(val title: String, val line: String, val seconds: Float) {
OrbitShield("Orbit Shield", "Bounces off the next 3 hits", 0f),
MagnetPulse("Magnet Pulse", "Pushes rocks back, pulls pickups in", 0f),
SlowOrbit("Slow Orbit", "Everything else slows for 8 s", 8f),
SplitBeam("Split Beam", "Fires three ways for 12 s", 12f),
SignalRepair("Signal Repair", "Mends two hull bars", 0f)
}
/** What happened this step, for the sounds and the HUD (a bit each). */
internal object OEvents { const val SHOT = 1; const val HIT = 2; const val CLEARED = 4; const val HURT = 8; const val POWER = 16; const val SECTOR = 32; const val DRIFT = 64; const val OVER = 128 }
internal class OrbiteroidsSim(var difficulty: Difficulty = Difficulty.Normal, seed: Long = 1) {
private var rnd = java.util.Random(seed)
// ---------- the ship ----------
var shipX = OW / 2; var shipY = OH / 2; var shipVx = 0f; var shipVy = 0f
/** Where the nose points, in radians (0 is up). */
var shipAngle = 0f
var hull = HULL; var shield = SHIELD
/** Orbit Shield's hits left to bounce off. */
var bounces = 0
var hurtFor = 0f
private var sinceHurt = 10f
private var shieldTimer = 0f
private var fireIn = 0f
/** The arrows held: a bit each, up, right, down, left. */
var held = 0
// ---------- pools ----------
val hzKind = IntArray(MAX_HZ); val hzX = FloatArray(MAX_HZ); val hzY = FloatArray(MAX_HZ); val hzVx = FloatArray(MAX_HZ); val hzVy = FloatArray(MAX_HZ)
val hzHits = IntArray(MAX_HZ); val hzSpin = FloatArray(MAX_HZ); val hzOn = BooleanArray(MAX_HZ); val hzTint = FloatArray(MAX_HZ)
val shotX = FloatArray(MAX_SHOTS); val shotY = FloatArray(MAX_SHOTS); val shotVx = FloatArray(MAX_SHOTS); val shotVy = FloatArray(MAX_SHOTS)
val shotLife = FloatArray(MAX_SHOTS); val shotOn = BooleanArray(MAX_SHOTS)
val pickX = FloatArray(MAX_PICKS); val pickY = FloatArray(MAX_PICKS); val pickLife = FloatArray(MAX_PICKS); val pickKind = IntArray(MAX_PICKS); val pickOn = BooleanArray(MAX_PICKS)
// ---------- the run ----------
var score = 0; var sector = 1; var clock = 0f
var over = false
/** Between sectors: the "Sector clear" card waits for OK. */
var between = false
/** The power-up on now (Slow Orbit, Split Beam) and its seconds left. */
var power: OPower? = null; var powerLeft = 0f
/** The last power-up picked up, for the HUD's badge (Shield, Pulse and Repair act at once). */
var lastPower: OPower? = null; var lastPowerFor = 0f
// Tallies for the result card (frame 53).
var rocksCleared = 0; var satellitesSplit = 0; var driftFound = 0
var events = 0
/** How fast the sky moves: by difficulty, and a little quicker every sector. */
val pace get() = when (difficulty) { Difficulty.Gentle -> .75f; Difficulty.Normal -> 1f; Difficulty.Tricky -> 1.25f } * (1f + .07f * (sector - 1))
fun newRun(d: Difficulty, seed: Long) {
difficulty = d; rnd = java.util.Random(seed)
shipX = OW / 2; shipY = OH / 2; shipVx = 0f; shipVy = 0f; shipAngle = 0f
hull = HULL; shield = SHIELD; bounces = 0; hurtFor = 0f; sinceHurt = 10f; shieldTimer = 0f; fireIn = 0f; held = 0
hzOn.fill(false); shotOn.fill(false); pickOn.fill(false)
score = 0; sector = 1; clock = 0f; over = false; between = false; power = null; powerLeft = 0f; lastPower = null; lastPowerFor = 0f
rocksCleared = 0; satellitesSplit = 0; driftFound = 0; events = 0
startSector()
}
/** OK on the "Sector clear" card: on to the next. */
fun nextSector() { if (!between) return; between = false; sector++; startSector() }
/** A sector's sky: more rocks each time, and a new kind of trouble every sector or so. */
private fun startSector() {
hzOn.fill(false); shotOn.fill(false)
// The shield mends two bars between sectors (full at the start of a run).
shield = if (sector == 1) SHIELD else minOf(SHIELD, shield + 2); shieldTimer = 0f
val rocks = 3 + sector + sector / 2 + (if (difficulty == Difficulty.Tricky) 1 else 0) - (if (difficulty == Difficulty.Gentle) 1 else 0)
repeat(rocks) { spawnEdge(Hz.ROCK_L) }
if (sector >= 2) repeat(1 + sector / 3) { spawnEdge(Hz.TV) }
if (sector >= 3) repeat(1 + (sector - 3) / 2) { spawnEdge(Hz.SAT) }
if (sector >= 4) spawnEdge(Hz.CLOUD)
if (sector >= 5) spawnEdge(Hz.BEACON)
if (sector >= 6) spawnEdge(Hz.ANOMALY)
if (sector >= 2 && sector % 2 == 0) spawnEdge(Hz.DRIFT)
}
private fun free(on: BooleanArray): Int { for (i in on.indices) if (!on[i]) return i; return -1 }
/** A hazard coming in from an edge, well away from the ship, drifting across. */
private fun spawnEdge(kind: Int) {
var x: Float; var y: Float
do { x = rnd.nextFloat() * OW; y = rnd.nextFloat() * OH } while (dist2(x, y, shipX, shipY) < 220f * 220f)
val speed = when (kind) { Hz.ROCK_L -> 55f; Hz.TV -> 60f; Hz.SAT -> 45f; Hz.CLOUD -> 18f; Hz.BEACON -> 22f; Hz.ANOMALY -> 14f; Hz.DRIFT -> 34f; else -> 60f } * (.7f + rnd.nextFloat() * .6f)
val a = rnd.nextFloat() * 6.2832f
spawn(kind, x, y, cos(a) * speed, sin(a) * speed)
}
private fun spawn(kind: Int, x: Float, y: Float, vx: Float, vy: Float) {
val i = free(hzOn); if (i < 0) return
hzOn[i] = true; hzKind[i] = kind; hzX[i] = x; hzY[i] = y; hzVx[i] = vx; hzVy[i] = vy; hzHits[i] = Hz.hits[kind]; hzSpin[i] = rnd.nextFloat() * 6.28f; hzTint[i] = 0f
}
private fun dist2(ax: Float, ay: Float, bx: Float, by: Float): Float {
var dx = ax - bx; var dy = ay - by
if (dx > OW / 2) dx -= OW; if (dx < -OW / 2) dx += OW
if (dy > OH / 2) dy -= OH; if (dy < -OH / 2) dy += OH
return dx * dx + dy * dy
}
private fun wrapX(x: Float) = if (x < 0f) x + OW else if (x >= OW) x - OW else x
private fun wrapY(y: Float) = if (y < 0f) y + OH else if (y >= OH) y - OH else y
fun step() {
events = 0
if (over || between) return
val dt = OSTEP
clock += dt
val slow = if (power == OPower.SlowOrbit) .4f else 1f
// ---- the ship: Scheme A, fly the way the arrows say; the nose follows ----
var ax = 0f; var ay = 0f
if (held and 1 != 0) ay -= 1f; if (held and 4 != 0) ay += 1f; if (held and 8 != 0) ax -= 1f; if (held and 2 != 0) ax += 1f
val len = sqrt(ax * ax + ay * ay)
if (len > 0f) { shipVx += ax / len * THRUST * dt; shipVy += ay / len * THRUST * dt }
val drag = 1f - DRAG * dt
shipVx *= drag; shipVy *= drag
val sp = sqrt(shipVx * shipVx + shipVy * shipVy)
if (sp > MAX_SPEED) { shipVx *= MAX_SPEED / sp; shipVy *= MAX_SPEED / sp }
shipX = wrapX(shipX + shipVx * dt); shipY = wrapY(shipY + shipVy * dt)
if (sp > 12f) {
val want = atan2(shipVx, -shipVy)
var d = want - shipAngle
while (d > Math.PI) d -= (2 * Math.PI).toFloat(); while (d < -Math.PI) d += (2 * Math.PI).toFloat()
shipAngle += d * minOf(1f, 10f * dt)
}
// ---- pulls: the rogue beacon's rings and the anomaly ----
for (i in 0 until MAX_HZ) {
if (!hzOn[i]) continue
val k = hzKind[i]
if (k == Hz.BEACON || k == Hz.ANOMALY) {
val reach = if (k == Hz.BEACON) 170f else 210f
val d2 = dist2(hzX[i], hzY[i], shipX, shipY)
if (d2 < reach * reach && d2 > 1f) {
var dx = hzX[i] - shipX; var dy = hzY[i] - shipY
if (dx > OW / 2) dx -= OW; if (dx < -OW / 2) dx += OW; if (dy > OH / 2) dy -= OH; if (dy < -OH / 2) dy += OH
val d = sqrt(d2); val pull = (if (k == Hz.BEACON) 150f else 190f) * pace * (1f - d / reach)
shipVx += dx / d * pull * dt; shipVy += dy / d * pull * dt
}
}
}
// ---- the guns: at the nearest hazard in range, on their own ----
fireIn -= dt
if (fireIn <= 0f) {
var best = -1; var bestD = RANGE * RANGE
for (i in 0 until MAX_HZ) if (hzOn[i] && Hz.target(hzKind[i])) { val d2 = dist2(hzX[i], hzY[i], shipX, shipY); if (d2 < bestD) { bestD = d2; best = i } }
if (best >= 0) {
var dx = hzX[best] - shipX; var dy = hzY[best] - shipY
if (dx > OW / 2) dx -= OW; if (dx < -OW / 2) dx += OW; if (dy > OH / 2) dy -= OH; if (dy < -OH / 2) dy += OH
val a = atan2(dx, -dy)
fire(a)
if (power == OPower.SplitBeam) { fire(a - .22f); fire(a + .22f) }
fireIn = FIRE_GAP; events = events or OEvents.SHOT
}
}
// ---- shots ----
for (s in 0 until MAX_SHOTS) {
if (!shotOn[s]) continue
shotX[s] = wrapX(shotX[s] + shotVx[s] * dt); shotY[s] = wrapY(shotY[s] + shotVy[s] * dt)
shotLife[s] -= dt
if (shotLife[s] <= 0f) { shotOn[s] = false; continue }
for (i in 0 until MAX_HZ) {
if (!hzOn[i] || !Hz.target(hzKind[i])) continue
val r = Hz.radius[hzKind[i]]
if (dist2(shotX[s], shotY[s], hzX[i], hzY[i]) < r * r) { shotOn[s] = false; hit(i); break }
}
}
// ---- hazards move, and meet the ship ----
val move = pace * slow
for (i in 0 until MAX_HZ) {
if (!hzOn[i]) continue
hzX[i] = wrapX(hzX[i] + hzVx[i] * move * dt); hzY[i] = wrapY(hzY[i] + hzVy[i] * move * dt)
hzSpin[i] += dt * .8f * move
if (hzTint[i] > 0f) hzTint[i] -= dt
val k = hzKind[i]
val r = Hz.radius[k] + SHIP_R
if (dist2(hzX[i], hzY[i], shipX, shipY) >= r * r) continue
when (k) {
Hz.DRIFT -> { hzOn[i] = false; score += Hz.points[k]; driftFound++; events = events or OEvents.DRIFT }
Hz.CLOUD -> if (hurtFor <= 0f) hurt(.6f) // inside the interference: a bar every so often
else -> if (hurtFor <= 0f) {
hurt(1f)
// Knocked away from what it met.
var dx = shipX - hzX[i]; var dy = shipY - hzY[i]
if (dx > OW / 2) dx -= OW; if (dx < -OW / 2) dx += OW; if (dy > OH / 2) dy -= OH; if (dy < -OH / 2) dy += OH
val d = sqrt(dx * dx + dy * dy).coerceAtLeast(1f)
shipVx = dx / d * 180f; shipVy = dy / d * 180f
if (k != Hz.ANOMALY && k != Hz.BEACON) hit(i)
}
}
}
// ---- pickups: they drift slowly and fade; fly through one and it switches on ----
for (p in 0 until MAX_PICKS) {
if (!pickOn[p]) continue
pickLife[p] -= dt
if (pickLife[p] <= 0f) { pickOn[p] = false; continue }
if (dist2(pickX[p], pickY[p], shipX, shipY) < (SHIP_R + 14f) * (SHIP_R + 14f)) { pickOn[p] = false; apply(OPower.entries[pickKind[p]]) }
}
if (powerLeft > 0f) { powerLeft -= dt; if (powerLeft <= 0f) { power = null; powerLeft = 0f } }
if (lastPowerFor > 0f) lastPowerFor -= dt
if (hurtFor > 0f) hurtFor -= dt
// The shield is full again at each new sector (startSector); in a sector it only goes down.
sinceHurt += dt
// ---- a sector cleared ----
var left = 0
for (i in 0 until MAX_HZ) if (hzOn[i] && Hz.clears(hzKind[i])) left++
if (left == 0) { score += 1000 * sector; between = true; events = events or OEvents.SECTOR; hzOn.fill(false); shotOn.fill(false); pickOn.fill(false) }
}
private fun fire(a: Float) {
val s = free(shotOn); if (s < 0) return
shotOn[s] = true; shotX[s] = shipX + sin(a) * SHIP_R; shotY[s] = shipY - cos(a) * SHIP_R
shotVx[s] = sin(a) * SHOT_SPEED; shotVy[s] = -cos(a) * SHOT_SPEED; shotLife[s] = RANGE / SHOT_SPEED + .1f
}
/** A shot lands: off comes a hit; at none left it's cleared, splitting into smaller pieces where it does. */
private fun hit(i: Int) {
hzHits[i]--; hzTint[i] = .15f
events = events or OEvents.HIT
if (hzHits[i] > 0) return
val k = hzKind[i]; val x = hzX[i]; val y = hzY[i]
hzOn[i] = false
score += Hz.points[k]
events = events or OEvents.CLEARED
when (k) {
Hz.ROCK_L, Hz.ROCK_M -> { rocksCleared++; val small = if (k == Hz.ROCK_L) Hz.ROCK_M else Hz.ROCK_S; split(small, x, y, if (k == Hz.ROCK_L) 80f else 100f) }
Hz.ROCK_S -> rocksCleared++
Hz.SAT -> { satellitesSplit++; split(Hz.SAT_PART, x, y, 75f) }
}
// About one in twelve leaves a power-up behind.
if (rnd.nextInt(PICKUP_ONE_IN) == 0) { val p = free(pickOn); if (p >= 0) { pickOn[p] = true; pickX[p] = x; pickY[p] = y; pickLife[p] = 10f; pickKind[p] = rnd.nextInt(OPower.entries.size) } }
}
private fun split(kind: Int, x: Float, y: Float, speed: Float) {
val a = rnd.nextFloat() * 6.28f
spawn(kind, x, y, cos(a) * speed, sin(a) * speed)
spawn(kind, x, y, -cos(a) * speed, -sin(a) * speed)
}
/** Losing a bar: the shield first, then the hull; Orbit Shield bounces it off altogether. */
private fun hurt(safe: Float) {
hurtFor = safe; sinceHurt = 0f; shieldTimer = 0f
if (bounces > 0) { bounces--; return }
events = events or OEvents.HURT
if (shield > 0) shield-- else hull--
if (hull <= 0) { hull = 0; over = true; events = events or OEvents.OVER }
}
internal fun apply(p: OPower) {
events = events or OEvents.POWER
lastPower = p; lastPowerFor = 3f
when (p) {
OPower.OrbitShield -> bounces = 3
OPower.MagnetPulse -> {
for (i in 0 until MAX_HZ) if (hzOn[i]) {
var dx = hzX[i] - shipX; var dy = hzY[i] - shipY
if (dx > OW / 2) dx -= OW; if (dx < -OW / 2) dx += OW; if (dy > OH / 2) dy -= OH; if (dy < -OH / 2) dy += OH
val d = sqrt(dx * dx + dy * dy)
if (d < 240f && d > 1f) { hzVx[i] += dx / d * 140f; hzVy[i] += dy / d * 140f }
}
for (q in 0 until MAX_PICKS) if (pickOn[q]) { pickX[q] = shipX; pickY[q] = shipY }
}
OPower.SlowOrbit, OPower.SplitBeam -> { power = p; powerLeft = p.seconds }
OPower.SignalRepair -> hull = minOf(HULL, hull + 2)
}
}
/** Hazards still to clear this sector, for the HUD. */
fun left(): Int { var n = 0; for (i in 0 until MAX_HZ) if (hzOn[i] && Hz.clears(hzKind[i])) n++; return n }
companion object {
const val MAX_HZ = 64; const val MAX_SHOTS = 48; const val MAX_PICKS = 6
const val HULL = 5; const val SHIELD = 5
const val THRUST = 520f; const val DRAG = 1.4f; const val MAX_SPEED = 260f
const val SHIP_R = 13f; const val RANGE = 330f; const val SHOT_SPEED = 620f; const val FIRE_GAP = .32f
const val SHIELD_REGROW = 8f; const val PICKUP_ONE_IN = 12
}
}
/** Run end: points, with the sector reached. */
internal fun orbiteroidsLine(sim: OrbiteroidsSim) = "${sim.rocksCleared} rocks cleared · ${sim.satellitesSplit} satellites split" +
(if (sim.driftFound > 0) " · Drift found ×${sim.driftFound}" else "") + (if (sim.over) " · hull lost in sector ${sim.sector}" else "")