-
Notifications
You must be signed in to change notification settings - Fork 7
/
game.go
339 lines (293 loc) · 5.95 KB
/
game.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/nsf/termbox-go"
"github.com/simulatedsimian/joystick"
)
type Highscore struct {
score int
name string
}
type ByScore []*Highscore
func (a ByScore) Len() int { return len(a) }
func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByScore) Less(i, j int) bool { return a[i].score < a[j].score }
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func tbrect(x, y, w, h int, fg, bg termbox.Attribute, border bool) {
end := " " + strings.Repeat("_", w)
if border {
tbprint(x, y-1, fg, bg, end)
}
s := strings.Repeat(" ", w)
if border {
s = fmt.Sprintf("%c%s%c", '|', s, '|')
}
for i := 0; i < h; i++ {
tbprint(x, y, fg, bg, s)
y++
}
if border {
tbprint(x, y, fg, bg, end)
}
}
// print a multi-line sprite
func tbprintsprite(x, y int, fg, bg termbox.Attribute, sprite string) {
lines := strings.Split(sprite, "\n")
for _, l := range lines {
tbprint(x, y, fg, bg, l)
y++
}
}
const (
highscoreFilename = "hs"
highscoreSeparator = ":"
maxHighscores = 5
fgDefault = termbox.ColorRed
bgDefault = termbox.ColorYellow
fps = 30
)
// GameState is used as an enum
type GameState uint8
const (
MenuState GameState = iota
HowtoState
PlayState
HighscoresState
WarnState
)
type Game struct {
highscores []*Highscore
state GameState
evq chan termbox.Event
timer <-chan time.Time
js joystick.Joystick
// frame counter
fc uint8
// highlighted menu item
hmi int
w int
h int
// fg and bg colors used when termbox.Clear() is called
cfg termbox.Attribute
cbg termbox.Attribute
}
func NewGame() *Game {
return &Game{
highscores: make([]*Highscore, 0),
evq: make(chan termbox.Event),
timer: time.Tick(time.Duration(1000/fps) * time.Millisecond),
fc: 1,
}
}
// Tick allows us to rate limit the FPS
func (g *Game) Tick() {
<-g.timer
g.fc++
if g.fc > fps {
g.fc = 1
}
}
func (g *Game) Listen() {
go func() {
for {
g.evq <- termbox.PollEvent()
}
}()
}
func (g *Game) HandleKey(k termbox.Key) {
switch g.state {
case MenuState:
g.HandleKeyMenu(k)
case HowtoState:
g.HandleKeyHowto(k)
case PlayState:
g.HandleKeyPlay(k)
case HighscoresState:
g.HandleKeyHighscores(k)
case WarnState:
g.HandleKeyWarn(k)
}
}
func (g *Game) FitScreen() {
termbox.Clear(g.cfg, g.cbg)
g.w, g.h = termbox.Size()
g.Draw()
}
func (g *Game) Draw() {
termbox.Clear(g.cfg, g.cbg)
switch g.state {
case MenuState:
g.DrawMenu()
case HowtoState:
g.DrawHowto()
case PlayState:
g.DrawPlay()
case HighscoresState:
g.DrawHighscores()
case WarnState:
g.DrawWarn()
}
termbox.Flush()
}
func (g *Game) ReadJoystick() {
if g.js != nil {
jstate, err := g.js.Read()
if err == nil {
if jstate.Buttons&1 != 0 {
g.HandleKey(termbox.KeySpace)
}
if jstate.AxisData[0] < -10000 {
g.HandleKey(termbox.KeyArrowLeft)
}
if jstate.AxisData[0] > 10000 {
g.HandleKey(termbox.KeyArrowRight)
}
}
}
}
func (g *Game) Update() {
g.Tick()
switch g.state {
case MenuState:
g.UpdateMenu()
case HowtoState:
g.UpdateHowto()
case PlayState:
g.ReadJoystick()
g.UpdatePlay()
case HighscoresState:
g.UpdateHighscores()
}
return
}
func (g *Game) loadHighscores() {
data, err := ioutil.ReadFile(highscoreFilename)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(data), "\n")
if len(lines) == 0 {
return
}
for _, l := range lines {
parts := strings.Split(l, highscoreSeparator)
if len(parts) != 2 {
log.Println("highscores file has been corrupted - please correct/delete it")
continue
} else if len(parts[0]) < 3 || len(parts[0]) > 10 {
log.Println("highscore file has been corrupted (name too long/short) - please correct/delete it")
continue
}
if i, err := strconv.Atoi(parts[1]); err == nil {
if i < 0 {
log.Println("negative highscore found - data corrupted - please correct/delete the hs file")
continue
}
g.highscores = append(g.highscores, &Highscore{i, parts[0]})
} else {
log.Fatalln(err)
}
}
sort.Sort(sort.Reverse(ByScore(g.highscores)))
if len(g.highscores) > 5 {
g.highscores = g.highscores[:5]
}
}
func loadHighscoresData(data []byte) error {
highscores := make([]*Highscore, 0)
lines := strings.Split(string(data), "\n")
if len(lines) == 0 {
return fmt.Errorf("no highscore data in file")
}
for _, l := range lines {
parts := strings.Split(l, highscoreSeparator)
if len(parts) != 2 {
return fmt.Errorf("corrupted highscore data")
}
if i, err := strconv.Atoi(parts[1]); err == nil {
if i < 0 {
return fmt.Errorf("negative highscore - data corrupted")
}
highscores = append(highscores, &Highscore{i, parts[0]})
} else {
return err
}
}
sort.Sort(sort.Reverse(ByScore(highscores)))
if len(highscores) > 5 {
highscores = highscores[:5]
}
return nil
}
func Fuzz(data []byte) int {
if err := loadHighscoresData(data); err != nil {
return 0
}
return 1
}
func (g *Game) checkSize() bool {
if g.w < logoLineLength+8 || g.h < (logoY+logoHeight+5+2) {
return false
}
return true
}
func main() {
if err := termbox.Init(); err != nil {
log.Fatalln(err)
}
termbox.SetOutputMode(termbox.Output256)
defer termbox.Close()
f, err := os.Create("diwe.log")
if err != nil {
log.Fatalln(err)
}
log.SetOutput(f)
g := NewGame()
js, _ := joystick.Open(0)
g.js = js
if _, err := os.Stat(highscoreFilename); err == nil {
g.loadHighscores()
}
g.Listen()
g.FitScreen()
if g.checkSize() {
g.GoMenu()
} else {
g.GoWarn()
}
g.FitScreen()
main:
for {
select {
case ev := <-g.evq:
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case 0:
if ev.Ch == 'q' {
break main
}
default:
g.HandleKey(ev.Key)
}
case termbox.EventResize:
g.FitScreen()
}
default:
}
g.Update()
g.Draw()
}
}