-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
379 lines (331 loc) · 8.33 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"fmt"
"github.com/ArchRobison/FrequonInvaders/coloring"
"github.com/ArchRobison/FrequonInvaders/fall"
"github.com/ArchRobison/FrequonInvaders/fourier"
"github.com/ArchRobison/FrequonInvaders/menu"
"github.com/ArchRobison/FrequonInvaders/radar"
"github.com/ArchRobison/FrequonInvaders/score"
"github.com/ArchRobison/FrequonInvaders/teletype"
"github.com/ArchRobison/FrequonInvaders/universe"
"github.com/ArchRobison/FrequonInvaders/vanity"
"github.com/ArchRobison/Gophetica/math32"
"github.com/ArchRobison/Gophetica/nimble"
"math/rand"
"time"
)
const title = "Frequon Invaders 2.2"
const edition = "(Go Edition)"
type context struct {
}
var lastTime float64
func updateClock() (dt float32) {
t := nimble.Now()
if lastTime > 0 {
dt = float32(t - lastTime)
} else {
dt = 0
}
lastTime = t
return
}
func (context) KeyDown(k nimble.Key) {
switch k {
case nimble.KeyEscape:
switch currentMode {
case modeSplash, modeVanity:
nimble.Quit()
case modeTraining, modeGame:
youLose()
case modeName:
// FIXME - ask for confirmation
nimble.Quit()
}
}
if currentMode == modeName {
if 0x20 <= k && k < 0x7F {
teletype.PrintCharUpper(rune(k))
} else {
switch k {
case nimble.KeyReturn:
acceptScore(uint8(universe.NKill()), teletype.CursorLine())
case nimble.KeyBackspace, nimble.KeyDelete:
teletype.Backup()
}
}
}
if devConfig {
// Shortcuts for debugging
switch k {
case 'b':
// Begin game
bootSequencePeriod = 0
setMode(modeGame)
case 'e':
// End game
youLose()
case 'r':
// Reset score file
records = make([]vanity.Record, 0)
vanity.WriteToFile(records)
case 's':
// Score a point
universe.TallyKill()
case 't':
// Begin training
bootSequencePeriod = 0
setMode(modeTraining)
}
}
}
var mouseX, mouseY int32
func (context) ObserveMouse(event nimble.MouseEvent, x, y int32) {
mouseX, mouseY = x, y
for _, m := range menuBar {
m.TrackMouse(event, x, y)
}
}
var (
fourierIsVisible = false
fallIsVisible = false
radarIsVisible = false
radarIsRunning = false
scoreIsVisible = false
dividerCount = 0
)
var cursorIsVisible = true
var invStorage = make([]fall.Invader, universe.MaxCritter)
func (context) Render(pm nimble.PixMap) {
dt := updateClock()
// Advance the boot sequence
advanceBootSequence(dt)
// Draw dividers
for i, r := range divider {
if i < dividerCount {
pm.DrawRect(r, nimble.White)
} else {
pm.DrawRect(r, nimble.Black)
}
}
if fourierIsVisible {
// Update universe
xf, yf := fourierPort.RelativeToLeftTop(mouseX, mouseY)
switch universe.Update(dt, xf, yf) {
case universe.GameWin:
youWin()
case universe.GameLose:
youLose()
}
updateZoom(dt)
// Fourier view
if rollPhase.Value {
updatePhaseRoll(dt)
}
drawFrequonsFourier(pm.Intersect(fourierPort))
drawFrequonsSpatial(pm.Intersect(fourierPort), xf, yf)
tallyFourierFrame()
} else {
// Teletype view
teletype.Draw(pm.Intersect(fourierPort))
}
// Fall view
if fallIsVisible {
inv := invStorage[:len(universe.Zoo)-1]
for k := range inv {
c := &universe.Zoo[k+1]
inv[k] = fall.Invader{
Progress: c.Progress,
Amplitude: c.Amplitude,
Color: pastels.Pixel(0, int32(c.Id)),
}
}
fall.Draw(pm.Intersect(fallPort), inv)
} else {
pm.DrawRect(fallPort, nimble.Black)
}
// Radar view
if radarIsVisible {
radar.Draw(pm.Intersect(radarPort), universe.Scheme(), radarIsRunning)
} else {
pm.DrawRect(radarPort, nimble.Black)
}
// Score
if scoreIsVisible {
score.Draw(pm.Intersect(scorePort), universe.NKill())
} else {
pm.DrawRect(scorePort, nimble.Black)
}
// Menu bar
if len(menuBar) > 0 {
menu.DrawMenuBar(pm, menuBar)
}
// Cursor
showCursor := true
switch currentMode {
case modeGame:
showCursor = false
case modeTraining:
showCursor = !fourierPort.Contains(mouseX, mouseY)
}
if showCursor != cursorIsVisible {
nimble.ShowCursor(showCursor)
cursorIsVisible = showCursor
}
}
var fallPort, radarPort, scorePort, fourierPort nimble.Rect
var divider [3]nimble.Rect
var screenWidth, screenHeight int32
var pastels nimble.PixMap
func (context) Init(width, height int32) {
if width < 640 || height < 400 {
panic(fmt.Sprintf("screen size of %v x %v is too small!", width, height))
}
screenWidth, screenHeight = width, height
nShade := int32(math32.Round(math32.Sqrt(float32(width*height)) * (32. / 1440)))
initCritterSprites(width, height)
pastels = coloring.PastelPalette(universe.MaxCritter, nShade)
teletype.Init("Characters.png")
if benchmarking {
bootSequencePeriod = 0
setMode(modeTraining)
} else {
setMode(modeSplash) // N.B. also causes partitionScreen to be called
if devConfig {
teletype.Print("[debug mode]\n")
}
}
}
func partitionScreen(width, height int32) {
if width <= 0 || height <= 0 {
panic(fmt.Sprintf("partitionScreen: width=%v height=%v", width, height))
}
panelWidth := width / 8 / 6 * 6
var menuHeight int32 = 0
if len(menuBar) > 0 {
_, menuHeight = menuBar[0].TabSize()
}
fourierPort = nimble.Rect{Left: panelWidth + 1, Top: menuHeight, Right: width, Bottom: height}
universe.Init(fourierPort.Size())
scoreBottom := height
scoreTop := scoreBottom - panelWidth/6
scorePort = nimble.Rect{Left: 0, Top: scoreTop, Right: panelWidth, Bottom: scoreBottom}
radarBottom := scoreTop - 1
radarTop := radarBottom - panelWidth
radarPort = nimble.Rect{Left: 0, Top: radarTop, Right: panelWidth, Bottom: radarBottom}
fallBottom := radarTop - 1
fallTop := menuHeight
fallPort = nimble.Rect{Left: 0, Top: fallTop, Right: panelWidth, Bottom: fallBottom}
divider[0] = nimble.Rect{Left: panelWidth, Top: menuHeight, Right: panelWidth + 1, Bottom: height}
divider[1] = nimble.Rect{Left: 0, Top: fallBottom, Right: panelWidth, Bottom: radarTop}
divider[2] = nimble.Rect{Left: 0, Top: radarBottom, Right: panelWidth, Bottom: scoreTop}
fall.Init(fallPort.Size())
radar.Init(radarPort.Size())
score.Init(scorePort.Size())
fourier.Init(fourierPort.Width(), universe.MaxCritter)
}
func youLose() {
setZoom(zoomShrink)
radarIsRunning = false
}
func youWin() {
setZoom(zoomShrink)
}
type mode int8
var currentMode mode
const (
modeSplash = mode(iota)
modeTraining
modeGame
modeName
modeVanity
)
var records []vanity.Record
func acceptScore(score uint8, name string) {
records = vanity.Insert(records, score, name)
err := vanity.WriteToFile(records)
setMode(modeVanity)
if err != nil {
teletype.Print(err.Error())
}
}
func setMode(m mode) {
fourierIsVisible = false
fallIsVisible = false
radarIsVisible = false
scoreIsVisible = false
dividerCount = 0
switch m {
case modeSplash:
teletype.PrintUpper(title + "\n")
teletype.PrintUpper(edition + "\n")
teletype.PrintUpper("By Arch D. Robison\n")
var err error
records, err = vanity.ReadFromFile()
if err != nil {
if devConfig {
teletype.Print(err.Error())
}
}
case modeName:
teletype.Reset()
teletype.PrintUpper(grammar.GenerateWithNumber(rune('H'), universe.NKill()) + "\n")
teletype.PrintUpper("Please enter your name:\n")
teletype.DisplayCursor(true)
case modeTraining, modeGame:
universe.BeginGame(m == modeTraining)
// Temporarily set NLiveMax to 0. End of boot sequence will set it to 1.
universe.SetNLiveMax(0)
startBootSequence()
case modeVanity:
teletype.Reset()
teletype.DisplayCursor(false)
teletype.PrintUpper("Supreme Freqs\n\nScore Player\n")
for _, r := range records {
teletype.PrintUpper(fmt.Sprintf("%5d %s\n", r.Score, r.Name))
}
}
currentMode = m
setMenus(m)
}
func endGame() {
teletype.Reset()
n := universe.NKill()
if n >= 64 {
teletype.PrintUpper(grammar.Generate(rune('W')) + "\n")
}
if vanity.IsWorthyScore(records, uint8(n)) {
setMode(modeName)
} else {
setMode(modeVanity)
}
}
type windowSpec struct {
width, height int32
title string
}
func (w *windowSpec) Size() (width, height int32) {
return w.width, w.height
}
func (w *windowSpec) Title() string {
return w.title
}
func main() {
var winSpec nimble.WindowSpec = nil
if devConfig {
for _, fun := range profileStart() {
defer fun()
}
if benchmarking {
winSpec = &windowSpec{1920, 1080, "benchmark"}
} else {
winSpec = &windowSpec{1024, 600, "debug"}
}
}
initMenuItem()
rand.Seed(time.Now().UnixNano())
nimble.AddRenderClient(context{})
nimble.AddMouseObserver(context{})
nimble.AddKeyObserver(context{})
nimble.Run(winSpec)
}