-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (161 loc) · 3.91 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
package main
import (
"flag"
"fmt"
"math/rand/v2"
"os"
"time"
"github.com/gdamore/tcell/v2"
)
const JPSPC = ' '
type options struct {
fps uint
lingerFrames uint
cooldownFrames uint
raindropRate uint
}
var (
width int
height int
defStyle = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
textStyle = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorWhite)
dropStyle = tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorWhite)
)
func watchEvents(s tcell.Screen, quit chan<- struct{}, pause chan<- struct{}, resume chan<- struct{}) {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape, tcell.KeyCtrlC:
close(quit)
return
case tcell.KeyCtrlL:
s.Sync()
}
case *tcell.EventResize:
s.Sync()
width, height = ev.Size()
if width < 30 || height < 30 {
pause <- struct{}{}
} else {
resume <- struct{}{}
}
}
}
}
type cell struct {
col int
row int
}
func runSim(opts options, s tcell.Screen, quit <-chan struct{}, pause <-chan struct{}, resume <-chan struct{}, hss []string) {
t := time.NewTicker(time.Second / time.Duration(opts.fps))
running := false
ds := []drop{}
var countBetweenDrops uint = 0
hai := newHaiku(hss[rand.IntN(len(hss))], randomAnchorPos(width, height))
for {
select {
case <-quit:
return
case <-pause:
if running {
running = false
}
case <-resume:
ds = []drop{}
hai = newHaiku(hss[rand.IntN(len(hss))], randomAnchorPos(width, height))
s.Clear()
running = true
case <-t.C:
if running {
// logic
// update haiku state machine
hai.updateState(opts)
if hai.state == "done" {
hai = newHaiku(hss[rand.IntN(len(hss))], randomAnchorPos(width, height))
}
// check existing drop positions intersect haiku letters
toFlip := map[cell]rune{}
for _, d := range ds {
toFlip[d.pos] = intersect(hai, d)
}
// create new drop
countBetweenDrops++
if countBetweenDrops >= opts.raindropRate {
countBetweenDrops -= opts.raindropRate
c := rand.IntN(width)
c = c - (c % 2) // skip odd columns
ds = append(ds, drop{cell{c, -1}, false})
}
// step all drops and filter out finished
newds := []drop{}
for _, d := range ds {
d.step()
if !d.done {
newds = append(newds, d)
}
}
ds = newds
// render
// render changed characters
for ce, ch := range toFlip {
s.SetContent(ce.col, ce.row, ch, nil, textStyle)
}
// render drops; should overwrite / be drawn over characters
for _, d := range ds {
// set drop tile
s.SetContent(d.pos.col, d.pos.row, JPSPC, nil, dropStyle)
}
// update display
s.Show()
}
}
}
}
func main() {
opts := options{}
flag.UintVar(&opts.fps, "fps", 30, "frames per second")
flag.UintVar(&opts.lingerFrames, "linger", 400, "frames to let haiku linger")
flag.UintVar(&opts.cooldownFrames, "cooldown", 400, "frames to wait between haikus")
flag.UintVar(&opts.raindropRate, "raindropRate", 1, "frames per raindrop")
flag.Parse()
// initialize tcell screen
screen, err := tcell.NewScreen()
if err != nil {
panic(err)
}
err = screen.Init()
if err != nil {
panic(err)
}
// clean up on quit
cleanUp := func() {
maybePanic := recover()
screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
// fmt.Println("asdf")
os.Exit(0)
}
defer cleanUp()
// set styling
screen.HideCursor()
screen.SetStyle(defStyle)
screen.Clear()
// get initial bounds
width, height = screen.Size() // non-inclusive
// load haikus
hss, err := parseHaikuStrings(haikusFile)
if err != nil {
fmt.Println(err)
return
}
// start sim
quit := make(chan struct{})
pause := make(chan struct{}, 1)
resume := make(chan struct{})
go watchEvents(screen, quit, pause, resume)
runSim(opts, screen, quit, pause, resume, hss)
}