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..