-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.go
62 lines (49 loc) · 1.17 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
package main
import (
"math"
"time"
"github.com/hajimehoshi/ebiten"
)
type bullet struct {
x, y float64
angle float64
deleted bool
}
var bullets map[string]*bullet
var bulletSpeed float64 = 10.0
func initBullets() {
bullets = make(map[string]*bullet)
}
func NewBullet(x, y float64, angle float64) {
b := bullet{x: x, y: y, angle: angle}
bullets[time.Now().String()] = &b
// return &b
}
func (b *bullet) Update() {
b.x = b.x + math.Cos(b.angle)*float64(bulletSpeed)
b.y = b.y + math.Sin(b.angle)*float64(bulletSpeed)
if b.x < 0 || b.x > screenWidth {
b.deleted = true
}
if b.y < 0 || b.y > screenHeight {
b.deleted = true
}
}
func (b *bullet) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(200.0/255.0, 200.0/255.0, 200.0/255.0, 1)
op.GeoM.Reset()
op.GeoM.Translate(float64(b.x), float64(b.y))
screen.DrawImage(bulletSprite, op)
}
func CleanBullets() {
for i, b := range bullets {
if b.deleted == true {
delete(bullets, i)
}
}
}
func (b *bullet) X() float64 { return b.x }
func (b *bullet) Y() float64 { return b.y }
func (b *bullet) SY() float64 { return b.x + 2 }
func (b *bullet) SX() float64 { return b.y + 2 }