| 1 | package codewhale.pet |
| 2 | |
| 3 | import androidx.compose.foundation.Canvas |
| 4 | import androidx.compose.foundation.layout.fillMaxSize |
| 5 | import androidx.compose.runtime.Composable |
| 6 | import androidx.compose.ui.Modifier |
| 7 | import androidx.compose.ui.geometry.Offset |
| 8 | import androidx.compose.ui.graphics.Brush |
| 9 | import androidx.compose.ui.graphics.Color |
| 10 | import androidx.compose.ui.graphics.Path |
| 11 | import androidx.compose.ui.graphics.drawscope.Stroke |
| 12 | import androidx.compose.ui.semantics.contentDescription |
| 13 | import androidx.compose.ui.semantics.semantics |
| 14 | import kotlin.math.sin |
| 15 | |
| 16 | /** Only places the worker's immutable dots. No clock, world or score here. */ |
| 17 | @Composable |
| 18 | fun CodewhalePet(scene: PetScene, modifier: Modifier = Modifier) { |
| 19 | Canvas(modifier = modifier.fillMaxSize().semantics { contentDescription = petContentDescription(scene) }) { |
| 20 | val w = size.width; val h = size.height |
| 21 | val appearance = scene.appearance |
| 22 | fun color(c: List<Int>) = Color(c[0],c[1],c[2]) |
| 23 | drawRect(Brush.verticalGradient(appearance?.let { listOf(color(it.backgroundTop),color(it.background)) } ?: listOf(Color(0xff0b2029), Color(0xff071319)))) |
| 24 | if (appearance?.environment != false) { |
| 25 | val surfaceY = h * (1 + scene.surface) / 2 |
| 26 | val surface = Path().apply { |
| 27 | moveTo(0f, surfaceY) |
| 28 | for (i in 1..80) { |
| 29 | val x = i * w / 80 |
| 30 | lineTo(x, surfaceY + sin(i * 0.10).toFloat() * h * 0.008f) |
| 31 | } |
| 32 | } |
| 33 | drawPath(surface, Color(0xff709f9b), alpha = 0.22f, style = Stroke(1f)) |
| 34 | drawRect(Brush.verticalGradient(listOf(Color(0xff73c9b5).copy(alpha = scene.caustic * 0.025f), Color.Transparent)), |
| 35 | topLeft = Offset(0f, surfaceY)) |
| 36 | drawLine(Color(0xff264048), Offset(0f, h * 0.93f), Offset(w, h * 0.93f), 1f) |
| 37 | } |
| 38 | scene.food?.let { food -> |
| 39 | drawCircle(Color(0xffc2b787), 3f, Offset(w * (0.5f + food.x * 0.3f), h * (0.5f + food.y * 0.3f)), |
| 40 | alpha = food.life.coerceIn(0f, 1f)) |
| 41 | } |
| 42 | val f = scene.style |
| 43 | val color = Color(f.r.toInt().coerceIn(0, 255), f.g.toInt().coerceIn(0, 255), f.b.toInt().coerceIn(0, 255)) |
| 44 | val lay = petLayout(w.toDouble(), h.toDouble(), scene.state) |
| 45 | val dot = lay.dot.toFloat() * (appearance?.dotScale ?: 1f) |
| 46 | for (i in scene.dots.indices step 2) { |
| 47 | val center = Offset((lay.ox + scene.dots[i] * lay.scale * lay.flipX).toFloat(), |
| 48 | (lay.oy + scene.dots[i + 1] * lay.scale).toFloat()) |
| 49 | if (f.hollow) drawCircle(color, dot / 2, center, f.alpha.toFloat(), style = Stroke(1f)) |
| 50 | else { |
| 51 | drawCircle(color, dot / 2, center, f.alpha.toFloat()) |
| 52 | val glow = appearance?.glow ?: 0f |
| 53 | if (glow > 0 && i % 10 == 0) drawCircle(color,dot/2*(1+glow*5),center,.055f*glow) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fun petContentDescription(scene: PetScene): String = buildList { |
| 60 | add("Codewhale"); add(scene.style.channel); add(scene.style.arch) |
| 61 | if (scene.style.hollow) add("unobserved") |
| 62 | add(scene.behaviour) |
| 63 | if (scene.needs != "none" && scene.style.channel == "human" && !scene.style.hollow && scene.state.attention > 0.5) |
| 64 | add("input pending") |
| 65 | if (scene.peers >= 3) add("${scene.peers} pod members") |
| 66 | }.joinToString(", ") |
| 67 |