-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.go
189 lines (145 loc) · 3.24 KB
/
score.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
package main
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
"image/color"
"time"
)
type score struct {
score float64
multiplier int
increment int
multiplierPos pixel.Vec
scorePos pixel.Vec
startTime time.Time
timeWindow, onBeat bool
audio *audio
audioCh chan struct{}
atlas *text.Atlas
color color.Color
}
func (s *score) incrementScore(by float64) {
s.score += by
}
func (s *score) setMultiplier(multiplier int) {
if multiplier < 1 {
return
}
s.multiplier = multiplier
}
func (s *score) draw(win *pixelgl.Window, canvas *pixelgl.Canvas) {
multiplierText := text.New(s.multiplierPos, s.atlas)
multiplierText.Color = s.color
_, err := fmt.Fprintf(multiplierText, "%dx", s.multiplier)
if err != nil {
panic(err)
}
multiplierText.Draw(win, pixel.IM.Moved(canvas.Bounds().Min))
scoreText := text.New(s.scorePos, s.atlas)
scoreText.Color = s.color
_, err = fmt.Fprintf(scoreText, "%.0f", s.score)
if err != nil {
panic(err)
}
scorePos := canvas.Bounds().Min
scorePos.X = canvas.Bounds().Max.X - 40
scoreText.Draw(win, pixel.IM.Moved(scorePos))
}
func (s *score) changeTrack(track *audio) {
err := track.load()
if err != nil {
panic(err)
}
s.audio.cfn()
s.audio = track
s.audio.play(s.audioCh)
}
func (s *score) init() {
s.multiplier = 1
s.multiplierPos = pixel.V(20, 20)
s.scorePos = pixel.V(0, 20)
s.atlas = text.NewAtlas(
basicfont.Face7x13,
[]rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', '-'},
)
s.audioCh = make(chan struct{})
go func() {
// change audio track here
s.audio = acidJazzAudio
err := s.audio.load()
if err != nil {
panic(err)
}
// start the current track
go s.audio.play(s.audioCh)
}()
go func() {
select {
case <-s.audioCh:
//s.startTime = t
s.startTime = time.Now().Add(time.Nanosecond * 27000000)
return
}
}()
}
func (s *score) update(dt float64) {
// If time is within 10ms of the bpm (from start time)
timeSince := (time.Now().UnixNano() - s.startTime.UnixNano() - int64(dt*1e+9)) % (int64(60000000000 / s.audio.bpm))
if timeSince <= 100000000 || timeSince >= 400000000 {
s.timeWindow = true
} else {
s.timeWindow = false
}
if win.JustPressed(pixelgl.MouseButtonLeft) {
if s.timeWindow {
s.onBeat = true
if characterIsOutside {
s.increment++
if s.increment >= 8 {
s.multiplier++
s.increment = 0
}
if s.multiplier > 8 {
s.multiplier = 8
}
}
} else {
s.onBeat = false
if characterIsOutside {
s.increment = s.increment - 2
if s.increment <= 0 {
s.multiplier--
s.increment = 8
}
if s.multiplier < 1 {
s.multiplier = 1
}
}
}
}
// @TODO colours for scores, perhaps a little animated multi tone stuff for big ones
switch s.multiplier {
case 0:
s.multiplier = 1
case 1:
s.color = colornames.Aqua
case 2:
s.color = colornames.Blue
case 3:
s.color = colornames.Blueviolet
case 4:
s.color = colornames.Purple
case 5:
s.color = colornames.Deeppink
case 6:
s.color = colornames.Coral
case 7:
s.color = colornames.Orangered
case 8:
s.color = colornames.Red
}
}