Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Arcade9// File : Games/TrafficMayhem/TrafficArt.swift10// Description : Traffic Mayhem — all artwork drawn in code: vehicle11// sprites and the baked night-city background (roads,12// markings, buildings, parks, lamps).13// Module : Games14// Repository : https://git.spboucher.ai/orix-arcade15// Created : 2026-11-2116// Last Modified : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : Proprietary, all rights reserved (see license file)83// License File : LICENSE84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import SpriteKit92import UIKit9394enum TrafficArt {95 static let palette: [UInt32] = [0xFF5E7E, 0x4DD8FF, 0xFFC94D, 0x8B7BFF, 0x4DE3A1, 0xFF8A3D, 0xEDEFFF]9697 static func bodyColor(kind: VehicleKind, tint: Int) -> UIColor {98 switch kind {99 case .car, .sport: UIColor(hex: palette[tint % palette.count])100 case .bus: UIColor(hex: tint.isMultiple(of: 2) ? 0xFFB341 : 0x3DE8C5)101 case .truck: UIColor(hex: [0x4DA3FF, 0xFF5E7E, 0x8B7BFF][tint % 3])102 case .ambulance: UIColor(hex: 0xF4F5FF)103 }104 }105106 // MARK: Vehicles107108 /// Top-down vehicle sprite, nose pointing along +x.109 static func vehicleTexture(kind: VehicleKind, tint: Int) -> SKTexture {110 let length = CGFloat(kind.length), width = CGFloat(kind.width)111 let color = bodyColor(kind: kind, tint: tint)112 let key = "traffic-vehicle-\(kind.rawValue)-\(color.hexKey)"113 return TextureFactory.texture(key, size: CGSize(width: length, height: width)) { ctx, size in114 let glass = UIColor(hex: 0x0B1022, alpha: 0.92)115 let fill: (CGRect, CGFloat, UIColor) -> Void = { rect, radius, c in rounded(ctx, rect, radius, c) }116 let body: (CGRect, CGFloat, UIColor) -> Void = { rect, radius, c in shadedBody(ctx, rect, radius, c) }117 let full = CGRect(origin: .zero, size: size).insetBy(dx: 0.4, dy: 0.4)118 let head = UIColor(hex: 0xFFF3B8), tail = UIColor(hex: 0xFF3B55)119 switch kind {120 case .car:121 body(full, width * 0.34, color)122 fill(CGRect(x: length * 0.26, y: 1.8, width: length * 0.5, height: width - 3.6), 2.6, glass)123 fill(CGRect(x: length * 0.36, y: 2.6, width: length * 0.26, height: width - 5.2), 1.8, color.lighter(0.18))124 case .sport:125 let nose = UIBezierPath()126 nose.move(to: CGPoint(x: 1.5, y: 1.2))127 nose.addLine(to: CGPoint(x: length * 0.62, y: 0.6))128 nose.addQuadCurve(to: CGPoint(x: length - 0.6, y: width / 2), controlPoint: CGPoint(x: length - 0.4, y: 1.4))129 nose.addQuadCurve(to: CGPoint(x: length * 0.62, y: width - 0.6), controlPoint: CGPoint(x: length - 0.4, y: width - 1.4))130 nose.addLine(to: CGPoint(x: 1.5, y: width - 1.2))131 nose.addQuadCurve(to: CGPoint(x: 1.5, y: 1.2), controlPoint: CGPoint(x: -0.6, y: width / 2))132 ctx.saveGState()133 ctx.addPath(nose.cgPath)134 ctx.clip()135 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),136 colors: [color.lighter(0.3).cgColor, color.cgColor, color.darker(0.35).cgColor] as CFArray, locations: [0, 0.45, 1])!137 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 0, y: width), options: [])138 ctx.restoreGState()139 fill(CGRect(x: 2, y: width / 2 - 1.1, width: length - 5, height: 2.2), 1, UIColor.white.withAlphaComponent(0.85))140 fill(CGRect(x: length * 0.3, y: 2.4, width: length * 0.34, height: width - 4.8), 2.4, glass)141 fill(CGRect(x: 0.6, y: 1.4, width: 2, height: width - 2.8), 0.8, UIColor(hex: 0x0B1022))142 case .bus:143 body(full, 3.4, color)144 fill(CGRect(x: length - 7.5, y: 1.6, width: 4.2, height: width - 3.2), 1.4, glass)145 fill(CGRect(x: 3, y: 1.3, width: length - 13, height: 1.7), 0.6, glass)146 fill(CGRect(x: 3, y: width - 3, width: length - 13, height: 1.7), 0.6, glass)147 for i in 0..<3 {148 fill(CGRect(x: 5 + CGFloat(i) * (length - 20) / 3, y: 4.6, width: (length - 20) / 3 - 2.5, height: width - 9.2), 1.2, color.lighter(0.22))149 }150 case .truck:151 let cab = CGRect(x: length - 13, y: 0.8, width: 12.4, height: width - 1.6)152 body(cab, 3, color)153 fill(CGRect(x: length - 8.5, y: 2.2, width: 4.4, height: width - 4.4), 1.4, glass)154 let cargo = CGRect(x: 0.4, y: 0.4, width: length - 14.6, height: width - 0.8)155 body(cargo, 1.8, UIColor(hex: 0xD9DEF2))156 ctx.setStrokeColor(UIColor(hex: 0x8D95BA).cgColor)157 ctx.setLineWidth(0.6)158 for i in 1..<5 {159 let x = cargo.minX + cargo.width * CGFloat(i) / 5160 ctx.move(to: CGPoint(x: x, y: cargo.minY + 1.4))161 ctx.addLine(to: CGPoint(x: x, y: cargo.maxY - 1.4))162 }163 ctx.strokePath()164 fill(CGRect(x: cargo.minX + 2, y: width / 2 - 1.3, width: cargo.width - 4, height: 2.6), 1, color.withAlphaComponent(0.9))165 case .ambulance:166 body(full, 3.6, color)167 fill(CGRect(x: length - 9, y: 1.8, width: 4.6, height: width - 3.6), 1.6, glass)168 let red = UIColor(hex: 0xFF3B55)169 fill(CGRect(x: 2, y: 1.2, width: length - 12, height: 1.5), 0.5, red)170 fill(CGRect(x: 2, y: width - 2.7, width: length - 12, height: 1.5), 0.5, red)171 fill(CGRect(x: length * 0.34 - 1.3, y: width / 2 - 4, width: 2.6, height: 8), 0.6, red)172 fill(CGRect(x: length * 0.34 - 4, y: width / 2 - 1.3, width: 8, height: 2.6), 0.6, red)173 }174 // Lights: headlights at the nose, tail lights at the back.175 fill(CGRect(x: length - 2.3, y: 1.8, width: 1.7, height: 2.6), 0.7, head)176 fill(CGRect(x: length - 2.3, y: width - 4.4, width: 1.7, height: 2.6), 0.7, head)177 if kind != .sport {178 fill(CGRect(x: 0.5, y: 1.8, width: 1.3, height: 2.4), 0.5, tail)179 fill(CGRect(x: 0.5, y: width - 4.2, width: 1.3, height: 2.4), 0.5, tail)180 }181 }182 }183184 private static func shadedBody(_ ctx: CGContext, _ rect: CGRect, _ radius: CGFloat, _ c: UIColor) {185 let path = UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath186 ctx.saveGState()187 ctx.addPath(path)188 ctx.clip()189 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),190 colors: [c.lighter(0.28).cgColor, c.cgColor, c.darker(0.32).cgColor] as CFArray, locations: [0, 0.45, 1])!191 ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: rect.minY), end: CGPoint(x: 0, y: rect.maxY), options: [])192 ctx.restoreGState()193 ctx.addPath(path)194 ctx.setStrokeColor(UIColor.black.withAlphaComponent(0.45).cgColor)195 ctx.setLineWidth(0.7)196 ctx.strokePath()197 }198199 static var dot: SKTexture {200 TextureFactory.texture("traffic-dot", size: CGSize(width: 6, height: 6)) { ctx, size in201 ctx.setFillColor(UIColor.white.cgColor)202 ctx.fillEllipse(in: CGRect(origin: .zero, size: size))203 }204 }205206 static var chevron: SKTexture {207 TextureFactory.texture("traffic-chevron", size: CGSize(width: 18, height: 22)) { ctx, size in208 ctx.setStrokeColor(UIColor.white.cgColor)209 ctx.setLineWidth(4)210 ctx.setLineCap(.round)211 ctx.setLineJoin(.round)212 ctx.move(to: CGPoint(x: 5, y: 4))213 ctx.addLine(to: CGPoint(x: 13, y: size.height / 2))214 ctx.addLine(to: CGPoint(x: 5, y: size.height - 4))215 ctx.strokePath()216 }217 }218219 // MARK: City background220221 /// Bakes the whole static scene into one texture. `scale` is points per logical unit; the layout's focus sits at the centre.222 static func cityTexture(layout: TrafficLayout, size: CGSize, scale k: CGFloat) -> SKTexture {223 let key = "traffic-city-\(layout.id)-\(Int(size.width))x\(Int(size.height))-\(Int(k * 1000))"224 return TextureFactory.texture(key, size: size) { ctx, size in225 ctx.setFillColor(UIColor(hex: 0x0B1022).cgColor)226 ctx.fill(CGRect(origin: .zero, size: size))227 ctx.saveGState()228 // Logical space: origin at the focus, y up.229 ctx.translateBy(x: size.width / 2, y: size.height / 2)230 ctx.scaleBy(x: k, y: -k)231 ctx.translateBy(x: -CGFloat(layout.focus.x), y: -CGFloat(layout.focus.y))232 let halfW = Double(size.width / k) / 2 + 30, halfH = Double(size.height / k) / 2 + 30233 let view = TrafficRect(minX: layout.focus.x - halfW, minY: layout.focus.y - halfH, maxX: layout.focus.x + halfW, maxY: layout.focus.y + halfH)234 drawBlocks(ctx, layout: layout, view: view)235 drawRoads(ctx, layout: layout, view: view)236 drawLamps(ctx, layout: layout)237 ctx.restoreGState()238 // Vignette pulls the eye to the junction.239 let colors = [UIColor.clear.cgColor, UIColor(hex: 0x05060D, alpha: 0.55).cgColor] as CFArray240 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0.45, 1])!241 let center = CGPoint(x: size.width / 2, y: size.height / 2)242 ctx.drawRadialGradient(gradient, startCenter: center, startRadius: 0, endCenter: center, endRadius: size.height * 0.62, options: [.drawsAfterEndLocation])243 }244 }245246 private static func cg(_ r: TrafficRect) -> CGRect { CGRect(x: r.minX, y: r.minY, width: r.maxX - r.minX, height: r.maxY - r.minY) }247248 private static func rounded(_ ctx: CGContext, _ rect: CGRect, _ radius: CGFloat, _ color: UIColor) {249 ctx.setFillColor(color.cgColor)250 ctx.addPath(UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath)251 ctx.fillPath()252 }253254 private static func drawBlocks(_ ctx: CGContext, layout: TrafficLayout, view: TrafficRect) {255 let cell = 46.0256 let roofs: [UInt32] = [0x2B3566, 0x3A2F6B, 0x1F4A5E, 0x4A2E5C, 0x27406F, 0x35356E]257 let x0 = Int((view.minX / cell).rounded(.down)), x1 = Int((view.maxX / cell).rounded(.up))258 let y0 = Int((view.minY / cell).rounded(.down)), y1 = Int((view.maxY / cell).rounded(.up))259 // Draw from the top of the screen down so nearer buildings overlap the ones behind.260 for iy in stride(from: y1, through: y0, by: -1) {261 for ix in x0...x1 {262 var rng = SeededRNG.derive(UInt64(layout.id + 1) &* 0x9E37, ix &* 7919 &+ iy &* 104_729)263 let inset = rng.double(in: 3...6)264 let rect = TrafficRect(minX: Double(ix) * cell + inset, minY: Double(iy) * cell + inset,265 maxX: Double(ix + 1) * cell - inset, maxY: Double(iy + 1) * cell - inset)266 if layout.roads.contains(where: { $0.expanded(11).intersects(rect) }) { continue }267 let r = cg(rect)268 let roll = rng.unit()269 if roll < 0.2 {270 rounded(ctx, r, 7, UIColor(hex: 0x12382F))271 for _ in 0..<rng.int(in: 2...4) {272 let radius = rng.double(in: 5...8.5)273 let c = CGPoint(x: rng.double(in: rect.minX + radius...rect.maxX - radius), y: rng.double(in: rect.minY + radius...rect.maxY - radius))274 ctx.setFillColor(UIColor(hex: 0x05060D, alpha: 0.35).cgColor)275 ctx.fillEllipse(in: CGRect(x: c.x - radius + 2, y: c.y - radius - 3, width: radius * 2, height: radius * 2))276 ctx.setFillColor(UIColor(hex: 0x1E6B55).cgColor)277 ctx.fillEllipse(in: CGRect(x: c.x - radius, y: c.y - radius, width: radius * 2, height: radius * 2))278 ctx.setFillColor(UIColor(hex: 0x33A982).cgColor)279 ctx.fillEllipse(in: CGRect(x: c.x - radius * 0.75, y: c.y - radius * 0.25, width: radius, height: radius))280 }281 } else if roll < 0.3 {282 rounded(ctx, r, 6, UIColor(hex: 0x171D3A))283 ctx.setFillColor(UIColor(hex: 0xFFFFFF, alpha: 0.10).cgColor)284 var px = rect.minX + 6285 while px < rect.maxX - 4 {286 ctx.fill(CGRect(x: px, y: rect.minY + 5, width: 1, height: rect.maxY - rect.minY - 10))287 px += 7288 }289 ctx.setFillColor(UIColor(hex: palette[rng.int(in: 0...6)], alpha: 0.85).cgColor)290 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: rect.minX + 7, y: rect.minY + 8, width: 5, height: 10), cornerRadius: 1.5).cgPath)291 ctx.fillPath()292 } else {293 let roof = UIColor(hex: roofs[rng.int(in: 0...(roofs.count - 1))])294 let height = CGFloat(rng.double(in: 5...11))295 // Ground shadow, front wall, then the roof: a simple three-quarter look.296 rounded(ctx, r.offsetBy(dx: 4, dy: -height - 4), 5, UIColor(hex: 0x05060D, alpha: 0.5))297 rounded(ctx, CGRect(x: r.minX, y: r.minY - height, width: r.width, height: r.height), 5, roof.darker(0.55))298 var wx = Double(r.minX) + 4299 while wx < Double(r.maxX) - 4 {300 if rng.chance(0.55) {301 ctx.setFillColor(UIColor(hex: rng.chance(0.75) ? 0xFFD98A : 0x7FE3FF, alpha: 0.9).cgColor)302 ctx.fill(CGRect(x: wx, y: Double(r.minY) - Double(height) + 1.6, width: 2.2, height: Double(height) - 3.4))303 }304 wx += 4.6305 }306 rounded(ctx, r, 5, roof)307 ctx.addPath(UIBezierPath(roundedRect: r.insetBy(dx: 2.2, dy: 2.2), cornerRadius: 3.4).cgPath)308 ctx.setStrokeColor(roof.lighter(0.22).cgColor)309 ctx.setLineWidth(1)310 ctx.strokePath()311 if rng.chance(0.6) {312 let w = rng.double(in: 6...11), hh = rng.double(in: 5...9)313 let bx = rng.double(in: Double(r.minX) + 5...Double(r.maxX) - 5 - w), by = rng.double(in: Double(r.minY) + 5...Double(r.maxY) - 5 - hh)314 rounded(ctx, CGRect(x: bx + 1.2, y: by - 1.6, width: w, height: hh), 1.5, UIColor(hex: 0x05060D, alpha: 0.4))315 rounded(ctx, CGRect(x: bx, y: by, width: w, height: hh), 1.5, roof.lighter(0.12))316 }317 }318 }319 }320 }321322 private static func drawRoads(_ ctx: CGContext, layout: TrafficLayout, view: TrafficRect) {323 for road in layout.roads { rounded(ctx, cg(road.expanded(7)), 2, UIColor(hex: 0x3A4272)) }324 for road in layout.roads {325 ctx.setFillColor(UIColor(hex: 0x141930).cgColor)326 ctx.fill(cg(road))327 }328 for box in layout.boxes {329 ctx.setFillColor(UIColor(hex: 0x1B2140).cgColor)330 ctx.fill(cg(box))331 }332 for marking in layout.markings {333 let from = CGPoint(x: marking.from.x, y: marking.from.y), to = CGPoint(x: marking.to.x, y: marking.to.y)334 switch marking.kind {335 case .centerLine, .laneDash, .stopLine:336 ctx.saveGState()337 ctx.move(to: from)338 ctx.addLine(to: to)339 switch marking.kind {340 case .centerLine:341 ctx.setStrokeColor(UIColor(hex: 0xFFC94D, alpha: 0.6).cgColor)342 ctx.setLineWidth(1.5)343 ctx.setLineDash(phase: 0, lengths: [9, 7])344 case .laneDash:345 ctx.setStrokeColor(UIColor(hex: 0xFFFFFF, alpha: 0.36).cgColor)346 ctx.setLineWidth(1.3)347 ctx.setLineDash(phase: 0, lengths: [7, 9])348 default:349 ctx.setStrokeColor(UIColor(hex: 0xFFFFFF, alpha: 0.92).cgColor)350 ctx.setLineWidth(2.6)351 ctx.setLineCap(.round)352 }353 ctx.strokePath()354 ctx.restoreGState()355 case .crosswalk:356 let dx = to.x - from.x, dy = to.y - from.y357 let length = (dx * dx + dy * dy).squareRoot()358 guard length > 0 else { continue }359 let ux = dx / length, uy = dy / length360 ctx.setFillColor(UIColor(hex: 0xFFFFFF, alpha: 0.5).cgColor)361 var d = 1.5362 while d + 3.4 <= length {363 let cx = from.x + ux * (d + 1.7), cy = from.y + uy * (d + 1.7)364 let w = abs(ux) > 0.5 ? 3.4 : marking.depth, h = abs(ux) > 0.5 ? marking.depth : 3.4365 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: cx - w / 2, y: cy - h / 2, width: w, height: h), cornerRadius: 0.8).cgPath)366 ctx.fillPath()367 d += 6.6368 }369 }370 }371 }372373 private static func drawLamps(_ ctx: CGContext, layout: TrafficLayout) {374 ctx.saveGState()375 ctx.setBlendMode(.plusLighter)376 let colors = [UIColor(hex: 0xFFD98A, alpha: 0.30).cgColor, UIColor(hex: 0xFFD98A, alpha: 0).cgColor] as CFArray377 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 1])!378 for box in layout.boxes {379 for corner in [CGPoint(x: box.minX - 6, y: box.minY - 6), CGPoint(x: box.maxX + 6, y: box.minY - 6),380 CGPoint(x: box.minX - 6, y: box.maxY + 6), CGPoint(x: box.maxX + 6, y: box.maxY + 6)] {381 ctx.drawRadialGradient(gradient, startCenter: corner, startRadius: 0, endCenter: corner, endRadius: 34, options: [])382 ctx.setFillColor(UIColor(hex: 0xFFF3C4).cgColor)383 ctx.fillEllipse(in: CGRect(x: corner.x - 1.6, y: corner.y - 1.6, width: 3.2, height: 3.2))384 }385 }386 ctx.restoreGState()387 }388}389