From 96ccb0c4b739a953325614efdfcff57820d6d6f6 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sat, 14 Jun 2025 19:42:40 +1000 Subject: [PATCH] swift: Decouple lesson09 star behaviour --- src/swift/Lesson9/lesson9.swift | 49 +++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/swift/Lesson9/lesson9.swift b/src/swift/Lesson9/lesson9.swift index f77f90e..557184b 100644 --- a/src/swift/Lesson9/lesson9.swift +++ b/src/swift/Lesson9/lesson9.swift @@ -8,6 +8,35 @@ import SDLSwift import NeHe import simd +extension Lesson9.Star +{ + init(coeff: Float, random: inout NeHeRandom) + { + self.angle = 0.0 + self.distance = 5.0 * coeff + self.color = Self.nextColor(random: &random) + } + + mutating func update(coeff: Float, random: inout NeHeRandom) + { + self.angle += coeff + self.distance -= 0.01 + if self.distance < 0.0 + { + self.distance += 5.0 + self.color = Self.nextColor(random: &random) + } + } + + private static func nextColor(random: inout NeHeRandom) -> SIMD3 + { + .init( + UInt8(truncatingIfNeeded: random.next() % 256), + UInt8(truncatingIfNeeded: random.next() % 256), + UInt8(truncatingIfNeeded: random.next() % 256)) + } +} + struct Lesson9: AppDelegate { struct Vertex @@ -179,15 +208,7 @@ struct Lesson9: AppDelegate self.instanceXferBuffer = instanceXferBuffer // Initialise stars - self.stars = (0..