Repository Public License
1// ORIX Arcade2// File: Games/DigToCore/DigArt.swift3// Description: Dig to Core — all art drawn in code: terrain, ore, hazard and pickup tiles, the drill and its skins, light and surface textures.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SpriteKit8import UIKit910enum DigArt {11 static let tilePoints: CGFloat = 441213 // MARK: Tiles1415 /// Texture for a tile. `variant` (0…3) picks strata band and speckle pattern so large areas do not look stamped.16 static func tile(_ tile: DigTile, biome: DigBiome, variant: Int) -> SKTexture? {17 guard tile != .air else { return nil }18 let band = variant & 1, speckle = (variant >> 1) & 119 let key: String20 switch tile {21 case .boulder, .lava, .gas, .fuelCell, .repairKit, .chest, .core: key = "dig-\(tile.rawValue)"22 default: key = "dig-\(tile.rawValue)-\(biome.rawValue)-\(band)-\(speckle)"23 }24 return TextureFactory.texture(key, size: CGSize(width: tilePoints, height: tilePoints)) { ctx, size in25 draw(tile, biome: biome, band: band, speckle: speckle, in: ctx, size: size)26 }27 }2829 private static func draw(_ tile: DigTile, biome: DigBiome, band: Int, speckle: Int, in ctx: CGContext, size: CGSize) {30 let rect = CGRect(origin: .zero, size: size)31 let groundColor = UIColor(hex: band == 0 ? biome.tileHex.0 : biome.tileHex.1)32 var rng = SeededRNG(seed: UInt64(tile.rawValue) &* 31 &+ UInt64(biome.rawValue) &* 7 &+ UInt64(speckle) &* 101 &+ UInt64(band))33 switch tile {34 case .ground:35 block(ctx, rect, groundColor)36 speckles(ctx, rect, groundColor, &rng, count: 7)37 if biome == .loam { roots(ctx, rect, &rng) }38 if biome == .crystal, speckle == 1 { glints(ctx, rect, UIColor(hex: biome.accentHex), &rng, count: 2) }39 if biome == .mantle { glints(ctx, rect, UIColor(hex: biome.accentHex), &rng, count: 2) }40 if biome == .magma { cracks(ctx, rect, UIColor(hex: 0xFF7A3D, alpha: 0.35), &rng, count: 1) }41 case .hardRock:42 let dark = groundColor.darker(0.42)43 block(ctx, rect, dark)44 cracks(ctx, rect, UIColor.black.withAlphaComponent(0.55), &rng, count: 3)45 cracks(ctx, rect, UIColor.white.withAlphaComponent(0.16), &rng, count: 2)46 // Corner studs mark it as "slow" rather than impassable.47 ctx.setFillColor(dark.lighter(0.25).cgColor)48 for corner in [CGPoint(x: 9, y: 9), CGPoint(x: size.width - 9, y: size.height - 9)] {49 ctx.fillEllipse(in: CGRect(x: corner.x - 2.5, y: corner.y - 2.5, width: 5, height: 5))50 }51 case .boulder:52 // Riveted steel-blue plate: clearly not diggable in any biome.53 block(ctx, rect, UIColor(hex: 0x2A3350), radius: 9, highlight: 0.22)54 ctx.setStrokeColor(UIColor(hex: 0x8FA2D9, alpha: 0.9).cgColor)55 ctx.setLineWidth(2)56 ctx.addPath(UIBezierPath(roundedRect: rect.insetBy(dx: 5, dy: 5), cornerRadius: 6).cgPath)57 ctx.strokePath()58 ctx.setLineWidth(3)59 ctx.setLineCap(.round)60 ctx.move(to: CGPoint(x: 15, y: 15)); ctx.addLine(to: CGPoint(x: size.width - 15, y: size.height - 15))61 ctx.move(to: CGPoint(x: size.width - 15, y: 15)); ctx.addLine(to: CGPoint(x: 15, y: size.height - 15))62 ctx.strokePath()63 case .fossil:64 block(ctx, rect, groundColor)65 speckles(ctx, rect, groundColor, &rng, count: 4)66 ammonite(ctx, rect)67 case .lava:68 lava(ctx, rect, &rng)69 case .gas:70 gas(ctx, rect, &rng)71 case .fuelCell:72 fuelCell(ctx, rect)73 case .repairKit:74 repairKit(ctx, rect)75 case .chest:76 chest(ctx, rect)77 case .core:78 let colors = [UIColor(hex: 0xFFFFFF).cgColor, UIColor(hex: 0xFFE58A).cgColor, UIColor(hex: 0xFF9A3D).cgColor] as CFArray79 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 0.5, 1])!80 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 0, y: size.height), options: [])81 default:82 // Ores: ground with three faceted gems.83 block(ctx, rect, groundColor)84 speckles(ctx, rect, groundColor, &rng, count: 3)85 gems(ctx, rect, tile, &rng)86 }87 }8889 /// A chunky rounded block with a top highlight and bottom shade — the basic voxel look.90 private static func block(_ ctx: CGContext, _ rect: CGRect, _ color: UIColor, radius: CGFloat = 7, highlight: CGFloat = 0.16) {91 let inset = rect.insetBy(dx: 0.6, dy: 0.6)92 let path = UIBezierPath(roundedRect: inset, cornerRadius: radius).cgPath93 ctx.saveGState()94 ctx.addPath(path)95 ctx.clip()96 let colors = [color.lighter(0.10).cgColor, color.cgColor, color.darker(0.20).cgColor] as CFArray97 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 0.45, 1])!98 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 0, y: rect.height), options: [])99 ctx.setFillColor(UIColor.white.withAlphaComponent(highlight).cgColor)100 ctx.fill(CGRect(x: 0, y: 0, width: rect.width, height: 4))101 ctx.setFillColor(UIColor.black.withAlphaComponent(0.22).cgColor)102 ctx.fill(CGRect(x: 0, y: rect.height - 4, width: rect.width, height: 4))103 ctx.restoreGState()104 }105106 private static func speckles(_ ctx: CGContext, _ rect: CGRect, _ base: UIColor, _ rng: inout SeededRNG, count: Int) {107 for index in 0..<count {108 let dark = index.isMultiple(of: 2)109 ctx.setFillColor((dark ? base.darker(0.28) : base.lighter(0.2)).withAlphaComponent(0.8).cgColor)110 let w = CGFloat(rng.double(in: 3...7)), h = CGFloat(rng.double(in: 2...4))111 let x = CGFloat(rng.double(in: 5...Double(rect.width - 12))), y = CGFloat(rng.double(in: 7...Double(rect.height - 11)))112 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: x, y: y, width: w, height: h), cornerRadius: 1.5).cgPath)113 ctx.fillPath()114 }115 }116117 private static func cracks(_ ctx: CGContext, _ rect: CGRect, _ color: UIColor, _ rng: inout SeededRNG, count: Int) {118 ctx.setStrokeColor(color.cgColor)119 ctx.setLineWidth(1.6)120 ctx.setLineCap(.round)121 ctx.setLineJoin(.round)122 for _ in 0..<count {123 var point = CGPoint(x: rng.double(in: 8...Double(rect.width - 8)), y: rng.double(in: 8...Double(rect.height - 8)))124 ctx.move(to: point)125 for _ in 0..<3 {126 point = CGPoint(x: min(max(point.x + rng.double(in: -11...11), 5), rect.width - 5), y: min(max(point.y + rng.double(in: -11...11), 5), rect.height - 5))127 ctx.addLine(to: point)128 }129 ctx.strokePath()130 }131 }132133 private static func roots(_ ctx: CGContext, _ rect: CGRect, _ rng: inout SeededRNG) {134 guard rng.chance(0.5) else { return }135 ctx.setStrokeColor(UIColor(hex: 0x8BE07A, alpha: 0.45).cgColor)136 ctx.setLineWidth(1.5)137 ctx.setLineCap(.round)138 let x = rng.double(in: 10...Double(rect.width - 10))139 ctx.move(to: CGPoint(x: x, y: 6))140 ctx.addQuadCurve(to: CGPoint(x: x + rng.double(in: -9...9), y: 26), control: CGPoint(x: x + rng.double(in: -10...10), y: 15))141 ctx.strokePath()142 }143144 private static func glints(_ ctx: CGContext, _ rect: CGRect, _ color: UIColor, _ rng: inout SeededRNG, count: Int) {145 ctx.setFillColor(color.withAlphaComponent(0.75).cgColor)146 for _ in 0..<count {147 let c = CGPoint(x: rng.double(in: 9...Double(rect.width - 9)), y: rng.double(in: 9...Double(rect.height - 9)))148 let r = CGFloat(rng.double(in: 1.6...3))149 ctx.move(to: CGPoint(x: c.x, y: c.y - r * 1.8)); ctx.addLine(to: CGPoint(x: c.x + r, y: c.y))150 ctx.addLine(to: CGPoint(x: c.x, y: c.y + r * 1.8)); ctx.addLine(to: CGPoint(x: c.x - r, y: c.y))151 ctx.closePath()152 ctx.fillPath()153 }154 }155156 private static func gems(_ ctx: CGContext, _ rect: CGRect, _ ore: DigTile, _ rng: inout SeededRNG) {157 let color = UIColor(hex: ore.colorHex)158 let centres = [CGPoint(x: rect.width * 0.34, y: rect.height * 0.36), CGPoint(x: rect.width * 0.66, y: rect.height * 0.52), CGPoint(x: rect.width * 0.4, y: rect.height * 0.72)]159 let precious = (ore.oreValue ?? 0) >= 16160 for (index, centre) in centres.enumerated() {161 let r = CGFloat(rng.double(in: 5...6.6)) + (index == 1 ? 1.4 : 0)162 let c = CGPoint(x: centre.x + rng.double(in: -2...2), y: centre.y + rng.double(in: -2...2))163 let path = UIBezierPath()164 if precious {165 // Cut gem: a hexagonal facet.166 for step in 0..<6 {167 let angle = Double(step) * .pi / 3 + .pi / 6168 let p = CGPoint(x: c.x + cos(angle) * r, y: c.y + sin(angle) * r * 1.1)169 if step == 0 { path.move(to: p) } else { path.addLine(to: p) }170 }171 path.close()172 } else {173 // Raw nugget.174 path.append(UIBezierPath(roundedRect: CGRect(x: c.x - r, y: c.y - r * 0.8, width: r * 2, height: r * 1.6), cornerRadius: r * 0.5))175 }176 ctx.setShadow(offset: CGSize(width: 0, height: 1.5), blur: 2, color: UIColor.black.withAlphaComponent(0.5).cgColor)177 ctx.setFillColor(color.cgColor)178 ctx.addPath(path.cgPath)179 ctx.fillPath()180 ctx.setShadow(offset: .zero, blur: 0, color: nil)181 ctx.setFillColor(UIColor.white.withAlphaComponent(ore == .coal ? 0.28 : 0.6).cgColor)182 ctx.fillEllipse(in: CGRect(x: c.x - r * 0.55, y: c.y - r * 0.6, width: r * 0.6, height: r * 0.45))183 ctx.setFillColor(UIColor.black.withAlphaComponent(0.18).cgColor)184 ctx.fill(CGRect(x: c.x - r * 0.2, y: c.y + r * 0.25, width: r * 0.9, height: r * 0.3))185 }186 }187188 private static func ammonite(_ ctx: CGContext, _ rect: CGRect) {189 let c = CGPoint(x: rect.midX, y: rect.midY)190 ctx.setStrokeColor(UIColor(hex: 0xEFE3C2).cgColor)191 ctx.setLineWidth(3.4)192 ctx.setLineCap(.round)193 var first = true194 for step in 0...60 {195 let t = Double(step) / 60196 let angle = t * 4.4 * .pi, radius = 2 + t * 12.5197 let p = CGPoint(x: c.x + cos(angle) * radius, y: c.y + sin(angle) * radius)198 if first { ctx.move(to: p); first = false } else { ctx.addLine(to: p) }199 }200 ctx.strokePath()201 ctx.setStrokeColor(UIColor(hex: 0x8A7B55).cgColor)202 ctx.setLineWidth(1)203 for step in stride(from: 26, through: 60, by: 4) {204 let t = Double(step) / 60, angle = t * 4.4 * .pi, radius = 2 + t * 12.5205 ctx.move(to: CGPoint(x: c.x + cos(angle) * (radius - 1.6), y: c.y + sin(angle) * (radius - 1.6)))206 ctx.addLine(to: CGPoint(x: c.x + cos(angle) * (radius + 1.6), y: c.y + sin(angle) * (radius + 1.6)))207 ctx.strokePath()208 }209 }210211 private static func lava(_ ctx: CGContext, _ rect: CGRect, _ rng: inout SeededRNG) {212 let path = UIBezierPath(roundedRect: rect.insetBy(dx: 0.6, dy: 0.6), cornerRadius: 8).cgPath213 ctx.saveGState()214 ctx.addPath(path)215 ctx.clip()216 let colors = [UIColor(hex: 0xFFE066).cgColor, UIColor(hex: 0xFF8A2E).cgColor, UIColor(hex: 0xD92B1C).cgColor] as CFArray217 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 0.4, 1])!218 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 0, y: rect.height), options: [])219 ctx.setFillColor(UIColor(hex: 0xFFF4B0, alpha: 0.9).cgColor)220 for _ in 0..<4 {221 let r = CGFloat(rng.double(in: 2.5...5.5))222 ctx.fillEllipse(in: CGRect(x: rng.double(in: 6...Double(rect.width - 14)), y: rng.double(in: 6...Double(rect.height - 14)), width: r * 2, height: r * 2))223 }224 // Wavy crust line.225 ctx.setStrokeColor(UIColor(hex: 0x7A1408, alpha: 0.75).cgColor)226 ctx.setLineWidth(2.2)227 ctx.move(to: CGPoint(x: 0, y: rect.height * 0.62))228 ctx.addCurve(to: CGPoint(x: rect.width, y: rect.height * 0.58), control1: CGPoint(x: rect.width * 0.3, y: rect.height * 0.44),229 control2: CGPoint(x: rect.width * 0.65, y: rect.height * 0.8))230 ctx.strokePath()231 ctx.restoreGState()232 }233234 private static func gas(_ ctx: CGContext, _ rect: CGRect, _ rng: inout SeededRNG) {235 let path = UIBezierPath(roundedRect: rect.insetBy(dx: 1.5, dy: 1.5), cornerRadius: 9).cgPath236 ctx.setFillColor(UIColor(hex: 0x1B3A1E, alpha: 0.95).cgColor)237 ctx.addPath(path)238 ctx.fillPath()239 ctx.setStrokeColor(UIColor(hex: 0x9CFF6B).cgColor)240 ctx.setLineWidth(1.6)241 ctx.setLineDash(phase: 0, lengths: [4, 3])242 ctx.addPath(path)243 ctx.strokePath()244 ctx.setLineDash(phase: 0, lengths: [])245 for (cx, cy, r) in [(0.36, 0.60, 8.0), (0.64, 0.42, 6.0), (0.62, 0.72, 3.6)] {246 let circle = CGRect(x: rect.width * cx - r, y: rect.height * cy - r, width: r * 2, height: r * 2)247 ctx.setFillColor(UIColor(hex: 0x9CFF6B, alpha: 0.35).cgColor)248 ctx.fillEllipse(in: circle)249 ctx.setStrokeColor(UIColor(hex: 0xD2FFB8).cgColor)250 ctx.setLineWidth(1.4)251 ctx.strokeEllipse(in: circle)252 ctx.setFillColor(UIColor.white.withAlphaComponent(0.8).cgColor)253 ctx.fillEllipse(in: CGRect(x: circle.minX + r * 0.45, y: circle.minY + r * 0.4, width: r * 0.4, height: r * 0.4))254 }255 _ = rng.next()256 }257258 private static func glow(_ ctx: CGContext, _ rect: CGRect, _ color: UIColor) {259 let colors = [color.withAlphaComponent(0.55).cgColor, color.withAlphaComponent(0).cgColor] as CFArray260 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 1])!261 ctx.drawRadialGradient(gradient, startCenter: CGPoint(x: rect.midX, y: rect.midY), startRadius: 2, endCenter: CGPoint(x: rect.midX, y: rect.midY),262 endRadius: rect.width / 2, options: [])263 }264265 private static func fuelCell(_ ctx: CGContext, _ rect: CGRect) {266 let color = UIColor(hex: 0x4DD8FF)267 glow(ctx, rect, color)268 let body = CGRect(x: rect.midX - 9, y: rect.midY - 12, width: 18, height: 25)269 ctx.setFillColor(UIColor(hex: 0x0E2A3A).cgColor)270 ctx.addPath(UIBezierPath(roundedRect: body, cornerRadius: 5).cgPath)271 ctx.fillPath()272 ctx.setStrokeColor(color.cgColor)273 ctx.setLineWidth(2)274 ctx.addPath(UIBezierPath(roundedRect: body, cornerRadius: 5).cgPath)275 ctx.strokePath()276 ctx.setFillColor(color.cgColor)277 ctx.fill(CGRect(x: rect.midX - 4, y: body.minY - 3.5, width: 8, height: 4))278 // Lightning bolt.279 ctx.setFillColor(UIColor(hex: 0xFFE066).cgColor)280 ctx.move(to: CGPoint(x: rect.midX + 2.5, y: body.minY + 4))281 ctx.addLine(to: CGPoint(x: rect.midX - 4.5, y: body.midY + 1.5))282 ctx.addLine(to: CGPoint(x: rect.midX - 0.5, y: body.midY + 1.5))283 ctx.addLine(to: CGPoint(x: rect.midX - 2.5, y: body.maxY - 4))284 ctx.addLine(to: CGPoint(x: rect.midX + 4.5, y: body.midY - 1.5))285 ctx.addLine(to: CGPoint(x: rect.midX + 0.5, y: body.midY - 1.5))286 ctx.closePath()287 ctx.fillPath()288 }289290 private static func repairKit(_ ctx: CGContext, _ rect: CGRect) {291 let color = UIColor(hex: 0x4DE3A1)292 glow(ctx, rect, color)293 let body = CGRect(x: rect.midX - 12, y: rect.midY - 10, width: 24, height: 21)294 ctx.setFillColor(UIColor(hex: 0x0F3327).cgColor)295 ctx.addPath(UIBezierPath(roundedRect: body, cornerRadius: 5).cgPath)296 ctx.fillPath()297 ctx.setStrokeColor(color.cgColor)298 ctx.setLineWidth(2)299 ctx.addPath(UIBezierPath(roundedRect: body, cornerRadius: 5).cgPath)300 ctx.strokePath()301 ctx.setLineWidth(2)302 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: rect.midX - 5, y: body.minY - 5, width: 10, height: 7), cornerRadius: 3).cgPath)303 ctx.strokePath()304 ctx.setFillColor(color.cgColor)305 ctx.fill(CGRect(x: rect.midX - 2, y: body.midY - 6.5, width: 4, height: 13))306 ctx.fill(CGRect(x: rect.midX - 6.5, y: body.midY - 2, width: 13, height: 4))307 }308309 private static func chest(_ ctx: CGContext, _ rect: CGRect) {310 glow(ctx, rect, UIColor(hex: 0xFFC94D))311 let body = CGRect(x: rect.midX - 14, y: rect.midY - 9, width: 28, height: 21)312 ctx.setFillColor(UIColor(hex: 0x8A4B24).cgColor)313 ctx.addPath(UIBezierPath(roundedRect: body, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 9, height: 9)).cgPath)314 ctx.fillPath()315 ctx.setFillColor(UIColor(hex: 0x6B3618).cgColor)316 ctx.fill(CGRect(x: body.minX, y: body.midY, width: body.width, height: body.height / 2))317 ctx.setFillColor(UIColor(hex: 0xFFC94D).cgColor)318 ctx.fill(CGRect(x: body.minX, y: body.midY - 1.5, width: body.width, height: 3))319 ctx.fill(CGRect(x: body.minX + 4, y: body.minY + 2, width: 3, height: body.height - 2))320 ctx.fill(CGRect(x: body.maxX - 7, y: body.minY + 2, width: 3, height: body.height - 2))321 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: rect.midX - 3.5, y: body.midY - 4, width: 7, height: 8), cornerRadius: 2).cgPath)322 ctx.fillPath()323 ctx.setFillColor(UIColor(hex: 0x3B1D0C).cgColor)324 ctx.fillEllipse(in: CGRect(x: rect.midX - 1.2, y: body.midY - 1.5, width: 2.4, height: 2.4))325 }326327 // MARK: Drill328329 static let drillPoints = CGSize(width: 44, height: 68)330331 /// Draws the drill, tip at the bottom centre. `frame` alternates the bit's spiral so it appears to spin.332 static func drawDrill(_ ctx: CGContext, size: CGSize, skin: DigSkin, frame: Int) {333 let w = size.width, h = size.height334 let body = UIColor(hex: skin.bodyHex), trim = UIColor(hex: skin.trimHex), glass = UIColor(hex: skin.glassHex)335 let bodyRect = CGRect(x: w * 0.14, y: h * 0.10, width: w * 0.72, height: h * 0.52)336 // Treads.337 for x in [w * 0.03, w * 0.82] {338 let tread = CGRect(x: x, y: h * 0.20, width: w * 0.15, height: h * 0.40)339 ctx.setFillColor(trim.cgColor)340 ctx.addPath(UIBezierPath(roundedRect: tread, cornerRadius: w * 0.06).cgPath)341 ctx.fillPath()342 ctx.setFillColor(UIColor.white.withAlphaComponent(0.18).cgColor)343 for step in 0..<4 {344 ctx.fill(CGRect(x: tread.minX + 1.5, y: tread.minY + 4 + CGFloat(step) * tread.height / 4.4 + CGFloat(frame) * 2.4, width: tread.width - 3, height: 2))345 }346 }347 // Bit collar.348 ctx.setFillColor(trim.cgColor)349 ctx.addPath(UIBezierPath(roundedRect: CGRect(x: w * 0.24, y: h * 0.60, width: w * 0.52, height: h * 0.08), cornerRadius: 2).cgPath)350 ctx.fillPath()351 // Drill bit: a cone with spiral flutes.352 let cone = UIBezierPath()353 cone.move(to: CGPoint(x: w * 0.22, y: h * 0.67))354 cone.addLine(to: CGPoint(x: w * 0.78, y: h * 0.67))355 cone.addLine(to: CGPoint(x: w * 0.5, y: h))356 cone.close()357 ctx.saveGState()358 ctx.addPath(cone.cgPath)359 ctx.clip()360 let steel = [UIColor(hex: 0xF2F5FF).cgColor, UIColor(hex: 0x9AA3BF).cgColor, UIColor(hex: 0x5C6480).cgColor] as CFArray361 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: steel, locations: [0, 0.5, 1])!362 ctx.drawLinearGradient(gradient, start: CGPoint(x: w * 0.22, y: 0), end: CGPoint(x: w * 0.78, y: 0), options: [])363 ctx.setStrokeColor(UIColor(hex: 0x363C55).cgColor)364 ctx.setLineWidth(2.4)365 let offset = CGFloat(frame) * h * 0.035366 for step in -1..<6 {367 let y = h * 0.66 + CGFloat(step) * h * 0.07 + offset368 ctx.move(to: CGPoint(x: w * 0.15, y: y + h * 0.05))369 ctx.addLine(to: CGPoint(x: w * 0.85, y: y - h * 0.03))370 }371 ctx.strokePath()372 ctx.restoreGState()373 // Body.374 let bodyPath = UIBezierPath(roundedRect: bodyRect, cornerRadius: w * 0.22).cgPath375 ctx.saveGState()376 ctx.addPath(bodyPath)377 ctx.clip()378 let paint = [body.lighter(0.25).cgColor, body.cgColor, body.darker(0.3).cgColor] as CFArray379 let bodyGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: paint, locations: [0, 0.5, 1])!380 ctx.drawLinearGradient(bodyGradient, start: CGPoint(x: bodyRect.minX, y: bodyRect.minY), end: CGPoint(x: bodyRect.maxX, y: bodyRect.maxY), options: [])381 // Hazard stripe band.382 ctx.setFillColor(trim.cgColor)383 ctx.fill(CGRect(x: bodyRect.minX, y: bodyRect.maxY - h * 0.10, width: bodyRect.width, height: h * 0.045))384 ctx.restoreGState()385 ctx.setStrokeColor(trim.cgColor)386 ctx.setLineWidth(1.6)387 ctx.addPath(bodyPath)388 ctx.strokePath()389 // Cockpit window.390 let window = CGRect(x: w * 0.29, y: h * 0.17, width: w * 0.42, height: h * 0.20)391 ctx.setFillColor(trim.cgColor)392 ctx.addPath(UIBezierPath(roundedRect: window.insetBy(dx: -2, dy: -2), cornerRadius: w * 0.14).cgPath)393 ctx.fillPath()394 ctx.saveGState()395 ctx.addPath(UIBezierPath(roundedRect: window, cornerRadius: w * 0.11).cgPath)396 ctx.clip()397 let glassColors = [glass.lighter(0.4).cgColor, glass.cgColor, glass.darker(0.45).cgColor] as CFArray398 let glassGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: glassColors, locations: [0, 0.45, 1])!399 ctx.drawLinearGradient(glassGradient, start: window.origin, end: CGPoint(x: window.maxX, y: window.maxY), options: [])400 ctx.setFillColor(UIColor.white.withAlphaComponent(0.55).cgColor)401 ctx.fillEllipse(in: CGRect(x: window.minX + 3, y: window.minY + 2, width: window.width * 0.36, height: window.height * 0.3))402 ctx.restoreGState()403 // Antenna light.404 ctx.setFillColor(trim.cgColor)405 ctx.fill(CGRect(x: w * 0.5 - 1, y: h * 0.035, width: 2, height: h * 0.07))406 ctx.setFillColor(UIColor(hex: frame == 0 ? 0xFF5470 : 0xFFB0BE).cgColor)407 ctx.fillEllipse(in: CGRect(x: w * 0.5 - 3, y: h * 0.005, width: 6, height: 6))408 }409410 static func drillTexture(skin: DigSkin, frame: Int) -> SKTexture {411 TextureFactory.texture("dig-drill-\(skin.id)-\(frame)", size: drillPoints) { ctx, size in drawDrill(ctx, size: size, skin: skin, frame: frame) }412 }413414 /// The drill as an image for SwiftUI (hangar, shop, lobby cover).415 static func drillImage(skin: DigSkin, scale: CGFloat = 3) -> UIImage {416 let size = CGSize(width: drillPoints.width * scale, height: drillPoints.height * scale)417 let format = UIGraphicsImageRendererFormat()418 format.scale = 2419 return UIGraphicsImageRenderer(size: size, format: format).image { context in420 context.cgContext.scaleBy(x: scale, y: scale)421 drawDrill(context.cgContext, size: drillPoints, skin: skin, frame: 0)422 }423 }424425 /// A single tile as an image for SwiftUI legends.426 static func tileImage(_ tile: DigTile, biome: DigBiome) -> UIImage {427 let size = CGSize(width: tilePoints, height: tilePoints)428 return UIGraphicsImageRenderer(size: size).image { context in429 draw(tile, biome: biome, band: 0, speckle: 0, in: context.cgContext, size: size)430 }431 }432433 // MARK: Lighting and scenery434435 /// Headlamp vignette: clear around the drill, darker toward the screen edges.436 static var lampTexture: SKTexture {437 TextureFactory.texture("dig-lamp2", size: CGSize(width: 128, height: 256)) { ctx, size in438 let colors = [UIColor.black.withAlphaComponent(0).cgColor, UIColor.black.withAlphaComponent(0.06).cgColor, UIColor.black.withAlphaComponent(0.42).cgColor] as CFArray439 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 0.45, 1])!440 ctx.saveGState()441 ctx.scaleBy(x: 1, y: size.height / size.width)442 let c = CGPoint(x: size.width / 2, y: size.width * 0.44)443 ctx.drawRadialGradient(gradient, startCenter: c, startRadius: 0, endCenter: c, endRadius: size.width * 0.38, options: [.drawsAfterEndLocation])444 ctx.restoreGState()445 }446 }447448 /// The sky, horizon glow and derrick above the dig site.449 static func surfaceTexture(width: CGFloat, height: CGFloat) -> SKTexture {450 TextureFactory.texture("dig-surface-\(Int(width))x\(Int(height))", size: CGSize(width: width, height: height)) { ctx, size in451 let colors = [UIColor(hex: 0x0A0C1A).cgColor, UIColor(hex: 0x16214A).cgColor, UIColor(hex: 0x2D6A6A).cgColor, UIColor(hex: 0xF2B36B).cgColor] as CFArray452 let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 0.45, 0.82, 1])!453 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: 0, y: size.height), options: [])454 var rng = SeededRNG(seed: 4242)455 for _ in 0..<70 {456 let y = rng.double(in: 0...Double(size.height * 0.7))457 ctx.setFillColor(UIColor.white.withAlphaComponent(rng.double(in: 0.25...0.9) * (1 - y / Double(size.height))).cgColor)458 let r = rng.double(in: 0.6...1.6)459 ctx.fillEllipse(in: CGRect(x: rng.double(in: 0...Double(size.width)), y: y, width: r * 2, height: r * 2))460 }461 // Distant hills.462 ctx.setFillColor(UIColor(hex: 0x12303A).cgColor)463 ctx.move(to: CGPoint(x: 0, y: size.height))464 ctx.addLine(to: CGPoint(x: 0, y: size.height - 46))465 ctx.addCurve(to: CGPoint(x: size.width, y: size.height - 58), control1: CGPoint(x: size.width * 0.3, y: size.height - 110),466 control2: CGPoint(x: size.width * 0.62, y: size.height - 10))467 ctx.addLine(to: CGPoint(x: size.width, y: size.height))468 ctx.closePath()469 ctx.fillPath()470 // Derrick over the shaft.471 let cx = size.width / 2, base = size.height472 ctx.setStrokeColor(UIColor(hex: 0x0B1620).cgColor)473 ctx.setLineWidth(4)474 ctx.setLineCap(.round)475 ctx.move(to: CGPoint(x: cx - 46, y: base)); ctx.addLine(to: CGPoint(x: cx - 10, y: base - 150))476 ctx.move(to: CGPoint(x: cx + 46, y: base)); ctx.addLine(to: CGPoint(x: cx + 10, y: base - 150))477 ctx.move(to: CGPoint(x: cx - 12, y: base - 150)); ctx.addLine(to: CGPoint(x: cx + 12, y: base - 150))478 ctx.strokePath()479 ctx.setLineWidth(2.4)480 for step in 0..<4 {481 let y0 = base - CGFloat(step) * 36, y1 = y0 - 36482 let half0 = 46 - CGFloat(step) * 8.64, half1 = half0 - 8.64483 ctx.move(to: CGPoint(x: cx - half0, y: y0)); ctx.addLine(to: CGPoint(x: cx + half1, y: y1))484 ctx.move(to: CGPoint(x: cx + half0, y: y0)); ctx.addLine(to: CGPoint(x: cx - half1, y: y1))485 ctx.move(to: CGPoint(x: cx - half1, y: y1)); ctx.addLine(to: CGPoint(x: cx + half1, y: y1))486 }487 ctx.strokePath()488 ctx.setFillColor(UIColor(hex: 0xFF5470).cgColor)489 ctx.fillEllipse(in: CGRect(x: cx - 4, y: base - 160, width: 8, height: 8))490 // Grass lip.491 ctx.setFillColor(UIColor(hex: 0x6FCB5F).cgColor)492 ctx.fill(CGRect(x: 0, y: size.height - 7, width: size.width, height: 7))493 ctx.setFillColor(UIColor(hex: 0xA6F08C).cgColor)494 ctx.fill(CGRect(x: 0, y: size.height - 7, width: size.width, height: 2))495 }496 }497}498