-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.go
153 lines (126 loc) · 2.37 KB
/
bird.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
package main
import (
"fmt"
"sync"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/mix"
"github.com/veandco/go-sdl2/sdl"
)
const (
gravity = 0.25
jumpSpeed = 5
)
type bird struct {
mu sync.RWMutex
time int
textures []*sdl.Texture
r *sdl.Renderer
chunk *mix.Chunk
x, y int32
w, h int32
speed float64
dead bool
}
func newBird(r *sdl.Renderer) (*bird, error) {
var textures []*sdl.Texture
var err error
chunk, err := mix.LoadWAV("resources/audio/boing.wav")
if err != nil {
return nil, err
}
for i := 1; i <= 4; i++ {
var texture *sdl.Texture
path := fmt.Sprintf("resources/imgs/bird_frame_%d.png", i)
sdl.Do(func() {
texture, err = img.LoadTexture(r, path)
})
if err != nil {
return nil, err
}
textures = append(textures, texture)
}
return &bird{
textures: textures,
chunk: chunk,
x: 10,
y: int32(windowH / 2),
w: int32(float64(windowH) * .07),
h: int32(float64(windowH) * .07),
r: r,
}, nil
}
func (b *bird) update() {
b.mu.Lock()
defer b.mu.Unlock()
b.time++
b.y -= int32(b.speed)
if b.y < 0 || b.y > 600+b.h {
b.dead = true
}
/*if b.y > 0 {
b.y -= int32(b.speed)
b.speed += gravity
}*/
b.speed += gravity
}
func (b *bird) paint() error {
b.mu.RLock()
defer b.mu.RUnlock()
var err error
rect := &sdl.Rect{X: b.x, Y: 600 - b.y - b.h/2, W: b.w, H: b.h}
i := b.time / 10 % len(b.textures)
sdl.Do(func() {
err = b.r.Copy(b.textures[i], nil, rect)
})
if err != nil {
return fmt.Errorf("could not copy bird: %v", err)
}
return nil
}
func (b *bird) restart() {
b.mu.RLock()
defer b.mu.RUnlock()
b.y = 300
b.speed = 0
b.dead = false
}
func (b *bird) destroy() {
b.mu.Lock()
defer b.mu.Unlock()
b.chunk.Free()
for _, t := range b.textures {
sdl.Do(func() {
t.Destroy()
})
}
}
func (b *bird) isDead() bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.dead
}
func (b *bird) jump() {
b.mu.Lock()
defer b.mu.Unlock()
if mix.Playing(-1) < 1 && !b.dead {
b.chunk.Play(-1, 1)
}
b.speed = -jumpSpeed
}
func (b *bird) touch(p *pipe) {
b.mu.Lock()
defer b.mu.Unlock()
if p.x > b.x+b.w { // too far left
return
}
if p.x+p.w < b.x { // too far right
return
}
if !p.inverted && p.h < b.y-b.h/2 { // pipe is too low
return
}
if p.inverted && (600-p.h) > b.y+b.h/2 { // inverted pipe is too high
return
}
b.dead = true
}