-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.go
274 lines (211 loc) · 6.55 KB
/
bullet.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
package main
import (
"image"
"math"
"math/rand"
"github.com/hajimehoshi/ebiten"
)
// BulletExplosion is the animation that plays when a bullet hits something
type BulletExplosion struct {
position Vec2f
size Vec2i
animation Animation
animationSpeed float64
animationStarted bool
animationFinished bool
lightID int // The ID of the light that follows the explosion
sprite *Sprite
image *ebiten.Image
}
func createBulletExplosion(position Vec2f, image *ebiten.Image, lightID int) BulletExplosion {
spritesheet := createSpritesheet(
newVec2i(0, 60),
newVec2i(112, 76),
7,
iitemsSpritesheet,
)
size := spritesheet.sprites[0].size
sprite := createSprite(newVec2i(0, 60), newVec2i(112, 76), size, image)
position.x -= float64(size.x) / 2
position.y -= float64(size.y) / 2
animation := createAnimation(
spritesheet,
image,
)
return BulletExplosion{
position: position,
size: size,
animation: animation,
animationSpeed: 6.,
animationStarted: false,
animationFinished: false,
lightID: lightID,
sprite: &sprite,
image: image,
}
}
func (b *BulletExplosion) update(game *Game) {
game.lightHandler.lights[game.lightHandler.getLightIndex(b.lightID)].
update(
newVec2f(b.position.x+float64(b.size.x/2), b.position.y+float64(b.size.y/2)),
0,
)
if !b.animationStarted {
b.animation.startForwards()
b.animationStarted = true
}
if b.animation.currentFrame == b.animation.spritesheet.numberOfSprites-1 {
b.animationFinished = true
}
b.animation.update(b.animationSpeed)
}
func (b *BulletExplosion) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(b.position.x, b.position.y)
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
currentFrame := b.animation.spritesheet.sprites[b.animation.currentFrame]
subImageRect := image.Rect(
currentFrame.startPosition.x,
currentFrame.startPosition.y,
currentFrame.endPosition.x,
currentFrame.endPosition.y,
)
screen.DrawImage(b.image.SubImage(subImageRect).(*ebiten.Image), op)
}
func updateBulletExplosions(game *Game) {
for i := 0; i < len(game.bulletExplosions); i++ {
if i-1 > len(game.bulletExplosions) {
break
}
game.bulletExplosions[i].update(game)
// Should we delete the explosion?
if game.bulletExplosions[i].animationFinished && game.bulletExplosions[i].animation.currentFrame == 0 {
game.lightHandler.lights = removeLight(game.lightHandler.lights, // Destroy light
game.bulletExplosions[i].lightID)
game.bulletExplosions = removeBulletExplosion(game.bulletExplosions, i) // Destroy explosion
}
}
}
func renderBulletExplosions(game *Game, screen *ebiten.Image) {
for i := 0; i < len(game.bulletExplosions); i++ {
game.bulletExplosions[i].render(screen)
}
}
func removeBulletExplosion(slice []BulletExplosion, e int) []BulletExplosion {
return append(slice[:e], slice[e+1:]...)
}
// BulletGlow is the glow around the laser bullets, just an image
type BulletGlow struct {
position Vec2f
size Vec2i
rotation float64
sprite *Sprite
image *ebiten.Image
}
func (b *BulletGlow) render(screen *ebiten.Image, glowSprite *Sprite) {
op := &ebiten.DrawImageOptions{}
// Translate center of image to 0, 0 before rotating
op.GeoM.Translate(0-float64(b.size.x)/2, 0-float64(b.size.y)/2)
op.GeoM.Rotate(b.rotation)
op.GeoM.Translate(b.position.x, b.position.y)
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
subImageRect := image.Rect(
b.sprite.startPosition.x,
b.sprite.startPosition.y,
b.sprite.endPosition.x,
b.sprite.endPosition.y,
)
screen.DrawImage(b.image.SubImage(subImageRect).(*ebiten.Image), op)
}
func (b *BulletGlow) update(position Vec2f, rotation float64) {
b.size = newVec2i(b.sprite.size.x, b.sprite.size.y) // Useless for now
b.position = position
b.rotation = rotation
}
// Bullet is for the player's gun to fire
type Bullet struct {
position Vec2f
size Vec2i
velocity Vec2f
rotation float64
storedAngle float64
speed float64
distanceTravelled float64 // How far has the bullet travelled?
collisionRect image.Rectangle
destroy bool
glow BulletGlow
glowSprite *Sprite
lightID int // The ID of the light that follows the bullet
sprite Sprite
image *ebiten.Image
}
func createBullet(position Vec2f, rotation float64, speed float64, lightID int) Bullet {
// Accuracy!
up := rand.Float64()
down := rand.Float64()
accuracyRate := float64(gameReference.player.accuracy) / 100.
rotation += (up - down) * (1. - accuracyRate)
velocity := newVec2f(speed*math.Cos(rotation), speed*math.Sin(rotation))
glowSprite := createSprite(newVec2i(27, 46), newVec2i(34, 53), newVec2i(7, 7), iitemsSpritesheet)
size := newVec2i(5, 5)
return Bullet{
position: position,
rotation: rotation,
sprite: createSprite(newVec2i(22, 47), newVec2i(27, 52), size, iitemsSpritesheet),
size: size,
image: iitemsSpritesheet,
velocity: velocity,
speed: speed,
glow: BulletGlow{image: iitemsSpritesheet, sprite: &glowSprite},
glowSprite: &glowSprite,
lightID: lightID,
}
}
func (b *Bullet) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
// Translate center of image to 0, 0 before rotating
op.GeoM.Translate(0-float64(b.sprite.size.x)/2, 0-float64(b.sprite.size.y)/2)
op.GeoM.Rotate(b.rotation)
op.GeoM.Translate(b.position.x, b.position.y)
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
subImageRect := image.Rect(
b.sprite.startPosition.x,
b.sprite.startPosition.y,
b.sprite.endPosition.x,
b.sprite.endPosition.y,
)
screen.DrawImage(b.image.SubImage(subImageRect).(*ebiten.Image), op)
// Draw glow
b.glow.render(screen, b.glowSprite)
}
func (b *Bullet) update(game *Game) {
b.borderCollision()
game.lightHandler.lights[game.lightHandler.getLightIndex(b.lightID)].
update(
newVec2f(b.position.x+float64(b.size.x/2), b.position.y+float64(b.size.y/2)),
b.rotation,
)
b.position.x += b.velocity.x
b.position.y += b.velocity.y
// Move glow
b.glow.update(b.position, b.rotation)
b.collisionRect = image.Rect(
int(b.position.x),
int(b.position.y),
int(b.position.x)+b.size.x,
int(b.position.y)+b.size.y,
)
b.distanceTravelled++
if b.distanceTravelled >= gameReference.player.gunRange {
b.destroy = true
}
}
// Checks if bullets collide with border, deletes if so
func (b *Bullet) borderCollision() {
if b.position.x <= 17+float64(b.size.x) ||
b.position.y <= 14 ||
b.position.x+float64(b.size.x) >= screenWidth-17 ||
b.position.y+float64(b.size.y) >= screenHeight-17 {
b.destroy = true
}
}