Skip to content
Source
Sign inSign up
samwillis/jerry
Open History to see the last commit

ExpeditionProps.swift

107 lines6.6 KB

1import AppKit
2import SpriteKit
3import ExpeditionCore
4​
5/// All props share the click-through scene. Only the explorer's body has an input panel.
6@MainActor
7final class ExpeditionProps: SKNode {
8 private let discovery = SKNode()
9 private let sock = SKNode()
10 private var sockStyle = -1
11 private let map = SKNode()
12 private let speech = SKLabelNode(fontNamed: "AvenirNext-DemiBold")
13 private let speechBacking = SKShapeNode(rectOf: CGSize(width: 106, height: 24), cornerRadius: 8)
14​
15 override init() {
16 super.init()
17 addChild(discovery)
18 discovery.addChild(sock)
19 styleSock(0)
20 let paper = SKShapeNode(rectOf: .init(width: 22, height: 16), cornerRadius: 1)
21 paper.fillColor = .init(calibratedRed: 0.94, green: 0.87, blue: 0.68, alpha: 1)
22 paper.strokeColor = .brown; paper.lineWidth = 0.7
23 map.addChild(paper)
24 let trail = CGMutablePath(); trail.move(to: .init(x: -8, y: -4)); trail.addLine(to: .init(x: -3, y: 4)); trail.addLine(to: .init(x: 5, y: -2))
25 let ink = SKShapeNode(path: trail); ink.strokeColor = .systemRed; ink.lineWidth = 1; map.addChild(ink)
26 addChild(map)
27 speech.fontSize = 10; speech.fontColor = .init(calibratedWhite: 0.15, alpha: 1)
28 speech.verticalAlignmentMode = .center; speech.zPosition = 3
29 speechBacking.fillColor = .init(calibratedRed: 1, green: 0.97, blue: 0.88, alpha: 0.97)
30 speechBacking.strokeColor = .init(calibratedWhite: 0.3, alpha: 0.9); speechBacking.lineWidth = 0.7
31 speechBacking.zPosition = -1; speech.addChild(speechBacking); addChild(speech)
32 }
33 required init?(coder: NSCoder) { fatalError("Use init()") }
34​
35 /// Four designs rotate between discoveries; every mark is clipped to the knit.
36 private func styleSock(_ style: Int) {
37 guard sockStyle != style else { return }
38 sockStyle = style; sock.removeAllChildren()
39 let bases: [NSColor] = [.init(calibratedRed: 0.72, green: 0.22, blue: 0.23, alpha: 1),
40 .init(calibratedRed: 0.17, green: 0.33, blue: 0.53, alpha: 1),
41 .init(calibratedRed: 0.17, green: 0.50, blue: 0.44, alpha: 1),
42 .init(calibratedRed: 0.49, green: 0.30, blue: 0.59, alpha: 1)]
43 let cream = NSColor(calibratedRed: 1, green: 0.91, blue: 0.68, alpha: 1)
44 let ink = NSColor(calibratedRed: 0.19, green: 0.20, blue: 0.23, alpha: 1)
45 let shape = CGMutablePath()
46 shape.move(to: .init(x: -5, y: 24)); shape.addLine(to: .init(x: 6, y: 24))
47 shape.addLine(to: .init(x: 6, y: 10))
48 shape.addQuadCurve(to: .init(x: 15, y: 8), control: .init(x: 18, y: 11))
49 shape.addQuadCurve(to: .init(x: 14, y: 2), control: .init(x: 19, y: 4))
50 shape.addLine(to: .init(x: 0, y: 0))
51 shape.addQuadCurve(to: .init(x: -5, y: 7), control: .init(x: -6, y: 0)); shape.closeSubpath()
52 let body = SKShapeNode(path: shape); body.fillColor = bases[style]; body.strokeColor = .clear; sock.addChild(body)
53 let crop = SKCropNode()
54 let mask = SKShapeNode(path: shape); mask.fillColor = .white; mask.strokeColor = .clear; crop.maskNode = mask; sock.addChild(crop)
55 func mark(_ path: CGPath, fill: NSColor = .clear, stroke: NSColor = .clear, width: CGFloat = 1) {
56 let node = SKShapeNode(path: path); node.fillColor = fill; node.strokeColor = stroke; node.lineWidth = width; crop.addChild(node)
57 }
58 switch style {
59 case 0: // Cream sailor stripes.
60 for y in stride(from: CGFloat(4), through: 20, by: 5) { mark(CGPath(rect: .init(x: -6, y: y, width: 26, height: 2), transform: nil), fill: cream) }
61 case 1: // Big enough to read as spots at desktop scale.
62 for y in stride(from: CGFloat(5), through: 20, by: 5) {
63 for x in stride(from: CGFloat(-3), through: 13, by: 5) {
64 mark(CGPath(ellipseIn: .init(x: x, y: y, width: 2.2, height: 2.2), transform: nil), fill: cream)
65 }
66 }
67 case 2: // Tiny argyle diamonds.
68 for y in stride(from: CGFloat(5), through: 20, by: 6) {
69 let diamond = CGMutablePath(); diamond.move(to: .init(x: 0.5, y: y + 3)); diamond.addLine(to: .init(x: 4, y: y)); diamond.addLine(to: .init(x: 0.5, y: y - 3)); diamond.addLine(to: .init(x: -3, y: y)); diamond.closeSubpath()
70 mark(diamond, fill: cream)
71 }
72 default: // Zigzag knit.
73 for y in stride(from: CGFloat(5), through: 20, by: 5) {
74 let zigzag = CGMutablePath(); zigzag.move(to: .init(x: -6, y: y))
75 for x in stride(from: -3, through: 18, by: 3) { zigzag.addLine(to: .init(x: CGFloat(x), y: y + (x % 2 == 0 ? 0 : 2))) }
76 mark(zigzag, stroke: cream, width: 1.5)
77 }
78 }
79 mark(CGPath(rect: .init(x: -5, y: 20, width: 11, height: 4), transform: nil), fill: cream)
80 mark(CGPath(ellipseIn: .init(x: 11, y: 0, width: 9, height: 9), transform: nil), fill: cream)
81 mark(CGPath(ellipseIn: .init(x: -8, y: 0, width: 9, height: 8), transform: nil), fill: cream)
82 for x in stride(from: CGFloat(-3), through: 4, by: 2) { mark(CGPath(rect: .init(x: x, y: 21, width: 0.5, height: 2), transform: nil), fill: bases[style].withAlphaComponent(0.5)) }
83 let outline = SKShapeNode(path: shape); outline.fillColor = .clear; outline.strokeColor = ink; outline.lineWidth = 0.9; sock.addChild(outline)
84 }
85​
86 func update(mission: SockMission, explorer: WorldPoint, target: WorldPoint?, facing: Double, viewport: CGRect, held: Bool) {
87 let stage = mission.stage
88 discovery.isHidden = target == nil || held
89 if let target { discovery.position = .init(x: target.x, y: target.y + 2) }
90 let revealed = stage == .revealing || stage == .updatingMap || stage == .resting
91 styleSock(mission.pattern.rawValue)
92 sock.zRotation = revealed ? sin(min(mission.elapsed, 0.4) / 0.4 * .pi) * 0.12 : 0
93 map.isHidden = stage != .updatingMap || held
94 map.position = .init(x: explorer.x + 8 * facing, y: explorer.y + 45)
95 map.zRotation = -0.2 * facing
96 let text: String?
97 switch stage {
98 case .spotting: text = "A sock!"
99 case .revealing: text = ["Stripes! Nice.", "Spots! Excellent.", "Argyle. Classy.", "Zigzags!"][sockStyle]
100 case .updatingMap: text = "Mark it on the map."
101 default: text = nil
102 }
103 speech.isHidden = text == nil || held
104 speech.text = text
105 speech.position = .init(x: min(max(explorer.x, viewport.minX + 58), viewport.maxX - 58), y: min(explorer.y + 96, viewport.maxY - 16))
106 }