-
Notifications
You must be signed in to change notification settings - Fork 1
/
effects.go
555 lines (443 loc) · 15.5 KB
/
effects.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package game
import (
"fmt"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/harbdog/raycaster-go/geom"
"github.com/pixelmek-3d/pixelmek-3d/game/model"
"github.com/pixelmek-3d/pixelmek-3d/game/render"
renderFx "github.com/pixelmek-3d/pixelmek-3d/game/render/effects"
"github.com/pixelmek-3d/pixelmek-3d/game/resources/effects"
)
const (
ejectName string = "EJECT"
)
var (
crtShader *renderFx.CRT
ejectLauncher model.Weapon
ejectPod *render.ProjectileSprite
jumpJetEffect *render.EffectSprite
attachedJJEffects map[*render.Sprite]*render.EffectSprite
bloodEffects map[string]*render.EffectSprite
explosionEffects map[string]*render.EffectSprite
fireEffects map[string]*render.EffectSprite
smokeEffects map[string]*render.EffectSprite
)
func init() {
attachedJJEffects = make(map[*render.Sprite]*render.EffectSprite)
bloodEffects = make(map[string]*render.EffectSprite)
explosionEffects = make(map[string]*render.EffectSprite)
fireEffects = make(map[string]*render.EffectSprite)
smokeEffects = make(map[string]*render.EffectSprite)
}
func (g *Game) loadSpecialEffects() {
// load shader effect
_loadShaderEffects()
// load the ejection pod template
_loadEjectionPodResource(g)
// load the jump jet effect sprite template
_loadJumpJetEffectResource()
// load the blood effect sprite templates
_loadEffectSpritesFromResourceList(effects.Blood, bloodEffects)
// load the explosion effect sprite templates
_loadEffectSpritesFromResourceList(effects.Explosions, explosionEffects)
// load the fire effect sprite templates
_loadEffectSpritesFromResourceList(effects.Fires, fireEffects)
// load the smoke effect sprite templates
_loadEffectSpritesFromResourceList(effects.Smokes, smokeEffects)
}
func _getEffectImageFromResource(r *model.ModelEffectResource) *ebiten.Image {
effectRelPath := fmt.Sprintf("%s/%s", model.EffectsResourceType, r.Image)
return getSpriteFromFile(effectRelPath)
}
func _loadShaderEffects() {
crtShader = renderFx.NewCRT()
}
func _loadJumpJetEffectResource() {
if jumpJetEffect == nil {
jumpJetImg := _getEffectImageFromResource(effects.JumpJet)
jumpJetEffect = render.NewAnimatedEffect(effects.JumpJet, jumpJetImg, math.MaxInt)
}
for s := range attachedJJEffects {
delete(attachedJJEffects, s)
}
}
func _loadEjectionPodResource(g *Game) {
if ejectPod != nil {
return
}
// TODO: refactor to use same func as g.loadUnitWeapons
weaponResource := g.resources.GetMissileWeaponResource("_ejection_pod.yaml")
// need to use the projectile image size to find the unit collision conversion from pixels
pResource := weaponResource.Projectile
projectileRelPath := fmt.Sprintf("%s/%s", model.ProjectilesResourceType, pResource.Image)
projectileImg := getSpriteFromFile(projectileRelPath)
pColumns, pRows := 1, 1
if pResource.ImageSheet != nil {
pColumns = pResource.ImageSheet.Columns
pRows = pResource.ImageSheet.Rows
}
pWidth, pHeight := projectileImg.Bounds().Dx(), projectileImg.Bounds().Dy()
pWidth = pWidth / pColumns
pHeight = pHeight / pRows
pCollisionRadius, pCollisionHeight := convertOffsetFromPx(
pResource.CollisionPxRadius, pResource.CollisionPxHeight, pWidth, pHeight, pResource.Scale,
)
// create the pod as missile projectile model
var modelPod model.Projectile
ejectLauncher, modelPod = model.NewMissileWeapon(
weaponResource, pCollisionRadius, pCollisionHeight, &geom.Vector2{}, &geom.Vector2{}, nil,
)
// create the projectile and effect sprite templates
eResource := weaponResource.Projectile.ImpactEffect
effectRelPath := fmt.Sprintf("%s/%s", model.EffectsResourceType, eResource.Image)
effectImg := getSpriteFromFile(effectRelPath)
projectileImpactAudioFiles := make([]string, 1)
projectileImpactAudioFiles = append(projectileImpactAudioFiles, pResource.ImpactEffect.Audio)
projectileImpactAudioFiles = append(projectileImpactAudioFiles, pResource.ImpactEffect.RandAudio...)
eSpriteTemplate := render.NewAnimatedEffect(eResource, effectImg, 1)
ejectPod = render.NewAnimatedProjectile(
&modelPod, pResource.Scale, projectileImg, *eSpriteTemplate, projectileImpactAudioFiles,
)
}
func _loadEffectSpritesFromResourceList(
resourceMap map[string]*model.ModelEffectResource, spriteMap map[string]*render.EffectSprite,
) {
for key, fx := range resourceMap {
if _, ok := spriteMap[key]; ok {
continue
}
// load the blood effect sprite template
eSpriteTemplate := render.NewAnimatedEffect(fx, _getEffectImageFromResource(fx), 1)
spriteMap[key] = eSpriteTemplate
}
}
func (g *Game) spawnEjectionPod(s *render.Sprite) *render.ProjectileSprite {
podSprite := ejectPod.Clone()
podSprite.SetParent(s.Entity)
podSprite.Projectile.SetWeapon(ejectLauncher)
podSprite.SetPos(s.Pos().Copy())
podSprite.SetPosZ(s.PosZ() + s.CollisionHeight())
podSprite.SetPitch(geom.HalfPi)
if s == g.player.sprite {
podHeading := model.ClampAngle2Pi(g.player.cameraAngle)
podSprite.SetHeading(podHeading)
} else {
podSprite.SetHeading(s.Entity.Heading())
}
// let ejection pod accelerate from zero for effect
podSprite.SetVelocity(0)
podSprite.Projectile.SetAcceleration(podSprite.Projectile.MaxVelocity() / (2 * model.TICKS_PER_SECOND))
g.sprites.addProjectile(podSprite)
g.audio.PlayLocalWeaponFireAudio(ejectLauncher)
return podSprite
}
func (g *Game) spawnEjectionPodSmokeEffects(s *render.ProjectileSprite) (duration int) {
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
// TODO: reduce smoke scale and/or opacity?
// only spawn smoke every few frames
fxCounter := s.EffectCounter()
if fxCounter > 0 {
s.SetEffectCounter(fxCounter - 1)
return
}
xFx := x + randFloat(-r/2, r/2)
yFx := y + randFloat(-r/2, r/2)
zFx := z + randFloat(-h/2, h/2)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
fxDuration := smokeFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
s.SetEffectCounter(1 + model.Randish.Intn(3))
return
}
func (g *Game) spawnJumpJetEffect(s *render.Sprite) {
_, found := attachedJJEffects[s]
if found {
// do not spawn another effect
return
}
jumpFx := jumpJetEffect.Clone()
jumpFx.SetScale(s.Scale())
jumpFx.AttachedTo = s
jumpFx.AttachedDepth = 0.01
g.sprites.addEffect(jumpFx)
// illuminate source sprite unit jump jetting
s.SetIlluminationPeriod(5000, 0.35)
// keep track of effect so only one is attached and can be deleted later
attachedJJEffects[s] = jumpFx
}
func (g *Game) removeJumpJetEffect(s *render.Sprite) {
jumpFx, found := attachedJJEffects[s]
if found {
g.sprites.deleteEffect(jumpFx)
delete(attachedJJEffects, s)
}
}
func (g *Game) spawnGenericDestroyEffects(s *render.Sprite, spawnFires bool) (duration int) {
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
numFx := 7 // TODO: alter number of effects based on sprite dimensions
if !spawnFires {
numFx = int(math.Ceil(float64(numFx) * 1.25))
}
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r/2, r/2)
yFx := y + randFloat(-r/2, r/2)
zFx := z + randFloat(h/8, h)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
if spawnFires {
fireFx := g.randFireEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(fireFx)
fxDuration := fireFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if !spawnFires {
// when not spawning fires, no duration implied
duration = 0
}
}
return
}
func (g *Game) spawnPlayerDestroyEffects() (duration int) {
s := g.player.sprite
// limit effects to only spawn every few frames to reduce performance impact
fxCounter := s.EffectCounter()
if fxCounter > 0 {
s.SetEffectCounter(fxCounter - 1)
return
}
// TODO: player destruction effects based on player unit type/size
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
numFx := 2
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r, r)
yFx := y + randFloat(-r, r)
zFx := z + randFloat(h/4, h)
explosionFx := g.randExplosionEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(explosionFx)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if i == 0 {
// only play one audio track at a time
g.audio.PlayEffectAudio(g, explosionFx)
}
fxDuration := explosionFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
s.SetEffectCounter(1 + model.Randish.Intn(4))
return
}
func (g *Game) spawnMechDestroyEffects(s *render.MechSprite) (duration int) {
// limit effects to only spawn every few frames to reduce performance impact
fxCounter := s.EffectCounter()
if fxCounter > 0 {
s.SetEffectCounter(fxCounter - 1)
return
}
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
// use size of mech to determine number of effects to randomly spawn each time
m := s.Mech()
numFx := 1
if m.Class() >= model.MECH_HEAVY {
numFx = 2
}
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r, r)
yFx := y + randFloat(-r, r)
zFx := z + randFloat(h/4, h)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
explosionFx := g.randExplosionEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(explosionFx)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if i == 0 {
// only play one audio track at a time
g.audio.PlayEffectAudio(g, explosionFx)
}
fxDuration := explosionFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
s.SetEffectCounter(1 + model.Randish.Intn(2))
return
}
func (g *Game) spawnInfantryDestroyEffects(s *render.InfantrySprite) (duration int) {
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
numFx := 4 // TODO: alter number of effects based on sprite dimensions
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r, r)
yFx := y + randFloat(-r, r)
zFx := z + randFloat(0, h)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
bloodFx := g.randBloodEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(bloodFx)
fxDuration := bloodFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
return
}
func (g *Game) spawnVehicleDestroyEffects(s *render.VehicleSprite) (duration int) {
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
numFx := 5 // TODO: alter number of effects based on sprite dimensions
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r, r)
yFx := y + randFloat(-r, r)
zFx := z + randFloat(0, h)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
explosionFx := g.randExplosionEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(explosionFx)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if i == 0 || i == numFx/2 {
// only play two audio tracks for now since they are played at once
g.audio.PlayEffectAudio(g, explosionFx)
}
fxDuration := explosionFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
return
}
func (g *Game) spawnVTOLDestroyEffects(s *render.VTOLSprite, spawnExplosions bool) (duration int) {
// only spawn smoke every few frames
fxCounter := s.EffectCounter()
numFx := 1 // TODO: alter number of effects based on sprite dimensions
if spawnExplosions {
numFx = 5
}
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r/2, r/2)
yFx := y + randFloat(-r/2, r/2)
zFx := z + randFloat(-h/2, h/2)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
if spawnExplosions {
explosionFx := g.randExplosionEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(explosionFx)
if i == 0 || i == numFx/2 {
// only play two audio tracks for now since they are played at once
g.audio.PlayEffectAudio(g, explosionFx)
}
fxDuration := explosionFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
if fxCounter == 0 {
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if !spawnExplosions {
fxDuration := smokeFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
}
}
if fxCounter > 0 {
s.SetEffectCounter(fxCounter - 1)
} else {
s.SetEffectCounter(1 + model.Randish.Intn(3))
}
return
}
func (g *Game) spawnEmplacementDestroyEffects(s *render.EmplacementSprite) (duration int) {
x, y, z := s.Pos().X, s.Pos().Y, s.PosZ()
r, h := s.CollisionRadius(), s.CollisionHeight()
numFx := 5 // TODO: alter number of effects based on sprite dimensions
for i := 0; i < numFx; i++ {
xFx := x + randFloat(-r/2, r/2)
yFx := y + randFloat(-r/2, r/2)
zFx := z + randFloat(h/12, h)
// only spawn effects in front of sprite relative to camera position
xFx, yFx = g.clampToCameraSpriteView(xFx, yFx, x, y)
explosionFx := g.randExplosionEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(explosionFx)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
if i == 0 || i == numFx/2 {
// only play two audio tracks for now since they are played at once
g.audio.PlayEffectAudio(g, explosionFx)
}
fxDuration := explosionFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
numFireFx := 5
for i := 0; i < numFireFx; i++ {
// for emplacements, also add some random fires
xFx := x + randFloat(-r/2, r/2)
yFx := y + randFloat(-r/2, r/2)
zFx := z + randFloat(h/8, h)
fireFx := g.randFireEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(fireFx)
smokeFx := g.randSmokeEffect(xFx, yFx, zFx, s.Heading(), 0)
g.sprites.addEffect(smokeFx)
fxDuration := fireFx.AnimationDuration()
if fxDuration > duration {
duration = fxDuration
}
}
return
}
func (g *Game) randBloodEffect(x, y, z, angle, pitch float64) *render.EffectSprite {
// return random blood effect
randKey := effects.RandBloodKey()
e := bloodEffects[randKey].Clone()
e.SetPos(&geom.Vector2{X: x, Y: y})
e.SetPosZ(z)
return e
}
func (g *Game) randExplosionEffect(x, y, z, angle, pitch float64) *render.EffectSprite {
// return random explosion effect
randKey := effects.RandExplosionKey()
e := explosionEffects[randKey].Clone()
e.SetPos(&geom.Vector2{X: x, Y: y})
e.SetPosZ(z)
// TODO: give small negative Z velocity so it falls with the unit being destroyed?
return e
}
func (g *Game) randFireEffect(x, y, z, angle, pitch float64) *render.EffectSprite {
// return random fire effect
randKey := effects.RandFireKey()
e := fireEffects[randKey].Clone()
e.SetPos(&geom.Vector2{X: x, Y: y})
e.SetPosZ(z)
return e
}
func (g *Game) randSmokeEffect(x, y, z, angle, pitch float64) *render.EffectSprite {
// return random smoke effect
randKey := effects.RandSmokeKey()
e := smokeEffects[randKey].Clone()
e.SetPos(&geom.Vector2{X: x, Y: y})
e.SetPosZ(z)
// give Z velocity so it rises
e.SetVelocityZ(0.003) // TODO: define velocity of rise in resource model
// TODO: give some small angle/pitch so it won't rise perfectly vertically (fake wind)
return e
}