-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.go
125 lines (109 loc) · 2.63 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
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
/*
Bullet is the ammo a player uses
*/
type Bullet struct {
assetManager *AssetManager
window *pixelgl.Window
sprite *pixel.Sprite
pos pixel.Vec
width float64
height float64
topEdge float64
dead bool
}
/*
NewBullet initializes a new bullet. It is setup to start at the player's position
*/
func NewBullet(window *pixelgl.Window, assetManager *AssetManager, playerPos pixel.Vec, playerHeight float64) *Bullet {
sprite := assetManager.GetBulletSprite()
return &Bullet{
assetManager: assetManager,
window: window,
sprite: sprite,
pos: pixel.V(playerPos.X, playerPos.Y+(playerHeight/2)),
width: sprite.Frame().W(),
height: sprite.Frame().H(),
topEdge: window.Bounds().H() + (sprite.Frame().H() / 2),
dead: true,
}
}
/*
Draw renders this bullet onto the window
*/
func (b *Bullet) Draw() {
if !b.dead {
b.sprite.Draw(b.assetManager.Batch, pixel.IM.Moved(b.pos))
// b.sprite.Draw(b.window, pixel.IM.Moved(b.pos))
}
}
/*
HitInvader return true, and the row/col of the invader hit when there is
a collision
*/
func (b *Bullet) HitInvader(invaders *Invaders) (bool, int, int) {
ivs := invaders.GetInvaders()
for row := 0; row < MAX_ROWS; row++ {
for col := 0; col < MAX_COLS; col++ {
rect := ivs[row][col].GetRect()
bulletLeft := b.pos.X - (b.width / 2)
bulletRight := b.pos.X + (b.width / 2)
bulletTop := b.pos.Y + (b.height / 2)
bulletBottom := b.pos.Y - (b.height / 2)
if ivs[row][col].IsAlive() && bulletRight >= rect.Min.X && bulletLeft <= rect.Max.X && bulletTop >= rect.Min.Y && bulletBottom <= rect.Max.Y {
return true, row, col
}
}
}
return false, 0, 0
}
/*
IsAlive returns true if the bullet is alive
*/
func (b *Bullet) IsAlive() bool {
return !b.dead
}
/*
IsDead returns true if the bullet is dead
*/
func (b *Bullet) IsDead() bool {
return b.dead
}
/*
IsTopEdge returns true if the bullet has gone off the top of the window
*/
func (b *Bullet) IsTopEdge() bool {
return b.pos.Y >= b.topEdge
}
/*
Kill marks the bullet as dead
*/
func (b *Bullet) Kill() {
b.dead = true
}
/*
Move advances the bullet toward the top
*/
func (b *Bullet) Move(dt float64) {
if !b.dead {
move := 200.0 * dt
y := b.pos.Y + move
b.pos.Y = pixel.Clamp(y, 0, b.topEdge)
}
}
/*
Reset positions the bullet to the player
*/
func (b *Bullet) Reset(playerPos pixel.Vec, playerHeight float64) {
b.pos = pixel.V(playerPos.X, playerPos.Y+(playerHeight/2))
}
/*
Resurrect sets the bullet as not dead
*/
func (b *Bullet) Resurrect() {
b.dead = false
}