-
Notifications
You must be signed in to change notification settings - Fork 0
/
gib.go
161 lines (133 loc) · 3.5 KB
/
gib.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
package main
import (
"image"
"math/rand"
"github.com/hajimehoshi/ebiten"
)
/* TODO: REMOVE GIB HANDLERS AFTER THEY FINISH SPAWNING GIBS
*/
// Gib is random piece of a subimage that has a bloodEmitter
type Gib struct {
position Vec2f
size Vec2i
velocity Vec2i
bloodEmitter BloodEmitter
distanceAllowed int
canMove bool
rotation float64
subImage image.Rectangle
image *ebiten.Image
}
func (g *Gib) update(game *Game) {
notInWall := true
if g.canMove {
if g.distanceAllowed >= 0 {
if g.position.x <= 5+float64(g.size.x) ||
g.position.y <= 27 ||
g.position.x+float64(g.size.x) >= screenWidth-5 ||
g.position.y+float64(g.size.y) >= screenHeight-5 {
notInWall = false
g.canMove = false
}
if notInWall {
g.position.x += float64(g.velocity.x)
g.position.y += float64(g.velocity.y)
g.distanceAllowed--
}
} else {
g.canMove = false
}
}
}
func (g *Gib) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(0-float64(g.size.x)/2, 0-float64(g.size.y)/2)
op.GeoM.Rotate(g.rotation)
op.GeoM.Translate(g.position.x, g.position.y)
screen.DrawImage(g.image.SubImage(g.subImage).(*ebiten.Image), op) // Draw gib
}
// GibHandler controls gibs, it's like a particle mitter but for a set of gibs
type GibHandler struct {
gibs []Gib
}
func createGibHandler() GibHandler {
var gibs []Gib
return GibHandler{
gibs,
}
}
func createGib(position Vec2f,
rotation float64,
distanceAllowed int,
randomVelocity Vec2i,
subImage image.Rectangle,
image *ebiten.Image) Gib {
bloodSpawnTimer := 10.
size := newVec2i((subImage.Max.X-subImage.Min.X)+8, (subImage.Max.Y-subImage.Min.Y)+8)
return Gib{
position: position,
size: size,
velocity: randomVelocity,
bloodEmitter: createBloodEmitter(position, bloodSpawnTimer, size, ibloodSpritesheet),
distanceAllowed: distanceAllowed,
canMove: true,
rotation: rotation,
subImage: subImage,
image: image,
}
}
func (g *GibHandler) update(game *Game) {
for i := 0; i < len(g.gibs); i++ {
g.gibs[i].update(game)
if g.gibs[i].canMove {
g.gibs[i].bloodEmitter.update(g.gibs[i].position)
}
}
}
func (g *GibHandler) render(screen *ebiten.Image) {
for i := 0; i < len(g.gibs); i++ {
g.gibs[i].render(screen)
g.gibs[i].bloodEmitter.render(screen)
}
}
func (g *GibHandler) explode(numberOfGibs int,
gibSize int,
originPosition Vec2f,
subImage image.Rectangle,
gibImage *ebiten.Image) {
for i := 0; i < numberOfGibs; i++ {
randomDistanceAllowed := 3 + rand.Intn(10)
randomRotation := float64(rand.Intn(15))
randomVelocity := newVec2i(-3+rand.Intn(6), -3+rand.Intn(6)) // Random velocity
// Get the subimage size and position for random gibs
subImageSize := newVec2i(subImage.Max.X-subImage.Min.X, subImage.Max.Y-subImage.Min.Y)
subImagePosition := newVec2i(
subImage.Min.X+rand.Intn(subImageSize.x-gibSize),
subImage.Min.Y+rand.Intn(subImageSize.y-gibSize),
)
newSubImage := image.Rect(
subImagePosition.x,
subImagePosition.y,
subImagePosition.x+gibSize,
subImagePosition.y+gibSize,
)
g.gibs = append(g.gibs, createGib(
originPosition,
randomRotation,
randomDistanceAllowed,
randomVelocity,
newSubImage,
gibImage,
))
}
}
func updateGibHandlers(g *Game) {
for i := 0; i < len(g.gibHandlers); i++ {
g.gibHandlers[i].update(g)
}
}
func renderGibHandlers(g *Game, screen *ebiten.Image) {
for i := 0; i < len(g.gibHandlers); i++ {
g.gibHandlers[i].render(screen)
}
}