SPB Git

Repository Public License

TrafficMayhemGame.swift 11.7 KB 229 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Arcade9// File             : Games/TrafficMayhem/TrafficMayhemGame.swift10// Description      : Traffic Mayhem — lobby descriptor, achievements and the11//                    animated cover art.12// Module           : Games13// Repository       : https://git.spboucher.ai/orix-arcade14// Created          : 2026-11-2115// Last Modified    : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : Proprietary, all rights reserved (see license file)82// License File     : LICENSE83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SwiftUI9192enum TrafficMayhemGame {93    static let rushAchievementScore = 12009495    static let achievements: [AchievementDef] = [96        .init(id: "traffic.first", game: .trafficMayhem, title: "Green Wave", detail: "Clear 10 vehicles in one run.", symbol: "car.fill"),97        .init(id: "traffic.streak", game: .trafficMayhem, title: "Flow State", detail: "Reach a streak of 25 safe crossings.", symbol: "flame.fill"),98        .init(id: "traffic.close", game: .trafficMayhem, title: "Paint Swapper", detail: "Pull off 5 close calls in one run.", symbol: "exclamationmark.triangle.fill"),99        .init(id: "traffic.flawless", game: .trafficMayhem, title: "Spotless", detail: "Clear 40 vehicles before losing a heart.", symbol: "heart.fill"),100        .init(id: "traffic.rush", game: .trafficMayhem, title: "Rush Hour Hero", detail: "Score 1,200 in a 60-second Rush.", symbol: "timer"),101        .init(id: "traffic.all", game: .trafficMayhem, title: "City Planner", detail: "Unlock every junction.", symbol: "map.fill"),102    ]103104    static let descriptor = GameDescriptor(105        id: .trafficMayhem,106        title: "Traffic Mayhem",107        tagline: "Tap cars through the junction. Keep the streak alive.",108        theme: .traffic,109        music: .traffic,110        preview: { AnyView(TrafficCoverView()) },111        bestLabel: { save in112            let best = save.traffic.overallBest113            return best > 0 ? "BEST \(best.formatted())" : nil114        },115        progress: { save in116            let unlocked = save.traffic.unlockedCount117            return ("\(unlocked) / \(TrafficLayout.all.count) junctions", Double(unlocked) / Double(TrafficLayout.all.count))118        },119        root: { onExit in AnyView(TrafficRootView(onExit: onExit)) })120}121122/// Lobby cover: a tilted night junction with cars streaming through in turns. Static under Reduce Motion.123struct TrafficCoverView: View {124    var body: some View {125        if OrixAppearance.reduceMotion {126            Canvas { context, size in Self.draw(&context, size: size, time: 1.4) }127        } else {128            TimelineView(.animation(minimumInterval: 1.0 / 30)) { timeline in129                Canvas { context, size in130                    Self.draw(&context, size: size, time: timeline.date.timeIntervalSinceReferenceDate)131                }132            }133        }134    }135136    private struct CoverCar {137        var horizontal: Bool138        var direction: Double139        var crossAt: Double140        var color: UInt32141        var length: Double142    }143144    private static let cars: [CoverCar] = [145        .init(horizontal: true, direction: 1, crossAt: 0.5, color: 0xFF5E7E, length: 30),146        .init(horizontal: true, direction: -1, crossAt: 0.9, color: 0x4DD8FF, length: 30),147        .init(horizontal: true, direction: 1, crossAt: 1.5, color: 0xFFB341, length: 50),148        .init(horizontal: true, direction: -1, crossAt: 1.9, color: 0xEDEFFF, length: 30),149        .init(horizontal: false, direction: 1, crossAt: 3.5, color: 0x4DE3A1, length: 30),150        .init(horizontal: false, direction: -1, crossAt: 3.9, color: 0xFFC94D, length: 30),151        .init(horizontal: false, direction: 1, crossAt: 4.4, color: 0x8B7BFF, length: 30),152        .init(horizontal: false, direction: -1, crossAt: 4.9, color: 0xFF8A3D, length: 50),153    ]154155    private static func draw(_ context: inout GraphicsContext, size: CGSize, time: Double) {156        let theme = GameTheme.traffic157        context.fill(Path(CGRect(origin: .zero, size: size)),158                     with: .linearGradient(Gradient(colors: [Color(hex: 0x0E1838), theme.deep, Color(hex: 0x070A18)]),159                                           startPoint: .zero, endPoint: CGPoint(x: size.width, y: size.height)))160        var ctx = context161        ctx.translateBy(x: size.width * 0.66, y: size.height * 0.42)162        ctx.rotate(by: .degrees(-24))163        let road: CGFloat = 62, reach: CGFloat = 520164        // City blocks.165        var rng = SeededRNG(seed: 77)166        for ix in -5...5 {167            for iy in -4...4 {168                let x = CGFloat(ix) * 58 + (ix > 0 ? road / 2 + 8 : -road / 2 - 58), y = CGFloat(iy) * 58 + (iy > 0 ? road / 2 + 8 : -road / 2 - 58)169                if ix == 0 || iy == 0 { continue }170                let hue: [UInt32] = [0x27336B, 0x35307A, 0x1F4A6B, 0x3D2C6E]171                let rect = CGRect(x: x, y: y, width: 50, height: 50)172                ctx.fill(Path(roundedRect: rect.offsetBy(dx: 5, dy: 7), cornerRadius: 9), with: .color(.black.opacity(0.35)))173                ctx.fill(Path(roundedRect: rect, cornerRadius: 9), with: .color(Color(hex: hue[rng.int(in: 0...3)])))174                if rng.chance(0.55) {175                    ctx.fill(Path(roundedRect: rect.insetBy(dx: 15, dy: 15), cornerRadius: 4), with: .color(Color(hex: rng.chance(0.5) ? 0xFFD98A : 0x7FE3FF, opacity: 0.55)))176                }177            }178        }179        // Roads and markings.180        let asphalt = Color(hex: 0x141A33)181        ctx.fill(Path(CGRect(x: -reach, y: -road / 2, width: reach * 2, height: road)), with: .color(asphalt))182        ctx.fill(Path(CGRect(x: -road / 2, y: -reach, width: road, height: reach * 2)), with: .color(asphalt))183        var dashes = Path()184        dashes.move(to: CGPoint(x: -reach, y: 0)); dashes.addLine(to: CGPoint(x: -road / 2 - 14, y: 0))185        dashes.move(to: CGPoint(x: road / 2 + 14, y: 0)); dashes.addLine(to: CGPoint(x: reach, y: 0))186        dashes.move(to: CGPoint(x: 0, y: -reach)); dashes.addLine(to: CGPoint(x: 0, y: -road / 2 - 14))187        dashes.move(to: CGPoint(x: 0, y: road / 2 + 14)); dashes.addLine(to: CGPoint(x: 0, y: reach))188        ctx.stroke(dashes, with: .color(Color(hex: 0xFFC94D, opacity: 0.65)), style: StrokeStyle(lineWidth: 2.4, dash: [12, 10]))189        for k in 0..<4 {190            var arm = ctx191            arm.rotate(by: .degrees(Double(k) * 90))192            for i in 0..<6 {193                arm.fill(Path(roundedRect: CGRect(x: -road / 2 + 4 + CGFloat(i) * 9.6, y: road / 2 + 3, width: 5.6, height: 10), cornerRadius: 1.5),194                         with: .color(.white.opacity(0.5)))195            }196        }197        // Junction glow in the theme colours.198        ctx.fill(Path(ellipseIn: CGRect(x: -90, y: -90, width: 180, height: 180)),199                 with: .radialGradient(Gradient(colors: [theme.primary.opacity(0.35), theme.secondary.opacity(0)]), center: .zero, startRadius: 0, endRadius: 90))200        // Cars: the two streets take turns, so nothing ever collides on the cover.201        let cycle = 6.0, speed = 150.0202        for car in cars {203            var phase = (time - car.crossAt).truncatingRemainder(dividingBy: cycle)204            if phase < -cycle / 2 { phase += cycle }205            if phase > cycle / 2 { phase -= cycle }206            let along = phase * speed * car.direction207            let lane = road / 4 * (car.horizontal ? CGFloat(car.direction) : CGFloat(-car.direction))208            var c = ctx209            if car.horizontal {210                c.translateBy(x: along, y: lane)211                if car.direction < 0 { c.rotate(by: .degrees(180)) }212            } else {213                c.translateBy(x: lane, y: along)214                c.rotate(by: .degrees(car.direction > 0 ? 90 : -90))215            }216            let length = CGFloat(car.length), width: CGFloat = 17217            let body = CGRect(x: -length / 2, y: -width / 2, width: length, height: width)218            c.fill(Path(ellipseIn: CGRect(x: length / 2 - 4, y: -width, width: 46, height: width * 2)),219                   with: .radialGradient(Gradient(colors: [Color(hex: 0xFFF3B8, opacity: 0.4), .clear]), center: CGPoint(x: length / 2 + 6, y: 0), startRadius: 0, endRadius: 34))220            c.fill(Path(roundedRect: body.offsetBy(dx: 2, dy: 4), cornerRadius: 6), with: .color(.black.opacity(0.4)))221            c.fill(Path(roundedRect: body, cornerRadius: 6),222                   with: .linearGradient(Gradient(colors: [Color(hex: car.color), Color(hex: car.color).opacity(0.72)]),223                                         startPoint: CGPoint(x: 0, y: -width / 2), endPoint: CGPoint(x: 0, y: width / 2)))224            c.fill(Path(roundedRect: CGRect(x: -length * 0.2, y: -width / 2 + 2.5, width: length * 0.45, height: width - 5), cornerRadius: 3),225                   with: .color(Color(hex: 0x0B1022, opacity: 0.85)))226        }227    }228}229
Files main
Menu

Theme