| 1 | import SwiftUI |
| 2 | |
| 3 | public struct PetHabitatView: View { |
| 4 | @ObservedObject var host: PetHost |
| 5 | @Environment(\.accessibilityReduceMotion) private var reducedMotion |
| 6 | public init(host: PetHost) { self.host = host } |
| 7 | public var body: some View { |
| 8 | VStack(spacing: 12) { |
| 9 | if host.source == .live { |
| 10 | PetSharedHabitat(host: host.shared, still: host.still || reducedMotion) |
| 11 | } else if let core = host.core { |
| 12 | ZStack { |
| 13 | LinearGradient(colors: [Color(red: 0.06, green: 0.15, blue: 0.20), Color(red: 0.03, green: 0.07, blue: 0.10)], startPoint: .top, endPoint: .bottom) |
| 14 | Canvas { ctx, size in |
| 15 | let frame = core.frame |
| 16 | var surface = Path(); surface.move(to: CGPoint(x: 12, y: 20 + frame.surface * 2)); surface.addLine(to: CGPoint(x: size.width - 12, y: 20 + frame.surface * 2)) |
| 17 | ctx.stroke(surface, with: .color(.cyan.opacity(0.12)), lineWidth: 1) |
| 18 | for i in 0..<5 { |
| 19 | var path = Path() |
| 20 | for x in stride(from: 0.0, through: size.width, by: 8) { |
| 21 | let t = host.still || reducedMotion ? 0 : frame.timeMs / 1000 |
| 22 | let y = size.height * 0.85 + sin(x / 90 + Double(i) + t * 0.18) * 6 + Double(i) * 4 |
| 23 | if x == 0 { path.move(to: CGPoint(x: x, y: y)) } else { path.addLine(to: CGPoint(x: x, y: y)) } |
| 24 | } |
| 25 | ctx.stroke(path, with: .color(.cyan.opacity(0.025 + 0.015 * frame.caustic)), lineWidth: 1) |
| 26 | } |
| 27 | if let food = frame.food { |
| 28 | let rect = CGRect(x: size.width * (0.5 + food.x * 0.3), y: size.height * (0.5 + food.y * 0.3), width: 4, height: 4) |
| 29 | ctx.fill(Path(ellipseIn: rect), with: .color(.yellow.opacity(food.life * 0.6))) |
| 30 | } |
| 31 | } |
| 32 | CodewhalePetView(sim: core.sim, state: core.frame.state).padding(12) |
| 33 | } |
| 34 | .clipShape(RoundedRectangle(cornerRadius: 6)) |
| 35 | Text("\(core.frame.behaviour)\(core.frame.needs == "none" ? "" : " · awaiting input")") |
| 36 | .font(.caption.monospaced()).foregroundStyle(.secondary) |
| 37 | } else { Text("Habitat unavailable").frame(maxWidth: .infinity, maxHeight: .infinity) } |
| 38 | if host.source != .live { |
| 39 | Text(host.message).font(.caption).foregroundStyle(.secondary) |
| 40 | if !host.persistenceMessage.isEmpty { Text(host.persistenceMessage).font(.caption).foregroundStyle(.secondary) } |
| 41 | HStack { |
| 42 | Button("Focus") { host.interact(food: false) } |
| 43 | Button("Pulse") { host.interact(food: true) } |
| 44 | Toggle("Sound", isOn: $host.sound) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | .onAppear { host.systemReducedMotion = reducedMotion } |
| 49 | .onChange(of: reducedMotion) { _, value in host.systemReducedMotion = value } |
| 50 | } |
| 51 | } |
| 52 |