-
Notifications
You must be signed in to change notification settings - Fork 5
/
battle_mine.go
84 lines (70 loc) · 1.68 KB
/
battle_mine.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
package main
import (
"github.com/quasilyte/ge"
"github.com/quasilyte/ge/physics"
"github.com/quasilyte/gmath"
)
type battleMine struct {
scene *ge.Scene
sprite *ge.Sprite
body physics.Body
alliance int
lifespan float64
}
func newBattleMine(pos gmath.Vec, alliance int, lifespan float64) *battleMine {
b := &battleMine{alliance: alliance, lifespan: lifespan}
b.body.Pos = pos
return b
}
func (b *battleMine) Init(scene *ge.Scene) {
b.scene = scene
b.sprite = scene.NewSprite(ImageMine)
b.sprite.Pos.Base = &b.body.Pos
b.sprite.SetHue(spriteHue(b.alliance))
scene.AddGraphicsBelow(b.sprite, 1)
b.body.InitStaticCircle(b, 8)
b.body.LayerMask = projectileLayerMask(b.alliance) | mineLayer
scene.AddBody(&b.body)
// Mines can't be stacked.
collided := false
for _, collision := range scene.GetCollisionsAtLayer(&b.body, gmath.Vec{}, mineLayer) {
m, ok := collision.Body.Object.(*battleMine)
if ok {
m.Destroy()
collided = true
}
}
if collided {
scene.Audio().PlaySound(mineLayerDesign.extra.hitSound)
b.Destroy()
}
}
func (b *battleMine) IsDisposed() bool {
return b.sprite.IsDisposed()
}
func (b *battleMine) Destroy() {
e := newExplosion(b.body.Pos)
b.scene.AddObject(e)
b.Dispose()
}
func (b *battleMine) Dispose() {
b.sprite.Dispose()
b.body.Dispose()
}
func (b *battleMine) Update(delta float64) {
for _, collision := range b.scene.GetCollisionsAtLayer(&b.body, gmath.Vec{}, mineLayer) {
p, ok := collision.Body.Object.(*projectile)
if ok && p.config.alliance != b.alliance {
p.Destroy()
b.Destroy()
return
}
}
if b.lifespan < 0 {
return
}
b.lifespan = gmath.ClampMin(b.lifespan-delta, 0)
if b.lifespan == 0 {
b.Dispose()
}
}