-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSmoke.swift
95 lines (86 loc) · 2.45 KB
/
Smoke.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Smoke.swift
//
//
// Created by Demirhan Mehmet Atabey on 22.03.2024.
//
import SwiftUI
import Particles
import Foundation
public extension Preset {
struct Smoke: Entity, PresetEntry {
var color: Color
var startRadius: CGFloat = 8.0
var endRadius: CGFloat = 30.0
var spawnPoint: UnitPoint
var spawnRadius: CGSize
var dirty: Bool = false
private var velocityX: ClosedRange<CGFloat> {
switch spawnPoint {
case .bottomLeading, .leading, .topLeading:
return -6.0 ... -3.0
case .topTrailing, .bottomTrailing, .trailing:
return 3.0 ... 6.0
default:
return -0.5 ... 0.5
}
}
private var velocityY: ClosedRange<CGFloat> {
switch spawnPoint {
case .bottomLeading, .bottomTrailing, .bottom:
return 1.0 ... 3.0
case .leading, .trailing:
return 0.0 ... 0.0
default:
return -3.0 ... 1.0
}
}
private var velocityAccelerationY: CGFloat {
switch spawnPoint {
case .bottom, .bottomLeading, .bottomTrailing:
return 0.02
case .leading, .trailing:
return 0
default:
return -0.02
}
}
public var body: some Entity {
Emitter(every: 0.01) {
Particle {
RadialGradient(
colors: [color, .clear],
center: .center,
startRadius: startRadius,
endRadius: endRadius
)
.clipShape(Circle())
}
.initialPosition(.center)
.initialOffset(xIn: -spawnRadius.width ... spawnRadius.width/2, yIn: -spawnRadius.height/2 ... spawnRadius.height/2)
.initialVelocity(xIn: velocityX, yIn: velocityY)
.fixAcceleration(y: velocityAccelerationY)
.lifetime(in: 3 +/- 0.2)
.blendMode(dirty ? .hardLight : .normal)
.transition(.scale, on: .death, duration: 0.5)
.transition(.opacity, on: .birth)
.opacity(0.3)
}
}
public init(
color: Color = Color(red: 128/255, green: 128/255, blue: 128/255, opacity: 1),
dirty: Bool = false,
spawnPoint: UnitPoint = .center,
startRadius: CGFloat = 12.0,
endRadius: CGFloat = 35.0,
spawnRadius: CGSize = .init(width: 40.0, height: 8.0)
) {
self.dirty = dirty
self.color = color
self.spawnPoint = spawnPoint
self.startRadius = startRadius
self.endRadius = endRadius
self.spawnRadius = spawnRadius
}
}
}