SPB Git

Repository Public License

DigViews.swift 23.5 KB 445 lines swift

1// ORIX Arcade2// File: Games/DigToCore/DigViews.swift3// Description: Dig to Core — SwiftUI screens: in-run HUD, hangar (records, journey, skins, field guide) and the upgrade shop.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI89// MARK: - HUD1011struct DigHUDView: View {12    let hud: DigHUD13    let leftHanded: Bool14    let onSurface: () -> Void15    @State private var confirmSurface = false16    @State private var bannerVisible = false17    @Environment(ArcadeServices.self) private var services1819    var body: some View {20        VStack(alignment: leftHanded ? .trailing : .leading, spacing: 6) {21            HStack(spacing: 6) {22                HUDPill(systemImage: "arrow.down", text: "\(hud.depth) m", tint: GameTheme.dig.primary, label: "Depth")23                HUDPill(systemImage: "shippingbox.fill", text: "\(hud.cargoCount)/\(hud.cargoCap)", tint: hud.cargoCount >= hud.cargoCap ? OrixColor.gold : OrixColor.textSecondary, label: "Cargo")24                HUDPill(systemImage: DigToCoreGame.creditSymbol, text: "\(hud.cargoValue + hud.finds)", tint: OrixColor.gold, label: "Haul value")25            }26            HStack(spacing: 6) {27                HUDGauge(systemImage: "fuelpump.fill", fraction: hud.fuel, tint: hud.fuel < 0.25 ? OrixColor.danger : OrixColor.cyan, label: "Fuel", width: 84)28                HUDGauge(systemImage: "shield.fill", fraction: hud.hull, tint: hud.hull < 0.3 ? OrixColor.danger : OrixColor.success, label: "Hull", width: 64)29                if !hud.ended { surfaceButton }30            }31            VStack(spacing: OrixSpace.xs) {32                if bannerVisible, let banner = hud.banner {33                    VStack(spacing: 2) {34                        Text(banner.uppercased()).font(.system(size: 26, weight: .black, design: .rounded)).tracking(1.5)35                            .foregroundStyle(Color(hex: hud.biome.accentHex))36                            .shadow(color: .black.opacity(0.8), radius: 6, y: 2)37                        if let detail = hud.bannerDetail {38                            Text(detail).font(OrixFont.caption).foregroundStyle(OrixColor.textPrimary).shadow(color: .black, radius: 4)39                        }40                    }41                    .padding(.horizontal, OrixSpace.md).padding(.vertical, OrixSpace.xs)42                    .background(Color.black.opacity(0.45), in: RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous))43                    .transition(.scale(scale: 0.85).combined(with: .opacity))44                    .accessibilityElement(children: .combine)45                }46                if let hint = hud.hint, !hud.ended {47                    CoachMark(text: hint, systemImage: hud.hintSymbol, theme: .dig)48                        .id(hint)49                        .transition(.opacity)50                }51            }52            .frame(maxWidth: .infinity)53            .padding(.top, OrixSpace.xs)54            Spacer()55        }56        .padding(.horizontal, OrixSpace.sm)57        .padding(.top, 10)58        .frame(maxWidth: .infinity, alignment: leftHanded ? .trailing : .leading)59        .animation(OrixMotion.decorative(OrixMotion.snappy), value: hud.hint)60        .animation(OrixMotion.decorative(OrixMotion.bouncy), value: bannerVisible)61        .onChange(of: hud.bannerTick) { _, tick in62            bannerVisible = true63            Task {64                try? await Task.sleep(for: .seconds(2.2))65                if hud.bannerTick == tick { bannerVisible = false }66            }67        }68    }6970    /// Two taps to surface, so a stray thumb never ends a dive.71    private var surfaceButton: some View {72        Button {73            services.feedbackTap()74            if confirmSurface {75                confirmSurface = false76                onSurface()77            } else {78                confirmSurface = true79                Task {80                    try? await Task.sleep(for: .seconds(2.5))81                    confirmSurface = false82                }83            }84        } label: {85            HStack(spacing: 5) {86                Image(systemName: "arrow.up.to.line").font(.system(size: 12, weight: .black))87                Text(confirmSurface ? "Tap to confirm" : "Surface").font(OrixFont.hud(12, weight: .bold))88            }89            .foregroundStyle(confirmSurface ? OrixColor.ink : OrixColor.textPrimary)90            .padding(.horizontal, 11)91            .frame(minHeight: 30)92            .background(confirmSurface ? AnyShapeStyle(OrixColor.gold) : AnyShapeStyle(Color.black.opacity(0.5)), in: Capsule())93            .overlay(Capsule().strokeBorder(OrixColor.gold.opacity(0.5), lineWidth: 1))94            .padding(.vertical, 9)95            .contentShape(Rectangle())96        }97        .buttonStyle(OrixPressStyle())98        .accessibilityLabel(confirmSurface ? "Confirm surfacing" : "Surface and bank the haul")99        .accessibilityIdentifier("dig-surface")100    }101}102103// MARK: - Shared bits104105struct DigCreditsPill: View {106    let amount: Int107108    var body: some View {109        HStack(spacing: 6) {110            Image(systemName: DigToCoreGame.creditSymbol).font(.system(size: 14, weight: .black)).foregroundStyle(OrixColor.gold)111            Text(amount.formatted()).font(OrixFont.hud(17)).foregroundStyle(OrixColor.textPrimary).contentTransition(.numericText())112        }113        .padding(.horizontal, 14)114        .frame(minHeight: 40)115        .background(Color.black.opacity(0.45), in: Capsule())116        .overlay(Capsule().strokeBorder(OrixColor.gold.opacity(0.4), lineWidth: 1))117        .accessibilityElement(children: .ignore)118        .accessibilityLabel("\(amount) credits")119        .accessibilityIdentifier("dig-bank")120    }121}122123// MARK: - Hangar124125struct DigHangarView: View {126    @Binding var showShop: Bool127    let onDive: () -> Void128    let onExit: () -> Void129    @Environment(ArcadeServices.self) private var services130    @State private var bob = false131132    var body: some View {133        let save = services.save.dig134        ZStack(alignment: .bottom) {135            AuroraBackground(theme: .dig)136            ScrollView {137                VStack(alignment: .leading, spacing: OrixSpace.lg) {138                    header(save)139                    showcase(save)140                    journey(save)141                    HStack(spacing: OrixSpace.xs) {142                        StatTile(value: "\(save.runs)", label: "Dives", systemImage: "arrow.down.circle.fill", tint: GameTheme.dig.primary)143                        StatTile(value: "\(save.fossils)", label: "Fossils", systemImage: "fossil.shell.fill", tint: Color(hex: DigTile.fossil.colorHex))144                        StatTile(value: "\(save.chests)", label: "Treasures", systemImage: "shippingbox.fill", tint: OrixColor.gold)145                    }146                    skins(save)147                    fieldGuide148                }149                .padding(.horizontal, OrixSpace.md)150                .padding(.bottom, 120)151            }152            .scrollIndicators(.hidden)153            actionBar(save)154            if showShop {155                DigShopView { withAnimation(OrixMotion.decorative(OrixMotion.snappy)) { showShop = false } }156                    .transition(.move(edge: .bottom).combined(with: .opacity))157                    .zIndex(3)158            }159        }160        .onAppear {161            guard !OrixAppearance.reduceMotion else { return }162            withAnimation(.easeInOut(duration: 1.4).repeatForever(autoreverses: true)) { bob = true }163        }164    }165166    private func header(_ save: DigSave) -> some View {167        HStack(spacing: OrixSpace.sm) {168            OrixIconButton(systemImage: "chevron.left", label: "Back to arcade", action: onExit)169                .accessibilityIdentifier("dig-back")170            VStack(alignment: .leading, spacing: 0) {171                Text("DIG TO CORE").font(OrixFont.overline).tracking(2).foregroundStyle(GameTheme.dig.primary)172                Text("Hangar").font(OrixFont.title).foregroundStyle(OrixColor.textPrimary)173            }174            Spacer()175            DigCreditsPill(amount: save.bank)176        }177        .padding(.top, OrixSpace.xs)178    }179180    private func showcase(_ save: DigSave) -> some View {181        let skin = DigSkin.find(save.selectedSkin)182        return HStack(spacing: OrixSpace.md) {183            ZStack {184                Circle().fill(RadialGradient(colors: [Color(hex: skin.bodyHex).opacity(0.45), .clear], center: .center, startRadius: 4, endRadius: 78))185                Image(uiImage: DigArt.drillImage(skin: skin))186                    .resizable().interpolation(.high).scaledToFit()187                    .frame(height: 132)188                    .offset(y: bob ? -5 : 5)189                    .shadow(color: .black.opacity(0.5), radius: 10, y: 8)190            }191            .frame(width: 132, height: 156)192            .accessibilityHidden(true)193            VStack(alignment: .leading, spacing: 4) {194                Text(save.runs == 0 ? "READY TO DIVE" : "BEST DEPTH").font(OrixFont.overline).tracking(1.6).foregroundStyle(OrixColor.textTertiary)195                HStack(alignment: .firstTextBaseline, spacing: 4) {196                    Text("\(save.bestDepth)").font(.system(size: 50, weight: .black, design: .rounded).monospacedDigit())197                        .foregroundStyle(GameTheme.dig.gradient).minimumScaleFactor(0.6).lineLimit(1)198                    Text("m").font(OrixFont.title).foregroundStyle(OrixColor.textSecondary)199                }200                Text(save.runs == 0 ? "Steer the drill, grab ore, come back richer." : save.coreReached > 0 ? "Core reached ×\(save.coreReached)" : save.deepestBiome.title)201                    .font(OrixFont.callout).foregroundStyle(OrixColor.textSecondary).fixedSize(horizontal: false, vertical: true)202                OrixChip(text: "\(save.totalUpgradeLevels) UPGRADES · \(skin.title.uppercased())", systemImage: "wrench.and.screwdriver.fill", tint: GameTheme.dig.primary)203                    .padding(.top, 2)204            }205            Spacer(minLength: 0)206        }207        .glassPanel(radius: OrixRadius.xl, tint: GameTheme.dig.primary)208        .accessibilityElement(children: .combine)209    }210211    private func journey(_ save: DigSave) -> some View {212        VStack(alignment: .leading, spacing: OrixSpace.sm) {213            SectionHeader(title: "Journey to the core", trailing: "\(save.bestDepth) / \(DigConfig.coreRow) m")214            VStack(spacing: 6) {215                ForEach(DigBiome.allCases, id: \.self) { biome in216                    let end = DigBiome(rawValue: biome.rawValue + 1)?.startRow ?? DigConfig.coreRow217                    let reached = save.bestDepth >= biome.startRow && (save.runs > 0 || biome == .loam)218                    let fraction = Double(save.bestDepth - biome.startRow) / Double(end - biome.startRow)219                    HStack(spacing: OrixSpace.sm) {220                        Image(uiImage: DigArt.tileImage(.ground, biome: biome))221                            .resizable().frame(width: 26, height: 26)222                            .saturation(reached ? 1 : 0.1).opacity(reached ? 1 : 0.45)223                        VStack(alignment: .leading, spacing: 3) {224                            HStack {225                                Text(reached ? biome.title : "Unknown strata").font(OrixFont.callout.weight(.bold))226                                    .foregroundStyle(reached ? OrixColor.textPrimary : OrixColor.textTertiary)227                                Spacer()228                                Text("\(biome.startRow) m").font(OrixFont.caption.monospacedDigit()).foregroundStyle(OrixColor.textTertiary)229                            }230                            OrixProgressBar(fraction: reached ? min(max(fraction, 0.02), 1) : 0, theme: .dig, height: 5)231                        }232                        if !reached { Image(systemName: "lock.fill").font(.system(size: 11, weight: .bold)).foregroundStyle(OrixColor.textTertiary) }233                    }234                    .accessibilityElement(children: .ignore)235                    .accessibilityLabel(reached ? "\(biome.title), starts at \(biome.startRow) metres, \(Int(min(max(fraction, 0), 1) * 100)) percent explored" : "Undiscovered strata at \(biome.startRow) metres")236                }237            }238            .glassPanel(radius: OrixRadius.md, padding: OrixSpace.sm)239        }240    }241242    private func skins(_ save: DigSave) -> some View {243        VStack(alignment: .leading, spacing: OrixSpace.sm) {244            SectionHeader(title: "Skins", trailing: "\(save.skins.count)/\(DigSkin.all.count)")245            ScrollView(.horizontal) {246                HStack(spacing: OrixSpace.xs) {247                    ForEach(DigSkin.all) { skin in248                        let owned = save.skins.contains(skin.id), selected = save.selectedSkin == skin.id249                        Button {250                            guard owned, !selected else { services.haptics.play(.tap); return }251                            services.feedbackTap()252                            services.store.update { $0.dig.selectedSkin = skin.id }253                        } label: {254                            VStack(spacing: 4) {255                                Image(uiImage: DigArt.drillImage(skin: skin, scale: 2))256                                    .resizable().scaledToFit().frame(height: 58)257                                    .saturation(owned ? 1 : 0).opacity(owned ? 1 : 0.35)258                                    .overlay { if !owned { Image(systemName: "lock.fill").font(.system(size: 16, weight: .bold)).foregroundStyle(OrixColor.textSecondary) } }259                                Text(skin.title).font(OrixFont.caption).foregroundStyle(owned ? OrixColor.textPrimary : OrixColor.textTertiary).lineLimit(1)260                                Text(owned ? (selected ? "Selected" : "Tap to use") : skin.requirement)261                                    .font(.system(size: 10, weight: .semibold, design: .rounded))262                                    .foregroundStyle(selected ? GameTheme.dig.primary : OrixColor.textTertiary)263                                    .multilineTextAlignment(.center).lineLimit(2).frame(height: 26, alignment: .top)264                            }265                            .frame(width: 104)266                            .padding(.vertical, OrixSpace.xs)267                            .background(Color.white.opacity(selected ? 0.10 : 0.04), in: RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous))268                            .overlay(RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous)269                                .strokeBorder(selected ? GameTheme.dig.primary : OrixColor.stroke, lineWidth: selected ? 2 : 1))270                        }271                        .buttonStyle(OrixPressStyle())272                        .accessibilityLabel("\(skin.title) skin. \(owned ? (selected ? "Selected" : "Owned") : "Locked: \(skin.requirement)")")273                        .accessibilityIdentifier("dig-skin-\(skin.id)")274                    }275                }276            }277            .scrollIndicators(.hidden)278        }279    }280281    private var fieldGuide: some View {282        let entries: [(DigTile, DigBiome, String, String)] = [283            (.gold, .crystal, "Ore", "Drill it to haul it"), (.fuelCell, .loam, "Fuel cell", "Refills the tank"), (.repairKit, .loam, "Repair kit", "Restores hull"),284            (.hardRock, .crystal, "Hard rock", "Slow going"), (.boulder, .loam, "Steel plate", "Go around"), (.lava, .magma, "Lava", "Burns the hull"),285            (.gas, .loam, "Gas pocket", "Explodes"), (.fossil, .shale, "Fossil", "Rare bonus"), (.chest, .loam, "Treasure", "Vault payout"),286        ]287        return VStack(alignment: .leading, spacing: OrixSpace.sm) {288            SectionHeader(title: "Field guide")289            LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: OrixSpace.xs), count: 3), spacing: OrixSpace.xs) {290                ForEach(Array(entries.enumerated()), id: \.offset) { _, entry in291                    VStack(spacing: 3) {292                        Image(uiImage: DigArt.tileImage(entry.0, biome: entry.1)).resizable().frame(width: 34, height: 34)293                        Text(entry.2).font(OrixFont.caption).foregroundStyle(OrixColor.textPrimary).lineLimit(1).minimumScaleFactor(0.8)294                        Text(entry.3).font(.system(size: 10, weight: .medium, design: .rounded)).foregroundStyle(OrixColor.textTertiary).lineLimit(1).minimumScaleFactor(0.8)295                    }296                    .frame(maxWidth: .infinity)297                    .padding(.vertical, OrixSpace.xs)298                    .background(Color.white.opacity(0.04), in: RoundedRectangle(cornerRadius: OrixRadius.sm, style: .continuous))299                    .accessibilityElement(children: .ignore)300                    .accessibilityLabel("\(entry.2): \(entry.3)")301                }302            }303        }304    }305306    private func actionBar(_ save: DigSave) -> some View {307        let affordable = DigUpgrade.all.filter { ($0.cost(atLevel: save.level($0)) ?? .max) <= save.bank }.count308        return HStack(spacing: OrixSpace.xs) {309            Button {310                services.feedbackTap()311                withAnimation(OrixMotion.decorative(OrixMotion.snappy)) { showShop = true }312            } label: {313                HStack(spacing: 6) {314                    Image(systemName: "wrench.and.screwdriver.fill").font(.system(size: 15, weight: .heavy))315                    Text("Upgrades")316                    if affordable > 0 {317                        Text("\(affordable)").font(OrixFont.overline).foregroundStyle(OrixColor.ink)318                            .frame(minWidth: 20, minHeight: 20).background(OrixColor.gold, in: Circle())319                    }320                }321            }322            .buttonStyle(OrixSecondaryButtonStyle())323            .accessibilityLabel("Upgrades. \(affordable) affordable")324            .accessibilityIdentifier("dig-upgrades")325            OrixButton(title: "Dive", systemImage: "arrow.down.circle.fill", theme: .dig, action: onDive)326                .accessibilityIdentifier("dig-dive")327        }328        .padding(.horizontal, OrixSpace.md)329        .padding(.top, OrixSpace.sm)330        .padding(.bottom, OrixSpace.xs)331        .background(LinearGradient(stops: [.init(color: OrixColor.void.opacity(0), location: 0), .init(color: OrixColor.void.opacity(0.96), location: 0.28)], startPoint: .top, endPoint: .bottom).ignoresSafeArea())332    }333}334335// MARK: - Shop336337struct DigShopView: View {338    let onClose: () -> Void339    @Environment(ArcadeServices.self) private var services340341    var body: some View {342        let save = services.save.dig343        ZStack {344            OrixColor.void.opacity(0.97).ignoresSafeArea()345            AuroraBackground(theme: .dig, intensity: 0.6)346            VStack(spacing: 0) {347                HStack(spacing: OrixSpace.sm) {348                    VStack(alignment: .leading, spacing: 0) {349                        Text("WORKSHOP").font(OrixFont.overline).tracking(2).foregroundStyle(GameTheme.dig.primary)350                        Text("Upgrades").font(OrixFont.title).foregroundStyle(OrixColor.textPrimary)351                    }352                    Spacer()353                    DigCreditsPill(amount: save.bank)354                    OrixIconButton(systemImage: "xmark", label: "Close upgrades", action: onClose)355                        .accessibilityIdentifier("dig-shop-close")356                }357                .padding(.horizontal, OrixSpace.md)358                .padding(.vertical, OrixSpace.xs)359                ScrollView {360                    VStack(spacing: OrixSpace.xs) {361                        ForEach(DigUpgrade.all) { upgrade in row(upgrade, save: save) }362                        Text("Upgrades are permanent. Credits come from ore, finds and depth.")363                            .font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary).padding(.top, OrixSpace.xs)364                    }365                    .padding(.horizontal, OrixSpace.md)366                    .padding(.bottom, OrixSpace.xl)367                }368                .scrollIndicators(.hidden)369            }370        }371        .accessibilityAddTraits(.isModal)372    }373374    private func row(_ upgrade: DigUpgrade, save: DigSave) -> some View {375        let level = save.level(upgrade)376        let cost = upgrade.cost(atLevel: level)377        let canBuy = cost.map { $0 <= save.bank } ?? false378        return HStack(spacing: OrixSpace.sm) {379            Image(systemName: upgrade.symbol)380                .font(.system(size: 18, weight: .bold))381                .foregroundStyle(level > 0 ? OrixColor.ink : GameTheme.dig.primary)382                .frame(width: 42, height: 42)383                .background(level > 0 ? AnyShapeStyle(GameTheme.dig.gradient) : AnyShapeStyle(Color.white.opacity(0.08)), in: Circle())384            VStack(alignment: .leading, spacing: 3) {385                HStack(spacing: 6) {386                    Text(upgrade.title).font(OrixFont.callout.weight(.heavy)).foregroundStyle(OrixColor.textPrimary).lineLimit(1)387                    HStack(spacing: 3) {388                        ForEach(0..<upgrade.maxLevel, id: \.self) { pip in389                            Capsule().fill(pip < level ? GameTheme.dig.primary : Color.white.opacity(0.16)).frame(width: 9, height: 4)390                        }391                    }392                }393                Text(upgrade.detail).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary).lineLimit(2).fixedSize(horizontal: false, vertical: true)394                HStack(spacing: 4) {395                    Text(upgrade.effectLabel(level: level)).foregroundStyle(OrixColor.textTertiary)396                    if cost != nil {397                        Image(systemName: "arrow.right").font(.system(size: 9, weight: .black)).foregroundStyle(OrixColor.textTertiary)398                        Text(upgrade.effectLabel(level: level + 1)).foregroundStyle(GameTheme.dig.primary)399                    }400                }401                .font(.system(size: 11, weight: .bold, design: .rounded).monospacedDigit())402            }403            Spacer(minLength: 4)404            if let cost {405                Button {406                    buy(upgrade)407                } label: {408                    HStack(spacing: 4) {409                        Image(systemName: DigToCoreGame.creditSymbol).font(.system(size: 11, weight: .black))410                        Text(cost.formatted()).font(OrixFont.hud(15))411                    }412                    .foregroundStyle(canBuy ? OrixColor.ink : OrixColor.textTertiary)413                    .padding(.horizontal, 12)414                    .frame(minWidth: 78, minHeight: 44)415                    .background(canBuy ? AnyShapeStyle(GameTheme.dig.gradient) : AnyShapeStyle(Color.white.opacity(0.07)), in: Capsule())416                    .overlay(Capsule().strokeBorder(OrixColor.stroke, lineWidth: canBuy ? 0 : 1))417                }418                .buttonStyle(OrixPressStyle())419                .disabled(!canBuy)420                .accessibilityLabel("Buy \(upgrade.title) level \(level + 1) for \(cost) credits")421                .accessibilityHint(canBuy ? "" : "Not enough credits")422                .accessibilityIdentifier("dig-buy-\(upgrade.id)")423            } else {424                OrixChip(text: "MAX", systemImage: "checkmark", tint: GameTheme.dig.primary, filled: true)425                    .frame(minWidth: 78)426            }427        }428        .glassPanel(radius: OrixRadius.md, tint: level > 0 ? GameTheme.dig.primary : .white, padding: OrixSpace.sm)429        .accessibilityElement(children: .contain)430    }431432    private func buy(_ upgrade: DigUpgrade) {433        var bought = false434        services.store.update { bought = DigEconomy.buy(upgrade, save: &$0.dig) }435        guard bought else {436            services.audio.play(.back)437            return438        }439        services.audio.play(.upgrade)440        services.haptics.play(.success)441        if services.save.dig.totalUpgradeLevels >= 10 { services.unlock("dig.geared") }442        services.store.flush()443    }444}445
Files main
Menu

Theme