-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparticle_emitter.go
248 lines (199 loc) · 5.24 KB
/
particle_emitter.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package ge
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
resource "github.com/quasilyte/ebitengine-resource"
"github.com/quasilyte/gmath"
)
type disposeState uint8
const (
disposeUnset disposeState = iota
disposeNow
disposeDetached
)
type Particle struct {
Velocity gmath.Vec
Rotation gmath.Rad
Lifetime float64
}
type ParticleConfig struct {
Lifetime float64
Amount int
InitFunc func(p *Particle)
RotationFunc func(p *Particle, delta float64) gmath.Rad
VelocityFunc func(p *Particle, delta float64) gmath.Vec
ColorFunc func(p *Particle) ColorScale
}
type ParticleEmitter struct {
Visible bool
Centered bool
Pos Pos
FrameOffset gmath.Vec
FrameWidth float64
FrameHeight float64
Hue gmath.Rad
image *ebiten.Image
disposed disposeState
emitReload float64
emitCooldown float64
particleLifetime float64
particleIndex gmath.Slider
tmpParticle Particle
initFunc func(p *Particle)
rotationFunc func(p *Particle, delta float64) gmath.Rad
velocityFunc func(p *Particle, delta float64) gmath.Vec
colorFunc func(p *Particle) ColorScale
particles []particle
}
func NewParticleEmitter() *ParticleEmitter {
return &ParticleEmitter{
Visible: true,
Centered: true,
}
}
func (e *ParticleEmitter) Init(scene *Scene) {}
func (e *ParticleEmitter) SetImage(img resource.Image) {
w, h := img.Data.Size()
e.image = img.Data
e.FrameWidth = img.DefaultFrameWidth
if e.FrameWidth == 0 {
e.FrameWidth = float64(w)
}
e.FrameHeight = img.DefaultFrameHeight
if e.FrameHeight == 0 {
e.FrameHeight = float64(h)
}
}
func (e *ParticleEmitter) SetConfig(config ParticleConfig) {
e.particleLifetime = config.Lifetime
if len(e.particles) < config.Amount {
e.particles = make([]particle, config.Amount)
}
e.particleIndex.SetBounds(0, config.Amount-1)
e.emitReload = config.Lifetime / float64(config.Amount)
e.emitCooldown = 0
e.initFunc = config.InitFunc
if e.initFunc == nil {
e.initFunc = func(p *Particle) { *p = Particle{} }
}
e.rotationFunc = config.RotationFunc
e.velocityFunc = config.VelocityFunc
e.colorFunc = config.ColorFunc
}
func (e *ParticleEmitter) IsDisposed() bool {
return e.disposed == disposeNow
}
func (e *ParticleEmitter) Dispose() {
e.disposed = disposeNow
}
func (e *ParticleEmitter) DisposeDetached() {
e.disposed = disposeDetached
}
func (e *ParticleEmitter) Update(delta float64) {
if len(e.particles) == 0 {
return
}
e.emitCooldown = gmath.ClampMin(e.emitCooldown-delta, 0)
if e.emitCooldown == 0 {
// Create new particles only if this object is not being disposed.
if e.disposed == disposeUnset {
e.emitCooldown = e.emitReload
p := &e.particles[e.particleIndex.Value()]
e.particleReset(p)
e.particleIndex.Inc()
}
}
hasActiveParticles := false
for i := range e.particles {
p := &e.particles[i]
if p.hp == 0 {
continue
}
hasActiveParticles = true
e.particleTick(delta, p)
}
// If all particles are gone and emitter is being disposed in detached mode,
// it's time for it to be deleted.
if !hasActiveParticles && e.disposed == disposeDetached {
e.Dispose()
}
}
func (e *ParticleEmitter) setTmpParticle(p *particle) {
e.tmpParticle.Velocity = p.velocity
e.tmpParticle.Rotation = p.rotation
e.tmpParticle.Lifetime = p.hp
}
func (e *ParticleEmitter) particleTick(delta float64, p *particle) {
e.setTmpParticle(p)
if e.velocityFunc != nil {
p.velocity = e.velocityFunc(&e.tmpParticle, delta)
}
if e.rotationFunc != nil {
p.rotation = e.rotationFunc(&e.tmpParticle, delta)
}
p.hp = gmath.ClampMin(p.hp-delta, 0)
p.offset = p.offset.Add(p.velocity.Mulf(delta))
}
func (e *ParticleEmitter) particleReset(p *particle) {
p.offset = gmath.Vec{}
p.hp = e.particleLifetime
e.initFunc(&e.tmpParticle)
p.rotation = e.tmpParticle.Rotation
p.velocity = e.tmpParticle.Velocity
}
func (e *ParticleEmitter) Draw(screen *ebiten.Image) {
if !e.Visible || len(e.particles) == 0 {
return
}
var origin gmath.Vec
if e.Centered {
origin = gmath.Vec{X: e.FrameWidth / 2, Y: e.FrameHeight / 2}
}
origin = origin.Sub(e.Pos.Offset)
subImage := e.image.SubImage(image.Rectangle{
Min: image.Point{
X: int(e.FrameOffset.X),
Y: int(e.FrameOffset.Y),
},
Max: image.Point{
X: int(e.FrameOffset.X + e.FrameWidth),
Y: int(e.FrameOffset.Y + e.FrameHeight),
},
}).(*ebiten.Image)
var posX float64
var posY float64
if e.Pos.Base != nil {
posX = e.Pos.Base.X - origin.X
posY = e.Pos.Base.Y - origin.Y
} else {
posX = 0 - origin.X
posY = 0 - origin.Y
}
for i := range e.particles {
p := &e.particles[i]
if p.hp == 0 {
continue
}
e.setTmpParticle(p)
var drawOptions ebiten.DrawImageOptions
drawOptions.GeoM.Translate(-origin.X, -origin.Y)
drawOptions.GeoM.Rotate(float64(p.rotation))
drawOptions.GeoM.Translate(origin.X, origin.Y)
drawOptions.GeoM.Translate(posX, posY)
if e.colorFunc != nil {
colorScale := e.colorFunc(&e.tmpParticle)
drawOptions.ColorM.Scale(float64(colorScale.R), float64(colorScale.G), float64(colorScale.B), float64(colorScale.A))
}
if e.Hue != 0 {
drawOptions.ColorM.RotateHue(float64(e.Hue))
}
drawOptions.GeoM.Translate(p.offset.X, p.offset.Y)
screen.DrawImage(subImage, &drawOptions)
}
}
type particle struct {
hp float64
rotation gmath.Rad
velocity gmath.Vec
offset gmath.Vec
}