CasinoGames/Baccarat/Views/ChipView.swift

292 lines
10 KiB
Swift

//
// ChipView.swift
// Baccarat
//
// Casino-style betting chips with realistic design.
//
import SwiftUI
/// The available chip denominations.
enum ChipDenomination: Int, CaseIterable, Identifiable {
case ten = 10
case twentyFive = 25
case fifty = 50
case hundred = 100
case fiveHundred = 500
case thousand = 1_000
case fiveThousand = 5_000
case tenThousand = 10_000
case twentyFiveThousand = 25_000
case fiftyThousand = 50_000
case hundredThousand = 100_000
var id: Int { rawValue }
/// The display text for this denomination.
var displayText: String {
switch self {
case .ten: return "10"
case .twentyFive: return "25"
case .fifty: return "50"
case .hundred: return "100"
case .fiveHundred: return "500"
case .thousand: return "1K"
case .fiveThousand: return "5K"
case .tenThousand: return "10K"
case .twentyFiveThousand: return "25K"
case .fiftyThousand: return "50K"
case .hundredThousand: return "100K"
}
}
/// The minimum balance required to show this chip in the selector.
/// Higher chips unlock as you win more!
var unlockBalance: Int {
switch self {
case .ten, .twentyFive, .fifty, .hundred: return 0 // Always available
case .fiveHundred: return 500
case .thousand: return 1_000
case .fiveThousand: return 5_000
case .tenThousand: return 10_000
case .twentyFiveThousand: return 25_000
case .fiftyThousand: return 50_000
case .hundredThousand: return 100_000
}
}
/// Whether this chip should be shown based on the player's balance.
func isUnlocked(forBalance balance: Int) -> Bool {
balance >= unlockBalance
}
/// Returns chips that should be visible for a given balance.
static func availableChips(forBalance balance: Int) -> [ChipDenomination] {
allCases.filter { $0.isUnlocked(forBalance: balance) }
}
/// The primary color for this chip.
var primaryColor: Color {
switch self {
case .ten: return Color(red: 0.2, green: 0.4, blue: 0.8) // Blue
case .twentyFive: return Color(red: 0.1, green: 0.6, blue: 0.3) // Green
case .fifty: return Color(red: 0.8, green: 0.5, blue: 0.1) // Orange
case .hundred: return Color(red: 0.1, green: 0.1, blue: 0.1) // Black
case .fiveHundred: return Color(red: 0.6, green: 0.2, blue: 0.6) // Purple
case .thousand: return Color(red: 0.8, green: 0.65, blue: 0.2) // Gold
case .fiveThousand: return Color(red: 0.7, green: 0.1, blue: 0.2) // Crimson
case .tenThousand: return Color(red: 0.2, green: 0.5, blue: 0.5) // Teal
case .twentyFiveThousand: return Color(red: 0.5, green: 0.3, blue: 0.1) // Bronze
case .fiftyThousand: return Color(red: 0.75, green: 0.75, blue: 0.8) // Platinum
case .hundredThousand: return Color(red: 0.9, green: 0.1, blue: 0.3) // Ruby
}
}
/// The secondary/accent color for this chip.
var secondaryColor: Color {
switch self {
case .ten: return Color(red: 0.3, green: 0.5, blue: 0.9)
case .twentyFive: return Color(red: 0.2, green: 0.7, blue: 0.4)
case .fifty: return Color(red: 0.9, green: 0.6, blue: 0.2)
case .hundred: return Color(red: 0.3, green: 0.3, blue: 0.3)
case .fiveHundred: return Color(red: 0.7, green: 0.3, blue: 0.7)
case .thousand: return Color(red: 0.9, green: 0.75, blue: 0.3)
case .fiveThousand: return Color(red: 0.85, green: 0.2, blue: 0.3)
case .tenThousand: return Color(red: 0.3, green: 0.6, blue: 0.6)
case .twentyFiveThousand: return Color(red: 0.65, green: 0.45, blue: 0.2)
case .fiftyThousand: return Color(red: 0.85, green: 0.85, blue: 0.9)
case .hundredThousand: return Color(red: 1.0, green: 0.2, blue: 0.4)
}
}
/// The edge stripe color for this chip.
var stripeColor: Color {
switch self {
case .ten, .twentyFive, .fifty: return .white
case .hundred: return Color(red: 0.9, green: 0.75, blue: 0.3)
case .fiveHundred: return .white
case .thousand: return .black
case .fiveThousand: return Color(red: 0.9, green: 0.75, blue: 0.3) // Gold stripes
case .tenThousand: return .white
case .twentyFiveThousand: return Color(red: 0.9, green: 0.75, blue: 0.3)
case .fiftyThousand: return Color(red: 0.2, green: 0.2, blue: 0.3) // Dark stripes
case .hundredThousand: return Color(red: 0.9, green: 0.85, blue: 0.3) // Gold stripes
}
}
}
/// A realistic casino-style betting chip.
struct ChipView: View {
let denomination: ChipDenomination
let size: CGFloat
var isSelected: Bool = false
init(denomination: ChipDenomination, size: CGFloat = 60, isSelected: Bool = false) {
self.denomination = denomination
self.size = size
self.isSelected = isSelected
}
var body: some View {
ZStack {
// Base circle with gradient
Circle()
.fill(
RadialGradient(
colors: [
denomination.secondaryColor,
denomination.primaryColor,
denomination.primaryColor.opacity(0.8)
],
center: .topLeading,
startRadius: 0,
endRadius: size
)
)
// Edge stripes pattern
ChipEdgePattern(stripeColor: denomination.stripeColor)
.clipShape(.circle)
// Inner circle
Circle()
.fill(
RadialGradient(
colors: [
denomination.secondaryColor,
denomination.primaryColor
],
center: .topLeading,
startRadius: 0,
endRadius: size * 0.4
)
)
.frame(width: size * 0.65, height: size * 0.65)
// Inner border
Circle()
.strokeBorder(
denomination.stripeColor.opacity(0.8),
lineWidth: 2
)
.frame(width: size * 0.65, height: size * 0.65)
// Denomination text
Text(denomination.displayText)
.font(.system(size: size * 0.25, weight: .heavy, design: .rounded))
.foregroundStyle(denomination.stripeColor)
.shadow(color: .black.opacity(0.3), radius: 1, x: 1, y: 1)
// Outer border
Circle()
.strokeBorder(
LinearGradient(
colors: [
Color.white.opacity(0.4),
Color.black.opacity(0.3)
],
startPoint: .topLeading,
endPoint: .bottomTrailing
),
lineWidth: 2
)
// Selection glow
if isSelected {
Circle()
.strokeBorder(Color.yellow, lineWidth: 3)
.frame(width: size + 6, height: size + 6)
}
}
.frame(width: size, height: size)
.shadow(color: .black.opacity(0.4), radius: isSelected ? 8 : 4, x: 2, y: 3)
.scaleEffect(isSelected ? 1.1 : 1.0)
.animation(.spring(duration: 0.2), value: isSelected)
}
}
/// The edge stripe pattern for chips.
struct ChipEdgePattern: View {
let stripeColor: Color
var body: some View {
Canvas { context, size in
let center = CGPoint(x: size.width / 2, y: size.height / 2)
let radius = min(size.width, size.height) / 2
let stripeCount = 16
let stripeWidth: CGFloat = 6
for i in 0..<stripeCount {
let angle = Double(i) * (360.0 / Double(stripeCount)) * .pi / 180
let innerRadius = radius * 0.75
let outerRadius = radius * 0.98
let startX = center.x + cos(angle) * innerRadius
let startY = center.y + sin(angle) * innerRadius
let endX = center.x + cos(angle) * outerRadius
let endY = center.y + sin(angle) * outerRadius
var path = Path()
path.move(to: CGPoint(x: startX, y: startY))
path.addLine(to: CGPoint(x: endX, y: endY))
context.stroke(
path,
with: .color(stripeColor),
lineWidth: stripeWidth
)
}
}
}
}
/// A stack of chips showing a bet amount.
struct ChipStackView: View {
let amount: Int
let maxChips: Int = 5
private var chipBreakdown: [(ChipDenomination, Int)] {
var remaining = amount
var result: [(ChipDenomination, Int)] = []
for denom in ChipDenomination.allCases.reversed() {
let count = remaining / denom.rawValue
if count > 0 {
result.append((denom, min(count, maxChips)))
remaining -= count * denom.rawValue
}
}
return result
}
var body: some View {
ZStack {
ForEach(chipBreakdown.indices, id: \.self) { index in
let (denom, _) = chipBreakdown[index]
ChipView(denomination: denom, size: 40)
.offset(y: CGFloat(-index * 4))
}
}
}
}
#Preview {
ZStack {
Color(red: 0.0, green: 0.3, blue: 0.15)
.ignoresSafeArea()
VStack(spacing: 30) {
HStack(spacing: 15) {
ForEach(ChipDenomination.allCases) { denom in
ChipView(denomination: denom, size: 50)
}
}
ChipView(denomination: .hundred, size: 80, isSelected: true)
}
}
}