-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStars.swift
61 lines (51 loc) · 1.54 KB
/
Stars.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Stars.swift
//
//
// Created by Ben Myers on 1/21/24.
//
import SwiftUI
import Particles
import Foundation
public extension Preset {
struct Stars: Entity, PresetEntry {
private var parameters: Parameters
public init(size: CGFloat = 30.0, lifetime: TimeInterval = 5.0, intensity: Int = 20, twinkle: Bool = true) {
self.parameters = .init(intensity: intensity, starSize: size, starLifetime: lifetime, twinkle: twinkle)
}
public var body: some Entity {
Emitter(every: 1.0 / Double(parameters.intensity)) {
Star(parameters: parameters)
}
.emitAll()
}
public struct Star: Entity {
internal var parameters: Stars.Parameters
public var body: some Entity {
Particle {
Image("sparkle", bundle: .module)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: parameters.starSize, height: parameters.starSize)
}
.initialPosition { c in
let x = Int.random(in: 0 ... Int(c.system.size.width))
let y = Int.random(in: 0 ... Int(c.system.size.height))
return CGPoint(x: x, y: y)
}
.transition(.opacity, duration: 3.0)
.opacity { c in
return 0.5 + sin(c.timeAlive)
}
.scale(factorIn: 0.5 ... 1.0)
.blendMode(.plusLighter)
}
}
internal struct Parameters {
var intensity: Int = 3
var starSize: CGFloat
var starLifetime: TimeInterval
var twinkle: Bool = false
}
}
}